如何为chart.js线图动态创建数据集?

时间:2022-12-04 10:03:03

I want to create a Line chart with multiple datasets dynamically in the chart.js library.

我想在chart.js库中动态创建包含多个数据集的折线图。

I am able to assign the data dynamically. But I want to create the datasets itself dynamically. I saw the link below:

我能够动态分配数据。但我想动态创建数据集本身。我看到下面的链接:

How to add the elements dynamically from the html table for chart.js

如何从chart.js的html表中动态添加元素

and tried this :

并试过这个:

var datasetValue = [];
for (var j = 0; j < count; j++) {
datasetValue[j] = [
{
fillColor: 'rgba(220,220,220,0.5)',
strokeColor :'rgba(220,220,220,1)' ,
title :'2013',
data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
}]
}

var mydata = {
datasets : datasetValue
}

Here the count value is 3 and I want to display the 3 lines in the chart and the count value will vary. Even though the chart line color and title will be the same, I want to display the line first and will change the rest once this is solved.

这里的计数值是3,我想在图表中显示3行,计数值会有所不同。即使图表线颜色和标题相同,我想先显示该行,并在解决后更改其余部分。

I tried to access the data like so :

我试图像这样访问数据:

alert("Datasets:"+mydata.datasets[0].data);

It should show the data of the first dataset but it's showing undefined.

它应该显示第一个数据集的数据,但它显示未定义。

However in the below case:

但是在下面的情况中:

var mydata1 = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
    {
        fillColor : "rgba(220,220,220,0.5)",
        strokeColor : "rgba(220,220,220,1)",
        data : [95,53,99,73,27,82,40],
        title : "2014"
    },
    {
        fillColor : "rgba(151,187,205,0.5)",
        strokeColor : "rgba(151,187,205,1)",
        data : [35,43,59,31,50,66,55],
        title : "2013"
    }
 ]
}

alert("Datasets:"+mydata1.datasets[0].data);

I am able to get the data of first dataset. Could anyone please give me the solution?

我能够获得第一个数据集的数据。有谁能请给我解决方案?

2 个解决方案

#1


9  

I think you are looking for something like the following solution. http://jsfiddle.net/5m63232a/

我认为你正在寻找类似以下解决方案的东西。 http://jsfiddle.net/5m63232a/

var datasetValue = [];
var count = 10;
for (var j=0; j<count; j++) {
    datasetValue[j] = {
        fillColor: 'rgba(220,220,220,0.5)',
        strokeColor :'rgba(220,220,220,1)',
        title :'2013',
        data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
    }
}
var mydata = {
    datasets : datasetValue
}
alert("Datasets: "+mydata.datasets[0].data);

#2


4  

Create service to fetch json formt like :

创建服务以获取json formt,如:

labels : ["January","February","March","April","May","June","July"],
        datasets : [
            {
                label: "My First dataset",
                fillColor : "rgba(220,220,220,0.2)",
                strokeColor : "rgba(220,220,220,1)",
                pointColor : "rgba(220,220,220,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(220,220,220,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            },
            {
                label: "My Second dataset",
                fillColor : "rgba(151,187,205,0.2)",
                strokeColor : "rgba(151,187,205,1)",
                pointColor : "rgba(151,187,205,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(151,187,205,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }
        ]  

Then add following javascript code

然后添加以下javascript代码

var j = [];
$.ajax({
    type: 'GET',
    url: 'http://' + window.location.host.toString() + "/path",
    dataType: 'json',
    success: function (data) {
       j = data;
    },
    async: false
 });
 var chartData = {
     labels: j.labels,
     datasets: j.datasets
 };
 window.onload = function () {
     var ctx = document.getElementById("canvas").getContext("2d");
     window.myLine = new Chart(ctx).Line(chartData, {
         responsive: true,
         animation: true,
         tooltipFillColor: "rgba(0,0,0,0.8)",
         multiTooltipTemplate: "<%= datasetLabel %> - <%=value %>"
     });
 }

#1


9  

I think you are looking for something like the following solution. http://jsfiddle.net/5m63232a/

我认为你正在寻找类似以下解决方案的东西。 http://jsfiddle.net/5m63232a/

var datasetValue = [];
var count = 10;
for (var j=0; j<count; j++) {
    datasetValue[j] = {
        fillColor: 'rgba(220,220,220,0.5)',
        strokeColor :'rgba(220,220,220,1)',
        title :'2013',
        data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
    }
}
var mydata = {
    datasets : datasetValue
}
alert("Datasets: "+mydata.datasets[0].data);

#2


4  

Create service to fetch json formt like :

创建服务以获取json formt,如:

labels : ["January","February","March","April","May","June","July"],
        datasets : [
            {
                label: "My First dataset",
                fillColor : "rgba(220,220,220,0.2)",
                strokeColor : "rgba(220,220,220,1)",
                pointColor : "rgba(220,220,220,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(220,220,220,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            },
            {
                label: "My Second dataset",
                fillColor : "rgba(151,187,205,0.2)",
                strokeColor : "rgba(151,187,205,1)",
                pointColor : "rgba(151,187,205,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(151,187,205,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }
        ]  

Then add following javascript code

然后添加以下javascript代码

var j = [];
$.ajax({
    type: 'GET',
    url: 'http://' + window.location.host.toString() + "/path",
    dataType: 'json',
    success: function (data) {
       j = data;
    },
    async: false
 });
 var chartData = {
     labels: j.labels,
     datasets: j.datasets
 };
 window.onload = function () {
     var ctx = document.getElementById("canvas").getContext("2d");
     window.myLine = new Chart(ctx).Line(chartData, {
         responsive: true,
         animation: true,
         tooltipFillColor: "rgba(0,0,0,0.8)",
         multiTooltipTemplate: "<%= datasetLabel %> - <%=value %>"
     });
 }