[echarts] 用graphic模拟可移动的markline

在这里插入图片描述

用graphic模拟可移动的markline

let base = +new Date(1988, 9, 3);
let oneDay = 24 * 3600 * 1000;
let data = [[base, Math.random() * 300]];

for (let i = 1; i < 20000; i++) {
    let now = new Date((base += oneDay));
    data.push([+now, Math.round((Math.random() - 0.5) * 20 + data[i - 1][1])]);
}

option = {
  grid: {
    top: '40px',
    bottom: '60px',
    left: '50px',
    right: '30px'
  },
  xAxis: {
    type: 'time',
    boundaryGap: false,
    axisLine: {onZero: false},
    axisPointer:
    {
      show: true,
      type: 'line',
    },
  },
  yAxis: {
    type: 'value',
    boundaryGap: [0, '100%']
  },
  series: [
    {
    name: 'Fake Data',
    type: 'line',
    smooth: true,
    symbol: 'none',
    areaStyle: {},
    data: data
    }
  ],
  graphic: {
    elements: [
    {
    type: 'group',
    left: 'center',
    draggable: 'horizontal',
    ondrag: function (params) {    
      var pointInPixel = [params.offsetX, params.offsetY];
      var pointInGrid = myChart.convertFromPixel('grid', pointInPixel);

      var xTime = new Date(pointInGrid[0])

      //get closest value from cursor
      var point = data.reduce((prev, curr) => Math.abs(new Date(curr[0]).valueOf() - xTime.valueOf()) < Math.abs(new Date(prev[0]).valueOf() - xTime.valueOf()) ? curr : prev)

      //console.log('poi', new Date(pointInGrid[0]), new Date(point[0]), point[1])

      var d = document.getElementById('value2');
      d.style.left = params.offsetX+'px';
      d.innerHTML = point[1]
    },
    children: [
    {
      id: 'bar1',
      type: 'rect',
      top: '30px',
      shape: {
          width: 2,
          height: 685
      },
      style: {
          fill: "#ff0000"
      },
      cursor: 'ew-resize'
    },
    {
      type: 'circle',
      top: '740px',
      shape: {
          r:10
      },
      style: {
          fill: "#ff0000"
      },
    }
    ]
    },
    {
      type: 'group',
      left: '150px',
      draggable: 'horizontal',
      ondrag: function (params) {    
        var pointInPixel = [params.offsetX, params.offsetY];
        var pointInGrid = myChart.convertFromPixel('grid', pointInPixel);

        var xTime = new Date(pointInGrid[0])

        //get closest value from cursor
        var point = data.reduce((prev, curr) => Math.abs(new Date(curr[0]).valueOf() - xTime.valueOf()) < Math.abs(new Date(prev[0]).valueOf() - xTime.valueOf()) ? curr : prev)

        //console.log('poi', new Date(pointInGrid[0]), new Date(point[0]), point[1])

        var d = document.getElementById('value1');
        d.style.left = params.offsetX+'px';
        d.innerHTML = point[1]
      },
      children: [
        {
            type: 'rect',
            top: '30px',
            shape: {
                width: 2,
                height: 685
            },
            style: {
                fill: "#0000ff"
            },
            cursor: 'ew-resize'
        },
      ]
    },
  ]}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值