javascript - 为什么这些 2.9 ChartJS 条形图不一样?
问题描述
使用 ChartJS 2.9.4,为什么这 2 个图表不同?
<div >
<canvas id="topcanvas"></canvas>
<canvas id="bottomcanvas"></canvas>
</div>
var leftdata = {
labels: [0, 1, 2, 3, 4, 5],
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var rightdata = {
labels: [0, 1, 2, 3, "4", 5], // the #4 being a string is the only difference
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var options = {
scales: {
xAxes: [{
id: "A",
display: false,
barPercentage: 1.25,
ticks: {
max: 4
}
},
{
id: "B",
display: false,
stacked: true,
offset: true,
barPercentage: 1.25,
ticks: {
max: 4
}
},
{
display: true,
ticks: {
autoSkip: false,
max: 5
}
}
],
yAxes: [{
id: "bar-y-axis1",
ticks: {
beginAtZero: true
}
}]
}
};
var leftctx = document.getElementById("topcanvas").getContext("2d");
new Chart(leftctx, {
type: 'bar',
data: leftdata,
options: options
});
var rightctx = document.getElementById("bottomcanvas").getContext("2d");
new Chart(rightctx, {
type: 'bar',
data: rightdata,
options: options
});
解决方案
此行为已在 lib 的 v3 中修复
var leftdata = {
labels: [0, 1, 2, 3, 4, 5],
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var rightdata = {
labels: [0, 1, 2, 3, "4", 5], // the #4 being a string is the only difference
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var options = {
scales: {
A: {
display: false,
barPercentage: 1.25,
ticks: {
max: 4
},
position: 'bottom'
},
B: {
display: false,
stacked: true,
offset: true,
barPercentage: 1.25,
position: 'bottom',
ticks: {
max: 4
}
},
x: {
display: true,
ticks: {
autoSkip: false,
max: 5
}
},
y: {
id: "bar-y-axis1",
ticks: {
beginAtZero: true
}
}
}
};
var leftctx = document.getElementById("topcanvas").getContext("2d");
new Chart(leftctx, {
type: 'bar',
data: leftdata,
options: options
});
var rightctx = document.getElementById("bottomcanvas").getContext("2d");
new Chart(rightctx, {
type: 'bar',
data: rightdata,
options: options
});
<div class="grid">
<canvas id="topcanvas"></canvas>
<canvas id="bottomcanvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
</div>
推荐阅读
- java - `@Transactional` 不适用于 Spring 2 控制器
- python - 在python(shell)中返回文件名的长度
- python - 如何使用 Python Client Rancher 拨打电话
- python - 最新的 Tenorflow 版本 2.2.0 支持哪些 Cuda 和 cudnn 版本?
- html - 如何使用 Selenium 从网站获取不断变化的价值
- java - 在 Spring Boot(Maven)中运行时找不到外部 jar 类
- .net - 我在 kafka 制作人身上遗漏了一些东西,我正在向所有主题发送修复信
- python - 将列的值映射到另一列
- visual-studio-code - VS代码工作区中选定的文件夹路径
- reactjs - react-tag-autocomplete 与 redux 形式