【发布时间】:2021-12-17 08:36:47
【问题描述】:
我是 chart.js 的新手,我正在尝试将散点图与折线图结合起来。
我一直在努力解决两个问题:
1.- 我已经成功绘制了散点图,但是没有显示折线图。没有错误信息被抛出。
2.- 我想在底部添加一些标签,但经过多次接近后,图表仅显示 x 轴上的数字。
我附上了一张图片,这样你就可以看到我现在在哪里。我手动绘制了折线图。
这是我正在使用的代码:
<script>
const ctx = document.getElementById('myChart');
Chart.defaults.elements.point.pointStyle = 'dash';
Chart.defaults.elements.point.borderWidth = 2;
Chart.defaults.elements.point.radius = 12;
const labels1 = ['A', 'B','C','T','GW','RT','MJ','JY','YJ','TR','UY','IY','TR','RE','WE','WE','WE','BV','CS', 'EW'];
const data1 = {
datasets: [
{
type: 'line',
label: 'Line Dataset',
data: [10, 10, 10, 10],
backgroundColor: 'rgb(0, 0, 255)',
borderColor: 'rgb(0, 0, 255)'
},
{
type: 'scatter',
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(255, 0, 0)',
data: [{x:1, y:36}, {x:1, y:37}, {x:1, y:40}, {x:1, y:40}, //.... and many more!!
}
],
};
const myChart = new Chart(ctx, {
type: 'scatter',
data: data1,
labels: labels1,
options: {
scales: {
x: {
min: 0,
max: 19,
ticks: {stepSize: 1}
},
y: {
min: 20,
max: 120,
ticks: {stepSize: 10},
grid:{display:false}
},
}
}
});
</script>
我尝试将 labels: labels1 放置在很多地方,但它们从未显示出来。 如附图所示,折线图也未显示。
Chart.js 版本为 3.6.2
任何帮助将不胜感激。
最好的问候!!
【问题讨论】:
标签: javascript charts chart.js