使用Echart的过程中,碰到过这样的一个问题有以下几组数据:
[['2020-10-20 15:03:30',5.0],['2020-10-21 15:03:30',7.0],['2020-10-20 15:05:30',8.0],['2020-10-23 15:03:30',9.0]]
[['2021-10-20 15:03:30',5.0],['2021-10-21 15:03:30',7.0],['2021-10-20 15:05:30',8.0],['2021-10-23 15:03:30',9.0]]
发现上面的数据时间都不一样,当出现这种情况时,xAxis的data可以不用设置了,直接在series里设置,如下:
series:[{
name: 'pn',
type: 'line',
data: [['2020-10-20 15:03:30',5.0],['2020-10-21 15:03:30',7.0],['2020-10-20 15:05:30',8.0],['2020-10-23 15:03:30',9.0]],
symbol: 'circle',
smooth: true,
hoverAnimation: true,
symbolSize: '3',
animation: false
};
{
name: 'pn1',
type: 'line',
data: [['2021-10-20 15:03:30',5.0],['2021-10-21 15:03:30',7.0],['2021-10-20 15:05:30',8.0],['2021-10-23 15:03:30',9.0]],
symbol: 'circle',
smooth: true,
hoverAnimation: true,
symbolSize: '3',
animation: false,
};
]
还有一种情况 有时候x轴的刻度可能不是我们想要的。例如 把时间格式化只要年,这个时候x轴的刻度顺序混乱的情况。这个时候,我们可以添加时间间隔,让其自动生成x轴刻度
xAxis: [{
type:'category',
maxInterVal: 3600 * 24 * 1000,//一天间隔
minInterVal: 3600 * 24 * 1000,//一天间隔
axisLabel: {
hideOverlap: true,
formatter:function (dates) {
//格式化 x轴显示的值
}
}
}],
series:[{
name: 'pn',
type: 'line',
data: [['2020-10-20 15:03:30',5.0],['2020-10-21 15:03:30',7.0],['2020-10-20 15:05:30',8.0],['2020-10-23 15:03:30',9.0]],
symbol: 'circle',
smooth: true,
hoverAnimation: true,
symbolSize: '3',
animation: false
};
{
name: 'pn1',
type: 'line',
data: [['2021-10-20 15:03:30',5.0],['2021-10-21 15:03:30',7.0],['2021-10-20 15:05:30',8.0],['2021-10-23 15:03:30',9.0]],
symbol: 'circle',
smooth: true,
hoverAnimation: true,
symbolSize: '3',
animation: false,
};
]