echarts自定义系列custom实现时间线图

echarts可以用custom自定义实现各种图形,需要提供一个renderItem方法,这里介绍一个自定义的时间线图,每天在干啥一目了然。

先生成一些假数据:

var data = [];
var dataCount = 10;
var startTime = +new Date('2023-03-01 09:00:00');
var categories = ['2023-03-01', '2023-03-02', '2023-03-03'];
var types = [
  { name: '吃饭', color: '#7b9ce1' },
  { name: '睡觉', color: '#bd6d6c' },
  { name: '学习', color: '#75d874' },
  { name: '摸鱼', color: '#e0bc78' },
  { name: '贤者时间', color: '#dc77dc' },
  { name: '开黑', color: '#72b362' }
];
categories.forEach(function (category, index) {
  var baseTime = startTime;
  for (var i = 0; i < dataCount; i++) {
    var typeItem = types[Math.round(Math.random() * (types.length - 1))];
    var duration = Math.round(Math.random() * 10000 * 1000);
    data.push({
      name: typeItem.name,
      value: [index, baseTime, (baseTime += duration), duration],
      itemStyle: {
        normal: {
          color: typeItem.color
        }
      }
    });
    baseTime += Math.round(Math.random() * 2000);
  }
});

然后设置echarts的option

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.6;
  var rectShape = 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
    }
  );
  return (
    rectShape && {
      type: 'rect',
      transition: ['shape'],
      shape: rectShape,
      style: api.style()
    }
  );
}
function formate(val) {
  let h = new Date(val).getHours();
  let m = new Date(val).getMinutes();
  let s = new Date(val).getSeconds();
  return h + ':' + m + ':' + s;
}
function getDuration(val) {
  let temp = Math.ceil(val / 1000);
  let minutes = Math.ceil(temp / 60);
  let seconds = temp % 60;
  return minutes + '分钟' + seconds + '秒';
}
option = {
  tooltip: {
    formatter: function (params) {
      return params.marker + params.name + '<br>'
      + '开始 ' + formate(params.value[1]) + '<br>'
      + '结束 ' + formate(params.value[2]) + '<br>'
      + '持续 ' + getDuration(params.value[3]);
    }
  },
  dataZoom: [
    {
      type: 'slider',
      filterMode: 'weakFilter',
      showDataShadow: false,
      top: 400,
      labelFormatter: ''
    },
    {
      type: 'inside',
      filterMode: 'weakFilter'
    }
  ],
  grid: {
    height: 300
  },
  xAxis: {
    min: startTime,
    scale: true,
    axisLabel: {
      formatter: function (val) {
        let h = new Date(val).getHours();
        let m = new Date(val).getMinutes();
        let s = new Date(val).getSeconds();
        return h + ':' + m + ':' + s;
      }
    }
  },
  yAxis: {
    data: categories
  },
  series: [
    {
      type: 'custom',
      renderItem: renderItem,
      itemStyle: {
        opacity: 0.8
      },
      encode: {
        x: [1, 2],
        y: 0
      },
      data: data
    }
  ]
};

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摸鱼857

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值