Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chart.js-将数组中的数据动态加载到数据集中_Javascript_Jquery_Arrays_Chart.js - Fatal编程技术网

Javascript Chart.js-将数组中的数据动态加载到数据集中

Javascript Chart.js-将数组中的数据动态加载到数据集中,javascript,jquery,arrays,chart.js,Javascript,Jquery,Arrays,Chart.js,我需要将数据动态加载到chart.js中。目前,我正在手动创建数据集中的每个块,如下代码所示: var outcomeData = "['Outcomes','Abstinent','Improved','Unchanged','Deteriorated'],['2015/16',18036,11414,14430,1880],['2016/17',17642,11688,14010,1738],['2017/18',17282,10796,13542,1686]"; var avgDa

我需要将数据动态加载到chart.js中。目前,我正在手动创建数据集中的每个块,如下代码所示:

var outcomeData = "['Outcomes','Abstinent','Improved','Unchanged','Deteriorated'],['2015/16',18036,11414,14430,1880],['2016/17',17642,11688,14010,1738],['2017/18',17282,10796,13542,1686]";
    var avgDaysUseData = "['year','AvgDaysUseAtStart','AvgDaysUseAtReview'],['2015/16',21.6,8.3],['2016/17',22.2,8.5],['2017/18',22.1,8.6]";
    var outcomeArray = outcomeData.replace(/\[/g, "").replace(/\]/g, "").replace(/\'/g, "").split(',');
    var avgDaysUseArray = avgDaysUseData.replace(/\[/g, "").replace(/\]/g, "").replace(/\'/g, "").split(',');
    var reportingPeriods = [];
    var outcomeNames = [];
    var outcomeValues = [];
    var avgDaysUseValues = [];

    $.each(avgDaysUseArray,
        function (index, value) {
            if (Number.isNaN(value) === false) {
                value = parseFloat(value);
                avgDaysUseValues.push(value);
            }
        });

    $.each(outcomeArray,
        function (index, value) {
            var reportingPeriod = new RegExp('\\d{4}(\/)\\d{2}');
            var isReportingPeriod = reportingPeriod.test(value);
            if (isReportingPeriod) {
                reportingPeriods.push(value);
            }
            else {
                var outcomeName = new RegExp('[a-zA-Z]');
                var isOutcomeName = outcomeName.test(value);
                if (isOutcomeName) {
                    if (value !== "Outcomes") {
                        outcomeNames.push(value);
                    }
                } else {
                    value = parseInt(value);
                    outcomeValues.push(value);
                }
            }
        });

    var ctx = document.getElementById('chart');

    var outcomeChart = new Chart(ctx,
        {
            type: 'bar',
            data: {
                labels: reportingPeriods,
                datasets: [
                    {
                        type: 'line', fill: false, lineTension: 0,
                        label: 'Average days of use at start',
                        data: [avgDaysUseValues[4], avgDaysUseValues[7], avgDaysUseValues[10]],
                        yAxisId: 'y-axis-1',
                        borderColor: 'Black',
                        backgroundColor: 'Black',
                        borderWidth: 2
                    },
                    {
                        type: 'line', fill: false, lineTension: 0,
                        label: 'Average days of use at review',
                        data: [avgDaysUseValues[5], avgDaysUseValues[8], avgDaysUseValues[11]],
                        yAxisId: 'y-axis-1',
                        borderColor: 'Blue',
                        backgroundColor: 'Blue',
                        borderWidth: 2
                    },
                    {
                        type: 'bar',
                        label: outcomeNames[0],
                        data: [outcomeValues[0], outcomeValues[4], outcomeValues[8]],
                        yAxisID: 'y-axis-2',
                        borderColor: '#FF9900',
                        backgroundColor: '#FF9900',
                        borderWidth: 2
                    },
                    {
                        type: 'bar',
                        label: outcomeNames[1],
                        data: [outcomeValues[1], outcomeValues[5], outcomeValues[9]],
                        yAxisID: 'y-axis-2',
                        borderColor: '#FF6400',
                        backgroundColor: '#FF6400',
                        borderWidth: 2
                    },
                    {
                        type: 'bar',
                        label: outcomeNames[2],
                        data: [outcomeValues[2], outcomeValues[6], outcomeValues[10]],
                        yAxisID: 'y-axis-2',
                        borderColor: '#FF0000',
                        backgroundColor: '#FF0000',
                        borderWidth: 2
                    },
                    {
                        type: 'bar',
                        label: outcomeNames[3],
                        data: [outcomeValues[3], outcomeValues[7], outcomeValues[11]],
                        yAxisID: 'y-axis-2',
                        borderColor: '#9A0033',
                        backgroundColor: '#9A0033',
                        borderWidth: 2
                    }
                ]
            },
            options: { scales: {
                xAxes: [
                    {
                        gridLines: {
                            display: false
                        },
                        scaleLabel: { display: true, labelString: 'Reporting Period', fontStyle: 'italic' },
                        position: 'bottom',
                        ticks: { fontStyle: 'italic' },
                        barPercentage: 0.95,
                        categoryPercentage: 0.15
                    }
                ],
                yAxes: [
                    {
                        scaleLabel: { display: true, labelString: 'Average no. of days', fontStyle: 'italic' },
                        position: 'right', id: 'y-axis-1', type: 'linear',
                        ticks: { min: 0, beginAtZero: true, max: 28 },
                        gridLines: {display: true}
                    },
                    {
                        scaleLabel: { display: true, labelString: 'No. of clients', fontStyle: 'italic' },
                        position: 'left', id: 'y-axis-2',type: 'linear',
                        gridLines: { display: false }
                    }]
            },
                responsive: true,
                maintainAspectRatio: false,
                legend: {
                    position: 'right',
                    display: true,
                    onClick: null,
                    labels: {
                        fontColor: '#000000'
                    }
                }
            }
        });

    outcomeChart.aspectRatio = 0;


    $('#chart').bind('contextmenu',
        function(e) {
            return false;
        });
我在代码笔中提供了此代码,以显示正在生成的内容:

我已经创建了许多数组,数据通过viewmodel从控制器动态加载到这些数组中


如何将outcomeArray和avgDaysUseArray中的数据加载到chart.js数据集中

您在控制器中使用的是哪种框架/语言?我在django(python)中也做过类似的事情。基本上,这个想法是在html模板中存储一个json数组,并在jquery中获得它,使用类似于
json.parse($(“#myDiv”).attr('data')| |{}')
it's C#和MVC 5的东西。应用程序中的数据以这种格式在一个视图包中传递“['outcouts','abstant','Improved','Improved','Unchanged','Unchanged','detered','degregated','detered',['2015/16',1803611444301880],['2016/17',17642,11688,14010,1738],['2017/18',17282,10796,13542,1686]";