使用Chart.js在一个页面中绘制多个图表-DrawmultiplechartinonepagewithChart.js
作者:杜伟丿2552 | 来源:互联网 | 2023-06-12 11:50
Ineedtoshowmultiplechartfromdatawhichigetfromajaxsource.ButidontknowwhyChart.js
I need to show multiple chart from data which i get from ajax source. But i don't know why Chart.js only show one chart at a time although i made new dataset as well as new canvas for each chart. Please help me, this is the Javascript code which i used to draw chart.
我需要显示来自ajax源的数据的多个图表。但我不知道为什么Chart.js一次只显示一个图表,尽管我为每个图表创建了新的数据集和新的画布。请帮帮我,这是我用来绘制图表的Javascript代码。
function showBarChart(label, index, key){
var areaChartOptiOns= {
scaleBeginAtZero : true,
//Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true,
//String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)",
//Number - Width of the grid lines
scaleGridLineWidth : 1,
//Boolean - Whether to show horizontal lines (except X axis)
scaleShowHorizontalLines: true,
//Boolean - Whether to show vertical lines (except Y axis)
scaleShowVerticalLines: true,
//Boolean - If there is a stroke on each bar
barShowStroke : true,
//Number - Pixel width of the bar stroke
barStrokeWidth : 2,
//Number - Spacing between each of the X value sets
barValueSpacing : 5,
//Number - Spacing between data sets within X values
barDatasetSpacing : 1,
//String - A legend template
legendTemplate : "-legend\"><% for (var i=0; i- \"><%if(datasets[i].label){%><%=datasets[i].label%><%}%>
<%}%>
",
//Boolean - whether to make the chart responsive to window resizing
responsive: true,
animationSteps: 15,
scaleLabel: " <%=value%>"
};
document.getElementById('metterpanel').innerHTML = '';
for(i = 0; i ' + '' + '
' +
'
' + '
' + '
' + datakey
+'
' + '
' + '
' + '
'+ '
';
var startingData = {
labels: [label[datakey]],
datasets: [
{
label: datakey + ' Consumed',
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [index[datakey]]
}
]
};
//-------------
//- LINE CHART -
//--------------
var lineChartCanvas = $("#"+datakey).get(0).getContext("2d");
var datakey = new Chart(lineChartCanvas).Bar(startingData, areaChartOptions);
var lineChartOptiOns= areaChartOptions;
lineChartOptions.datasetFill = false;
}
//loop begin
//endloop
}
This is the code i used to get data from
这是我用来获取数据的代码
$.ajax({
type: "GET",
url: "/getListIndex?Data=" + stringlist,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (datas) {
showBarChart(datas.label, datas.index, datas.key);
},
failure: function (response) {
alert('error');
}
});
and this is the parent div which i append charts to
这是我附加图表的父div
It is just show the last data from data source and this is the json i got from ajax:
它只显示来自数据源的最后一个数据,这是我从ajax得到的json:
{"success":true,"label":{"740100001":"01:18:26","740100003":"03:53:33","740100004":"09:12:04"},"index":{"740100001":0,"740100003":0,"740100004":0},"date":"11:11:00","fulldate":{"740100001":"17\/11\/2015","740100003":"18\/11\/2015","740100004":"25\/11\/2015"},"key":["740100001","740100003","740100004"]}
Thank you.
1 个解决方案