我用chart.js构建了一个折线图.但是图表没有显示出来.这是我的HTML
这是我的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
});
1> 小智..:
在你的HTML上:
它应该是:
然后,替换您的代码
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);