chart.js - 对 Y 轴 + 图表 Js 上的时间单位进行排序
问题描述
我在 Y 轴上有时间,在 X 轴上有日期。我已经使用 chart.js 构建了图表,我的图表如下所示。
但是,如您所见,我的 Y 轴顺序相反。我不知道如何解决它。请协助。
这是我的js
var s1 = {
label:'S1',
borderColor: '#33b5e5',
data: [
{ x: '2017-01-06 00:00:00', y: '2017-01-06 04:15:30' },
{ x: '2017-01-07 00:00:00', y: '2017-01-06 07:39:30' },
{ x: '2017-01-08 00:00:00', y: '2017-01-06 06:39:30' },
{ x: '2017-01-09 00:00:00', y: '2017-01-06 08:00:30' },
{ x: '2017-01-10 00:00:00', y: '2017-01-06 05:39:30' },
{ x: '2017-01-11 00:00:00', y: '2017-01-06 09:39:30' },
]
};
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: { datasets: [s1] },
options: {
legend: {
display: false
},
scales: {
xAxes: [{
type: 'time',
weight : 0 ,
time: {
unit:'day'
}
}],
yAxes: [{
type: 'time',
//type: 'linear',
reverse: false,
time: {
unit:'hour'
},
ticks: {
beginAtZero:true
}
}]
}
}
});
chart.canvas.parentNode.style.height = '380px';
chart.canvas.parentNode.style.width = '700px';
请在下面的链接中找到完整的代码片段。 https://jsfiddle.net/sjabiulla/3d1tpwhy/1/
解决方案
Chart.js 2.8.0增加了reverse
对 time scales的支持,尽管除了发行说明之外似乎没有记录在任何地方。reverse
进入滴答配置,它应该可以解决问题:
yAxes: [{
ticks: {
reverse: true,
...
},
...
}]
这是一个工作示例:
var s1 = {
label: 'S1',
borderColor: '#33b5e5',
data: [{
x: '2017-01-06 00:00:00',
y: '2017-01-06 04:15:30'
},
{
x: '2017-01-07 00:00:00',
y: '2017-01-06 07:39:30'
},
{
x: '2017-01-08 00:00:00',
y: '2017-01-06 06:39:30'
},
{
x: '2017-01-09 00:00:00',
y: '2017-01-06 08:00:30'
},
{
x: '2017-01-10 00:00:00',
y: '2017-01-06 05:39:30'
},
{
x: '2017-01-11 00:00:00',
y: '2017-01-06 09:39:30'
},
]
};
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [s1]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
type: 'time',
weight: 0,
time: {
unit: 'day'
}
}],
yAxes: [{
type: 'time',
time: {
unit: 'hour'
},
ticks: {
reverse: true,
beginAtZero: true
}
}]
}
}
});
chart.canvas.parentNode.style.height = '380px';
chart.canvas.parentNode.style.width = '700px';
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>
推荐阅读
- typescript - 打字稿重载使用参数值来定义另一个参数的类型
- javascript - 具有自定义组件的样式组件不应用样式
- r - 闪亮而阴谋混乱
- javascript - 反应本机图像显示空白
- python - 从 python tcp 服务器到 nodejs 客户端的 AES 加密数据
- oracle - 在总帐 Oracle Fusion 20c 中查找日记帐批准日期
- matplotlib - how to animate over time properly. static background map (cartopy) + dynamic contourf + dynamic title
- sql - 子查询中的错误返回查询的所有数据
- php - 将参数从命令行传递到脚本
- google-chrome - Devtools 元素选项卡非常慢