vue+eacharts 折线圆点 拖拽

前言:
本文章主要根据eacharts官网实例进行改装,实现单个圆点,多条曲线实现当前某条曲线进行拖拽数据进行变化
话不多少上代码,(希望此篇文章 可以帮助小伙伴们完成 图表的拖拽功能!!!!!!)

此处代码为封装子组件 ,直接在父组件引用即可

<template>
  <div>
    <div id="linEacharts" :style="{width:width,height:height}"></div>
  </div>
</template>

<script>
import { DataSet } from '@antv/data-set'
import $echarts from 'echarts'
export default {
  name: 'LinEacharts',
  props: {
    data: { //传输数据
      type: Array,
      default: () => [
        { uuid: 20921, typicalVal: 1, gridPlanUuid: null, type: '00:00:00', forecastVal: 11.4 },
        { uuid: 21498, typicalVal: 1, gridPlanUuid: null, type: '00:05:00', forecastVal: 2.85 },
        { uuid: 21499, typicalVal: 2, gridPlanUuid: null, type: '00:10:00', forecastVal: 2.85 },
        { uuid: 21500, typicalVal: 3, gridPlanUuid: null, type: '00:15:00', forecastVal: 2.85 },
        { uuid: 21501, typicalVal: 4, gridPlanUuid: null, type: '00:20:00', forecastVal: 2.85 },
        { uuid: 21502, typicalVal: 5, gridPlanUuid: null, type: '00:25:00', forecastVal: 2.85 },
        { uuid: 21503, typicalVal: 6, gridPlanUuid: null, type: '00:30:00', forecastVal: 2.85 },
        { uuid: 21504, typicalVal: 7, gridPlanUuid: null, type: '00:35:00', forecastVal: 2.85 },
        { uuid: 21505, typicalVal: 8, gridPlanUuid: null, type: '00:40:00', forecastVal: 2.85 },
        { uuid: 21506, typicalVal: 9, gridPlanUuid: null, type: '00:45:00', forecastVal: 2.85 },
        { uuid: 21507, typicalVal: 5, gridPlanUuid: null, type: '00:50:00', forecastVal: 2.85 },
        { uuid: 21508, typicalVal: 6, gridPlanUuid: null, type: '00:55:00', forecastVal: 2.85 },
        { uuid: 21509, typicalVal: 5, gridPlanUuid: null, type: '01:00:00', forecastVal: 2.85 },
        { uuid: 21510, typicalVal: 5, gridPlanUuid: null, type: '01:05:00', forecastVal: 2.85 },
        { uuid: 21511, typicalVal: 4, gridPlanUuid: null, type: '01:10:00', forecastVal: 2.85 },
        { uuid: 21512, typicalVal: 3, gridPlanUuid: null, type: '01:15:00', forecastVal: 2.85 },
      ],
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '450px',
    },
    number: {
      type: Number,
      default: 0,
    },
    padding: {
      type: Object,
      default: () => {
        return { top: 40, right: 50, bottom: 0, left: 50 }
      }, // 上右下左
    },
    fields: {
      // y轴  折线显示的字段
      type: Array,
      default: () => ['forecastVal', 'typicalVal'],
    },
    xAxisName: {
      type: String, //X轴字段名
      default: 'type',
    },
    // 别名,需要的格式:[{field:'name',alias:'姓名'}, {field:'sex',alias:'性别'}]
    aliases: {
      type: Array,
      default: () => [
        { field: 'forecastVal', alias: '预测' },
        { field: 'typicalVal', alias: '典型' },
      ],
    },
    // 伸缩条
    dataZoom: {
      type: Array,
      default: () => [
        {
          show: false,
          realtime: true,
          // start: 30,
          // end: 70,
          // xAxisIndex: [0, 1]
        },
      ], // 上右下左
    },
  },
  data() {
    return {
      myChart: null,
      time: [],
      titleList: [],
      seriesValue: [],
      chartVaule: [],
    }
  },
  watch: {
    data: function (n, o) { //监听 数据变化是 更新图表
      this.init()
    },
    number: function (m, n) { //number控制那条折线为主线
      this.init()
    },
    deep: true,
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    this.myChart.clear()
  },
  methods: {
    init() { //实例
      this.myChart = $echarts.init(document.getElementById('linEacharts'))
      this.initCharts()
      window.addEventListener('resize', () => {
        if (this.myChart) {
          this.myChart.resize()
        }
      })
    },
    initCharts(list, index) {
      if (list) { //父组件调子组件传过来的值 
        this.data = list
      } 
      
      this.seriesValue = []
      this.titleList = []
      //此循环 主要是来将对应的字段数据进行替换
      this.fields.map((item, index) => {
        console.log(item)
        this.time = []
        for (let i = 0; i < this.aliases.length; i++) {
          if (item == this.aliases[i].field) {
            this.chartVaule = []
            this.data.map((dataItem) => {
              this.chartVaule.push(dataItem[item]) //预测值
              this.time.push(dataItem[this.xAxisName])
            })
            this.seriesValue.push({
              name: this.aliases[i].alias,
              data: this.chartVaule,
              seriesIndex: index,
              type: 'line',
              smooth: true,
              symbolSize: 5,
              areaStyle: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(235, 81, 118, 0.3)',
                    },
                    {
                      offset: 1,
                      color: 'rgba(235, 81, 118,0)',
                    },
                  ],
                  globalCoord: false,
                },
              },
            })
            this.titleList.push(this.aliases[i].alias)
          }
        }
      })
      this.myChart.setOption(
        {
          tooltip: {
            trigger: 'axis',
          },
          xAxis: {
            type: 'category',
            data: this.time,
            axisLine: { onZero: false },
          },
          yAxis: {
            type: 'value',
          },
          legend: {
            data: [...new Set(this.titleList)],
          },
          dataZoom: this.dataZoom,
          tooltip: {
            trigger: 'axis',
          },
          grid: {
            top: this.padding.top,
            left: this.padding.left,
            right: this.padding.right,
            bottom: this.padding.bottom,
            containLabel: true,
          },
          series: this.seriesValue,
        },
        true
      )
      this.DragShow(this.seriesValue[this.number]['data'])
    },
   // 实现拖拽
    DragShow(yAxisData) {
      const that = this
      // 拖拽
      setTimeout(function () {
        that.myChart.setOption({
          graphic: $echarts.util.map(yAxisData, function (item, dataIndex) {
            let position = that.myChart.convertToPixel({ seriesIndex: that.number }, [dataIndex, item])
            return {
              id: dataIndex,
              type: 'circle',
              position: position,
              shape: {
                r: 5,
              },
              invisible: true,
              draggable: true,
              ondrag: $echarts.util.curry(onPointDragging, dataIndex),
              onmousemove: $echarts.util.curry(showTooltip, dataIndex), 
              onmouseout: $echarts.util.curry(hideTooltip, dataIndex),
              ondragend: $echarts.util.curry(onPointDragEnd, dataIndex),
              z: 100,
            }
          }),
        })
      }, 0)
      window.addEventListener('resize', updatePosition)
      that.myChart.on('dataZoom', updatePosition)

      function updatePosition() {
        that.myChart.setOption({
          graphic: $echarts.util.map(yAxisData, function (item, dataIndex) {
            return {
              position: that.myChart.convertToPixel({ seriesIndex: that.number }, [dataIndex, item]),
            }
          }),
        })
      }

      function showTooltip(dataIndex) {
        that.myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: that.number,
          dataIndex: dataIndex,
        })
      }

      function hideTooltip(dataIndex) {
        that.myChart.dispatchAction({
          type: 'hideTip',
        })
      }
     //拖拽开始进行改变 当前圆点的位置  
      function onPointDragging(dataIndex, dx, dy) {
        yAxisData[dataIndex] = that.myChart.convertFromPixel({ seriesIndex: that.number }, this.position)[1]
        that.myChart.setOption({
          series: that.seriesValue,
        })
      }
     //拖拽结束 将当前圆点位置 对应的数据进行赋值
      function onPointDragEnd(dataIndex, dx, dy) {
        that.myChart.setOption({
          graphic: $echarts.util.map(yAxisData, function (item, dataIndex) {
            return {
              id: dataIndex,
              position: that.myChart.convertToPixel({ seriesIndex: that.number }, [dataIndex, item]),
            }
          }),
        })
        // 赋值结束 传输给父组件 进行数据更新
        that.$emit('setData', {
          setNumber: that.number,
          seriesData: that.seriesValue,
        })
      }
    },
  },
}
</script>

<style>
</style>

父组件引入子组件调用

     <LineEacharts
         //实现混动条
            :dataZoom="[{ 
          show: true,
          realtime: true,
        }]"
            @setData="getData" // 子传父过来的数据    
            :number="number"  //传入当前需要拖拽的下标
            ref="LineEacharts"
            :data="chartsList" //传入的数据
            :padding="{top:'10%',right:'4%',bottom:'4%',left:'4%'}"  //设置图表的padding 值
            :xAxisName="'type'"  //X轴的字段
            :fields="['forecastVal','typicalVal']" //传入Y轴的折线数量以及字段名称
            :aliases="[{field:'forecastVal',alias:'预测'}, {field:'typicalVal',alias:'典型'}]" //需要替换的字段名字
          ></LineEacharts>

运用子传父 方法名字来接收数据

  getData(value) {
console.log(value) //子传父过来的数据 
    },

以上是所有代码,如果需要根据实际状况进行修改即可运用,可能会存在 改变时不时太顺畅有卡顿现象,这点正在进一步优化,随时更新 !有好的建议希望留言!!!!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269