[英]Chart.js issue in plotting numeric X and Y in line chart
I have bellow Code to display a Chart using Chart.js.我有波纹管代码来使用 Chart.js 显示图表。
<canvas id="canvasline" width="200" height="200"></canvas>
<script>
var ctx = document.getElementById('canvasline').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['0', '30', '50'],
datasets: [{
data: [
{ x: "0", y: 0 },
{ x: "10", y: 10 },
{ x: "20", y: 20 },
{ x: "30", y: 30 },
{ x: "40", y: 40 },
{ x: "50", y: 50 },
],
borderWidth: 1
}]
},
options: {
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
display: true,
suggestedMin: 0,
suggestedMax: 100,
maxTicksLimit: 10
},
gridLines: {
display: true
}
}],
xAxes: [{
ticks: {
display: true,
suggestedMin: 0,
suggestedMax: 100,
maxTicksLimit: 3
},
gridLines: {
display: true
}
}]
}
}
});
</script>
Working Code Example:工作代码示例:
https://jsfiddle.net/2vcrsq6n/ https://jsfiddle.net/2vcrsq6n/
I am facing the below issues:我面临以下问题:
When the data is specified as individual points through objects that contain an x
and an y
property, you should not define data.labels
.当数据通过包含x
和y
属性的对象指定为单个点时,您不应定义data.labels
。
Also make sure, the x
and y
values are numbers but not strings.还要确保x
和y
值是数字而不是字符串。
Please take a look at your amended code below that uses the most recent stable version of Chart.js (2.9.3).请查看下面使用最新稳定版本 Chart.js (2.9.3) 的修改后的代码。
var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'scatter', data: { datasets: [{ data: [ { x: 0, y: 0 }, { x: 10, y: 10 }, { x: 20, y: 20 }, { x: 30, y: 30 }, { x: 40, y: 40 }, { x: 50, y: 50 } ], showLine: true, fill: false, borderWidth: 1 }] }, options: { legend: { display: false }, scales: { yAxes: [{ ticks: { suggestedMin: 0, suggestedMax: 100, maxTicksLimit: 10 } }], xAxes: [{ ticks: { suggestedMin: 0, suggestedMax: 100, maxTicksLimit: 3 } }] } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script> <canvas id="myChart"></canvas>
To update your chart with new data, simply add or replace the data in the dataset and invoke chart.update()
afterwards.要使用新数据更新图表,只需添加或替换数据集中的数据,然后调用chart.update()
。
myChart.data.datasets[0].data = <new data>;
myChart.update();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.