gpt4 book ai didi

Chart.js 折线图未显示

转载 作者:行者123 更新时间:2023-12-01 08:28:10 26 4
gpt4 key购买 nike

我用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]
}
]

};

我使用的另一种方法类似于文档。我实例化了一个新图表。但是,这种方法为我的折线图返回未定义。
var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).line(data,{
belzierCurve : false

});

最佳答案

在您的 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);

关于Chart.js 折线图未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com