这是一个小家伙 https://embed.plnkr.co/plunk/49kIfy8OShzlzu5J 解释了这个问题。
如果图表type
更改为bar
或line
,则工作正常。但是它无法呈现horizontalBar
-没有错误或警告表明任何问题,如果有的话。
horizontalBar
是否需要用户手动且明确地设置标签,还是这里缺少我的配置设置?
答案 0 :(得分:0)
使用horizontalBar
时,必须确保将yAxis
定义为type: 'time'
。您的数据还必须反映这一点,并提供正确轴的数据。
{ y: '2020-01-01T00:00:00', x: Math.random() * 20 }
如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Line Chart</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div style="height: 450px; width: 300px">
<canvas id="canvas"></canvas>
</div>
<script>
const config = {
type: 'horizontalBar',
options: {
title: { text: 'Bar Chart', display: true },
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [
{
offset: true,
type: 'time'
},
]
},
},
data: {
datasets: [
{
label: 'Label 1',
borderColor: '#40407a',
backgroundColor: '#d1ccc0',
borderWidth: 3,
data: [
{ y: '2020-01-01T00:00:00', x: Math.random() * 20 },
{ y: '2020-01-01T01:00:00', x: Math.random() * 20 },
{ y: '2020-01-01T01:20:00', x: Math.random() * 20 },
{ y: '2020-01-01T02:10:00', x: Math.random() * 20 },
{ y: '2020-01-01T02:35:00', x: Math.random() * 20 },
{ y: '2020-01-01T03:00:00', x: Math.random() * 20 },
],
},
],
},
};
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
window.myLine = new Chart(ctx, config);
};
</script>
</body>
</html>