echarts 多条markline 游标线拖动

<template>
  <div>
    <div
      ref="lineChart"
      v-on-echart-resize
      :style="{ width: width, height: height + 'px' }"
    ></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
import "@/global/utils/chart.resize"; //浏览器缩放时,图表会自适应
export default {
  props: {
    lazyResize: {
      type: Number,
      default: 200
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: Number,
      default: 180
    },
    chartConfig: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      option: {},
      chart: null,
      threshold1: "",
      xData: ["1年", "2年", "3年", "4年", "5年", "6年", "7年", "8年"],
      yData: [8.1, 13.3, 1.5, 14.6, 10.8, 9.0, 9.2, 9.3],
      markLineData: [ 
        { xAxis: "1年", name: "8.1" },
        { xAxis: "3年", name: "1.5" }
      ]
    };
  },
  watch: {
  //监听拖动后游标的位置
    threshold1(n, v) {
      this.updatePosition(); //更新游标位置
      this.refreshChart();
    }
  },
  mounted() {
    this.restoreChart();
    window.addEventListener("resize", () => {
      this.restoreChart();
    });
  },
  methods: {
    restoreChart() {
      this.initData();
      this.initEchart();
      this.refreshChart();
    },
    // 绘制图表
    refreshChart() {
      if (!this.chart) return false;
      this.chart.setOption(this.option || {}, true);
      this.initGraphic();
      // 添加根据视口缩放重绘功能
      if (this.lazyResize) {
        window.onresize = () => {
          setTimeout(() => {
            this.chart.resize();
          }, this.lazyResize);
        };
      }
    },
    initGraphic() {
      let that = this;
      that.chart.setOption({
        graphic: echarts.util.map(this.markLineData, function(item, dataIndex) {
          return {
            type: "rect",
            z: 100,
            id: dataIndex,
            shape: {
              width: 0,
              height: 180
              // r: 10
            },
            position: [
              that.chart.convertToPixel({ xAxisId: "2" }, item.xAxis),
              0
            ],
            draggable: true,
            style: {
              fill: "rgba(231,174,173,.2)",
              stroke: "rgba(231,174,173,.2)",
              lineWidth: 4
            },
            cursor: "move",
            ondrag: that.onPointDragging,
            onmousemove: that.showTooltip,
            onmouseout: that.hideTooltip
          };
        })
      });
    },
    initEchart() {
      this.chart = echarts.init(this.$refs.lineChart);
    },
    initData() {
      if (!this.chartConfig) return;
      this.option = {
        color: ["#F8CB00"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器,坐标轴触发有效
            type: "line" // 默认为直线,可选为:'line' | 'shadow'
          }
        },
        grid: {
          containLabel: true,
          left: 12,
          top: 45,
          right: 40,
          bottom: 0
        },
        xAxis: [
          {
            id: "2",
            type: "category",
            data: this.xData
          }
        ],
        yAxis: {
          type: "value",
          id: "2",
          axisLabel: {
            formatter: "{value}"
          }
        },
        series: [
          {
            id: "aaa",
            name: "",
            type: "line",
            data: this.yData,
            markLine: {
              //开始标预警线
              itemStyle: {
                normal: {
                  borderWidth: 1,
                  lineStyle: {
                    type: "dash",
                    color: "red",
                    width: 2
                  },
                  label: {
                    textStyle: {
                      fontSize: 16,
                      fontWeight: "bolder"
                    }
                  }
                }
              },
              data: this.markLineData
            },
            itemStyle: {
              normal: {
                color: function(params) {
                  //根据预警线的值 显示对应的颜色
                  // build a color map as your need.
                  var colorList = ["#c23531", "#c5bf66 "];
                  // if (params.data > threshold) {
                  //   return colorList[0];
                  // } else if (params.data < threshold) {
                  //   return colorList[1];
                  // }
                }
              }
            }
          }
        ]
      };
    },
    updatePosition() {
      let that = this;
      that.chart.setOption({
        graphic: echarts.util.map(that.markLineData, function(item, dataIndex) {
          return {
            position: [
              that.chart.convertToPixel({ xAxisId: "2" }, item["xAxis"]),
              0
            ]
          };
        })
      });
    },
    showTooltip(e) {
      let that = this;
      that.chart.dispatchAction({
        type: "showTip",
        seriesIndex: 0,
        dataIndex: e.target.position
      });
    },
    hideTooltip() {
      let that = this;
      that.chart.dispatchAction({
        type: "hideTip"
      });
    },
    onPointDragging(e) {
      //阈值变动
      this.threshold1 = this.chart.convertFromPixel(
        { xAxisId: "2" },
        e.target.position[0]
      );
      if (this.threshold1 < 0) {
        this.threshold1 = 0;
      }
      if (this.threshold1 > this.xData.length - 1) {
        this.threshold1 = this.xData.length - 1;
      }
      this.markLineData[Number(e.target.id)] = {
        xAxis: this.xData[this.threshold1],
        name: this.yData[this.threshold1]
      };
    }
  }
};
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值