echarts 甘特图代码

本文详细介绍了如何使用ECharts库创建一个定制化的图表,展示时间序列数据,特别关注了x轴的时间间隔设置和开发者自定义图形元素的renderItem函数实现。通过实例代码,读者可以学习如何根据时间值绘制矩形区域,并配置轴标签和颜色。
摘要由CSDN通过智能技术生成

效果如图

 代码。。。

var option = {
      grid: {
        left: '1%',
        right: '5%',
        bottom: '3%',
        top: '1%',
        containLabel: true
      },
      xAxis: {
        type: 'value',
        position: 'top',
        interval: 2,   //以一个小时递增 
        max: 30,
        min: 0, //将data里最小时间的整点时间设为min,否则min会以data里面的min为开始进行整点递增
        axisLabel: {
          show: true,
          formatter: function (value, index) {
            return value ? value : '';
          }
        }
      },
      yAxis: {
        axisLine: {
          lineStyle: {
            color: 'rgba(0,0,0,0.1)',
          }
        },
        data: ['aaa', 'bbb', 'ccc', 'ddd'],
        axisLabel: {
          textStyle: {
            color: "rgba(0, 0, 0, 0.65)", //刻度颜色
            fontSize: 14  //刻度大小
          },
        }
      },
      series: [
        {
          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];
            var height = 20;
            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 轴
          },
          itemStyle: {
            normal: {
              color: '#0A8BFF'
            }
          },
          data: [[0, 3, 5], [0, 3, 5], [0, 6, 10], [1, 2, 3], [1, 3, 4], [1, 6, 8], [1, 12, 20]]
        }
      ]
    };

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值