ECharts渲染连续图形
首页 > 随笔    作者:sunflower   2018年12月28日 10:12 星期五   热度:2333°   百度已收录  
时间:2018-12-28 10:12   热度:2333° 
function setChart(cdata) {
    var data = [];
    var color = '#14B4C2'; //填充颜色
    var emphasisColor = '#1AF4B8'; //鼠标滑入颜色
    for (var i = 0; i < cdata.length; i++) {
        data.push({
            value: [
                0,
                cdata[i].startTime,
                cdata[i].endTime,
                cdata[i].endTime - cdata[i].startTime
            ],
            itemStyle: {
                normal: {
                    color: color
                },
                emphasis: {
                    color: emphasisColor
                }
            }
        });
    }
    //params:包含了当前数据信息和坐标系的信息
    //api:是一些开发者可调用的方法集合,api.value(...):意思是取出 dataItem 中的数值,api.coord(...):意思是进行坐标转换计算
    function renderItem(params, api) {
        var categoryIndex = api.value(0);
        var start = api.coord([api.value(1), categoryIndex]);
        var end = api.coord([api.value(2), categoryIndex]);
        var height = api.size([0, 1])[1] * 0.7;

        return {
            type: 'rect',
            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(),
            styleEmphasis: api.styleEmphasis()
        };
    }

    var option = {
        grid: {
            height: 30,
            top: 15,
            left: 35,
            bottom: 0,
            right: 35
        },
        tooltip: {
            show: true,
            formatter: function (value, index) {
                return '起:' + getDate(value.value[1]) + '<br/>' + '止:' +getDate(value.value[2]);
//              return '起:' + moment(value.value[1]).format('MM-DD HH:mm:ss') + '<br/>' + '止:' + moment(value.value[2]).format('MM-DD HH:mm:ss');
            }
        },
        xAxis: {
            type: 'time',
            axisLabel: {
                show: true,
                textStyle: {color: '#396386'},
                formatter: function (value, index) {
                    //return moment(value).format('HH:mm').split(' ')[0]}
                    var value = getDate(value);
                    return value.split(' ')[0].substr(5) + '\n' + value.split(' ')[1];
                }
            },
            axisLine: {lineStyle: {color: '#265d7f'}}, //x轴的颜色
            axisTick: {lineStyle: {color: '#265d7f'}, alignWithLabel: true},
            splitNumber: 17,
            splitLine: {show: false}
        },
        yAxis: {
            data: 'd',
            axisLabel: {show: false},
            axisLine: {show: false},
            axisTick: {show: false},
            splitLine: {
                show: true,
                lineStyle: {color: '#265d7f'}
            }
        },
        series: [{
            type: 'custom',
            renderItem: renderItem,
            itemStyle: {
                normal: {
                    opacity: 0.8
                }
            },
            encode: {
                x: [1, 2],
                y: 0
            },
            data: data
        }]
    };
    
    myChart = echarts.init(document.getElementById('lastRect'));
    myChart.setOption(option);
    window.onresize=myChart.resize;

}

setChart([{startTime: 1543647087349, endTime: 1543648887000}]);//初始化时间条

二维码加载中...
本文作者:sunflower      文章标题: ECharts渲染连续图形
本文地址:?post=286
版权声明:若无注明,本文皆为“覆手为雨”原创,转载请保留文章出处。
分享本文至:

返回顶部    首页    手机版本    后花园   会员注册   
版权所有:覆手为雨    站长: sunflower