我用chart.js构建了一个折线图。但是图表没有显示出来。这是我的HTML
<canvas id="myChart" width"600" height:"600"></canvas>
这是我的javascript。我没有遇到这种方法的错误,但没有出现任何错误。
var c = $('#myChart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("myChart").getContext("2d").Line(data);
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July","August", "November", "December"],
datasets: [
{
label: "Sodium intake",
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: [165, 159, 180, 181, 156, 155, 140]
},
{
label: "Sugar intake",
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: [128, 148, 140, 119, 186, 127, 190]
}
]
};
我使用的另一种方法与文档类似。我实例化了一个新图表。但是,此方法为我的折线图返回undefined。
var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).line(data,{
belzierCurve : false
});
答案 0 :(得分:8)
在你的HTML上:
<canvas id="myChart" width"600" height:"600"></canvas>
应该是:
<canvas id="myChart" width="600" height="600"></canvas>
然后,替换你的代码
var c = $('#myChart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("myChart").getContext("2d").Line(data);
使用此代码
var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);
所以在你的js
var data = {
//your data here
}
enter code here
然后在数据
之后添加此行var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);
所以你的代码看起来像这样:
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July","August", "November", "December"],
datasets: [
{
label: "Sodium intake",
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: [165, 159, 180, 181, 156, 155, 140]
},
{
label: "Sugar intake",
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: [128, 148, 140, 119, 186, 127, 190]
}
]
}
var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);
答案 1 :(得分:2)
好的,试试这个:
开始纠正你的HTML:
<canvas id="myChart" width="600" height="600"></canvas>
然后尝试使用这个javascript:
var data = {
"labels": ["January", "February", "March", "April", "May", "June", "July", "August", "November", "December"],
"datasets": [{
label: "Sodium intake",
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: [165, 159, 180, 181, 156, 155, 140]
}, {
label: "Sugar intake",
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: [128, 148, 140, 119, 186, 127, 190]
}]
}
var chartDisplay = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);
这应该没问题。查看jsfiddle of it。
答案 2 :(得分:0)
原因可能是您使用的是最新的chart.min.js,但使用的是旧代码。要获得正确的参考,请遵循chartjs.org文档。
最新版本中的代码配置结构已更改。 (我猜从2.3开始)
所以,而不是
var countries= document.getElementById("countries").getContext("2d");
var chart = new Chart(countries).Pie(pieData,pieOptions);
我们的结构如下:
var countries= document.getElementById("countries").getContext("2d");
var chart = new Chart(countries,{
type: 'pie',
data:
{
labels: ['India', 'Germany', 'Netherland'],
datasets:
[{
backgroundColor: '#337ab7',
data: ['100', '99' ,'98'],
borderWidth:1,
}]
},
options:
{
responsive: true,
maintainAspectRatio:false,
legend: {
display: false,
position: 'top'
}
}
});
或
var countries= document.getElementById("countries").getContext("2d");
var pieData =
{
labels: ['India', 'Germany', 'Netherland'],
datasets: [{
backgroundColor: '#337ab7',
data: ['100', '99' ,'98'],
borderWidth:1,
}]
};
var pieOptions =
{
responsive: true,
maintainAspectRatio:false,
legend: {
display: false,
position: 'top'
}
};
var chart = new Chart(countries,{
type: 'pie',
data: pieData,
options: pieOptions
});