javascript - Chart JS 中体积数据集的规模较小
问题描述
我正在绘制一些股票数据,我想将交易量放在底部较小的灰色条数据集中,就像在雅虎财经中一样。
我想要的是:
小的灰色数据集位于底部。
我得到什么:
三个不同的 Y 尺度和体积数据集的大小是相同的。
我的代码:
当我创建图表时:
new Chart(context, {
type: 'line',
data: {
labels: timestamps.map(n =>
(new Date(n * 1000)).toLocaleString()
),
datasets: [
dataset,
vdataset
]
},
options: {
maintainAspectRatio: false,
scales: {
xAxis: {
// type: 'time'
ticks: {
//autoSkip: true,
maxTicksLimit: 10
}
},
yAxes: [{
id: 'volume',
display: false
}],
y: {
beginAtZero: false
},
},
animation: {
duration: 0
},
plugins: {
autocolors: false,
annotation: {
annotations
}
},
tooltips: {
mode: 'nearest'
},
hover: {
intersect: false
}
}
});
体积数据集:
{
type: 'bar',
label: `Volume`,
data: ...,
backgroundColor: 'transparent',
borderColor: 'grey',
fill: 'origin',
pointRadius: 0,
borderWidth: 2,
yAxisID: 'volume'
}
解决方案
你需要给每个数据集一个yAxisID
,然后你应该用这个 id 命名你的轴,如下所示:
数据:
var data = {
labels: labels,
datasets: [{
label: 'My First Dataset',
data: generateTestSeries(),
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1,
yAxisID: "something",
},
{
label: 'My Second Dataset',
data: generateTestSeries(),
fill: false,
borderColor: 'rgb(182, 50, 192)',
tension: 0.1,
yAxisID: "volume",
},
]
};
秤:
scales: {
volume: {
axis: "y",
min: -80,
max: 60,
position: 'right',
},
something: {
axis: "y",
min: -20,
max: 70,
position: 'left',
}
}
此外,您不应该有列表 yAxes,这就是在 chartjs 2.x 中定义轴的方式
这是一个使用中的小提琴:JSFiddle
编辑:如果您想要一个不可见的轴,只需设置display
为false
现在更新的小提琴中所见。
推荐阅读
- python - 如何在银河坐标系中绘制地球观测站的视野?
- c++ - 在使用预发布版本中使用 cmake 实现 Z3
- r - 即使在 {r, message=FALSE} 时,R Markdown Knitted PDF doc 也会继续显示消息
- vb.net - 文本框搜索重音字符问题?
- c# - LINQ Group 通过显示不正确的结果
- python - twitter 搜索 api - 只选择文本而不是用户
- reactjs - 在 GatsbyJS 中将自定义 React 组件导入 Markdown
- ssh - 当SSH在本地机器上终止时保持jupyter实验室笔记本运行?
- r - R中的$m是什么意思?
- javascript - 如何在 Angular 9 中显示文件的内容?