我终于将我的多轴功能端口推到了这个前叉上的主机上。作为对正在发生的事情的极其简短的概述,我在比例中添加了一组轴。每个轴都能够计算一个值相对于其自身比例的 y 位置。开发人员现在唯一需要做的就是说明每个数据集需要属于哪个轴组。每个轴还可以被告知它应该是什么颜色以及它应该出现在图表的哪一侧。
如何使用它 - 在每个数据集中声明一个名为的新属性yAxesGroup
datasets: [{
label: "My First dataset",
type: "bar",
yAxesGroup: "1", //any with same group will appear in the same axis, also this can be anything as long as you always refer to it with the same name
fillColor: "rgba(151,137,200,0.5)",
strokeColor: "rgba(151,137,200,0.8)",
highlightFill: "rgba(151,137,200,0.75)",
highlightStroke: "rgba(151,137,200,1)",
data: [28, 48, 40, 19, 86, 27, 90]
},
然后为该轴提供更多选项,向名为的数据添加一个额外的属性yAxis
这是轴选项的数组。默认情况下,每个轴都会在左侧显示一个比例,并以默认颜色或您设置为整体字体颜色的颜色显示,但您可以在此处覆盖它们
var overlayData = {
labels: ["January", "Februarya", "March", "April", "May", "Jun", "July"],
datasets: [{
label: "My First dataset",
type: "bar",
yAxesGroup: "1",
fillColor: "rgba(151,137,200,0.5)",
strokeColor: "rgba(151,137,200,0.8)",
highlightFill: "rgba(151,137,200,0.75)",
highlightStroke: "rgba(151,137,200,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}, {
label: "My Second dataset",
type: "line",
yAxesGroup: "2",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [8, 38, 30, 29, 46, 67, 80]
}],
yAxes: [{
name: "1",
scalePositionLeft: false,
scaleFontColor: "rgba(151,137,200,0.8)"
}, {
name: "2",
scalePositionLeft: true,
scaleFontColor: "rgba(151,187,205,0.8)"
}]
};
现在该图表将有 2 个轴,一个在左侧,一个在右侧。
有几点需要注意,如果任何值低于 0,则需要设置scaleBeginAtZero
to false
否则会有点混乱。此外,每个轴都有自己的 0 基线,因此 2 条边不会对齐。这正如我所预期的那样,因为我认为你有两个轴的原因是因为它们显示不同的比例,所以为了让所有东西都很好地坐在 0 上,每个轴需要处于不同的位置。
我已经对它进行了测试,以针对我添加的其他功能进行回归,这一切似乎都运行良好,但如果您确实使用它并注意到任何奇怪的事情,请告诉我。
另外,我无法将其编写为 ChartJS 的插件,因为核心中涉及的内容太多,因此它没有意义,因为您可以从 fork 构建版本,而不仅仅是覆盖 ChartJS 的原始版本。
总的来说,这就是它会产生的效果
var overlayData = {
labels: ["January", "Februarya", "March", "April", "May", "Jun", "July"],
datasets: [{
label: "My First dataset",
type: "bar",
yAxesGroup: "1",
fillColor: "rgba(151,137,200,0.5)",
strokeColor: "rgba(151,137,200,0.8)",
highlightFill: "rgba(151,137,200,0.75)",
highlightStroke: "rgba(151,137,200,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}, {
label: "My Second dataset",
type: "line",
yAxesGroup: "2",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [8, 38, 30, 29, 46, 67, 80]
}],
yAxes: [{
name: "1",
scalePositionLeft: false,
scaleFontColor: "rgba(151,137,200,0.8)"
}, {
name: "2",
scalePositionLeft: true,
scaleFontColor: "rgba(151,187,205,0.8)"
}]
};
window.onload = function () {
window.myOverlayChart = new Chart(document.getElementById("canvas").getContext("2d")).Overlay(overlayData, {
populateSparseData: true,
overlayBars: false,
datasetFill: true,
});
}
<script src="https://raw.githack.com/leighquince/Chart.js/master/Chart.js"></script>
<canvas id="canvas" height="300" width="300"></canvas>