使用echarts实现时间状态区间图

该图例参考了官方的demo:https://echarts.baidu.com/examples/editor.html?c=custom-profile以及https://www.cnblogs.com/cindy-hmy/p/8251193.html


    var colors = ['#2f4554', '#d48265','#c23531']; //三种状态的颜色
    var state = ['深睡', '浅睡','清醒']; //三种状态
 
    // echart配置
    var option = {
        color: colors,
        tooltip: {//提示框
            formatter: function (params) {
                return params.name + ':' + params.value[1] + '~' + params.value[2];
            }//数据的值
        },
        legend: {//图例
            data: state,
            bottom: '1%',
            selectedMode: false, // 图例设为不可点击
            textStyle: {
                color: '#000'
            }
        },
        grid: {//绘图网格
            left: '3%',
            right: '3%',
            top: '1%',
            bottom: '10%',
            containLabel: true
        },
        xAxis: {                   
            type: 'time',               
            interval: 3600  * 1000,   //以一个小时递增 
            min:'2009/6/1 1:00', //将data里最小时间的整点时间设为min,否则min会以data里面的min为开始进行整点递增
            axisLabel: {formatter: function (value) { 
                var date = new Date(value); return getzf(date.getHours()) + ':00';  
                function getzf(num) {
                	if (parseInt(num) < 10) {  num = '0' + num; }  
                	return num; 
                }             
              },                   
            }
        },
        yAxis: {
            data: ['深睡', '浅睡', '清醒']
        },
        series: [
            // 用空bar来显示三个图例
            { name: state[0], type: 'bar', data: [] },
            { name: state[1], type: 'bar', data: [] },
            { name: state[2], type: 'bar', data: [] },
            {
                type: 'custom',                
                renderItem: function (params, api) {//开发者自定义的图形元素渲染逻辑,是通过书写 renderItem 函数实现的
                    var categoryIndex = api.value(0);//这里使用 api.value(0) 取出当前 dataItem 中第一个维度的数值。
                    var start = api.coord([api.value(1), categoryIndex]); // 这里使用 api.coord(...) 将数值在当前坐标系中转换成为屏幕上的点的像素值。
                    var end = api.coord([api.value(2), categoryIndex]);
                    var height = api.size([0, 1])[1];
 
                    return {
                        type: 'rect',// 表示这个图形元素是矩形。还可以是 'circle', 'sector', 'polygon' 等等。
                        shape: echarts.graphic.clipRectByRect({ // 矩形的位置和大小。
                            x: start[0],
                            y: start[1] - height / 2,
                            width: end[0] - start[0],
                            height: height
                        }, { // 当前坐标系的包围盒。
                            x: params.coordSys.x,
                            y: params.coordSys.y,
                            width: params.coordSys.width,
                            height: params.coordSys.height
                        }),
                        style: api.style()
                    };
                },
                encode: {
                    x: [1, 2], // data 中『维度1』和『维度2』对应到 X 轴
                    y: 0// data 中『维度0』对应到 Y 轴
                },
                data: [ // 维度0 维度1 维度2
                  {
                      itemStyle: { normal: { color: colors[0] } },//条形颜色
                      name: '深睡',
                      value: [0, '2009/6/1 1:28', '2009/6/1 5:00']//0,1,2代表y轴的索引,后两位代表x轴数据开始和结束
                  },
                    {
                        itemStyle: { normal: { color: colors[0] } },
                        name: '深睡',
                        value: [0, '2009/6/1 6:13', '2009/6/1 8:22']
                    },
                   {
                       itemStyle: { normal: { color: colors[1] } },
                       name: '浅睡',
                       value: [1, '2009/6/1 5:00', '2009/6/1 6:13']
                   },
                    {
                        itemStyle: { normal: { color: colors[1] } },
                        name: '浅睡',
                        value: [1, '2009/6/1 8:22', '2009/6/1 9:10']
                    },
                    {
                        itemStyle: { normal: { color: colors[1] } },
                        name: '浅睡',
                        value: [1, '2009/6/1 12:47', '2009/6/1 14:52']
                    },
                  {
                      itemStyle: { normal: { color: colors[2] } },
                      name: '清醒',
                      value: [2, '2009/6/1 9:10', '2009/6/1 12:47']
                  },
                {
                    itemStyle: { normal: { color: colors[2] } },
                    name: '清醒',
                    value: [2, '2009/6/1 14:52', '2009/6/1 17:00']
                },
                ]
            }
        ]
    };

最终效果如下图:
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值