Javascript 如何使用图表JS中的x和y从数据中堆叠条形图
我试图用具有x和y值的数据绘制堆栈条,例如Javascript 如何使用图表JS中的x和y从数据中堆叠条形图,javascript,chart.js,Javascript,Chart.js,我试图用具有x和y值的数据绘制堆栈条,例如{x:1,y:1}。 但是,图表JS似乎遵循数据中传递的数组的索引,索引在标签中,而不是x。 我还尝试使用了记号:{source:'data'},但没有用 我不得不取消对标签的注释:,并在非洲数据集中添加索引1和索引2,以查看所需的结果 有什么办法吗 <canvas id="barchart" width="800" height="450"></canvas> <script> var ctx = document.
{x:1,y:1}
。
但是,图表JS似乎遵循数据中传递的数组的索引,索引在标签中,而不是x
。
我还尝试使用了记号:{source:'data'}
,但没有用
我不得不取消对标签的注释:
,并在非洲数据集中添加索引1和索引2,以查看所需的结果
有什么办法吗
<canvas id="barchart" width="800" height="450"></canvas>
<script>
var ctx = document.getElementById("barchart").getContext('2d');
var barchart = new Chart(ctx, {
type: 'bar',
data: {
//labels: [0,1,2,3],
datasets: [{
data: [{x: 0, y: 2}, {x: 3, y: 1}],
label: "Africa",
backgroundColor: "#3e95cd",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 2}],
label: "Asia",
backgroundColor: "#8e5ea2",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Europe",
backgroundColor: "#3cba9f",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Latin America",
backgroundColor: "#e8c3b9",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "North America",
backgroundColor: "#c45850",
fill: false
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
},
responsive: false,
maintainAspectRatio: false,
scales: {
xAxes: [{
stacked: true,
ticks: {source: 'data'}
}],
yAxes: [{
stacked: true
}]
}
}
});
</script>
var ctx=document.getElementById(“条形图”).getContext(“2d”);
var条形图=新图表(ctx{
类型:'bar',
数据:{
//标签:[0,1,2,3],
数据集:[{
数据:[{x:0,y:2},{x:3,y:1}],
标签:“非洲”,
背景色:“3e95cd”,
填充:假
}, {
数据:[{x:0,y:1},{x:1,y:1},{x:2,y:1},{x:3,y:2}],
标签:“亚洲”,
背景色:“8e5ea2”,
填充:假
}, {
数据:[{x:0,y:1},{x:1,y:1},{x:2,y:1},{x:3,y:1}],
标签:“欧洲”,
背景色:“3cba9f”,
填充:假
}, {
数据:[{x:0,y:1},{x:1,y:1},{x:2,y:1},{x:3,y:1}],
标签:“拉丁美洲”,
背景色:“e8c3b9”,
填充:假
}, {
数据:[{x:0,y:1},{x:1,y:1},{x:2,y:1},{x:3,y:1}],
标签:“北美”,
背景颜色:“c45850”,
填充:假
}
]
},
选项:{
图例:{display:false},
标题:{
显示:对,
正文:“预测2050年世界人口(百万)”
},
回答:错,
MaintaintAspectRatio:false,
比例:{
xAxes:[{
对,,
记号:{source:'data'}
}],
雅克斯:[{
是的
}]
}
}
});
您面临的问题是什么?