echarts点击当前图例高亮,其他置灰

   <div ref="chartContainer" style="height: 350px"></div>
    
   function initChart() {
      let that = this;
      if (
        that.chartInstance != null &&
        that.chartInstance != "" &&
        that.chartInstance != undefined
      ) {
        that.chartInstance.dispose();
      }
      const chartContainer = that.$refs.chartContainer;
      that.chartInstance = echarts.init(chartContainer);
      let options = {
        title: {
          text: "时序统计图",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: "6%",
          selectedMode: that.queryParams.stationId ? "single" : "multiple", // 单个/多个
          data: ['未来馨居',...],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: date,
          axisLine: {
            lineStyle: {
              color: ["#ccc", "#333", "#333", "#333", "#333", "#333", "#333"],
            },
          },
        },
        yAxis: {
          name: "mm",
          nameTextStyle: {
            padding: [0, 30, 0, 0],
          },
          type: "value",
        },
        series: [{},{},...],
      };
      // 绘制图表
      that.chartInstance.setOption(options, true);
      // 自适应窗口变化
      window.addEventListener("resize", that.handleResize);

      // 图例单击事件,点击时当前图例高亮,其他置灰,站点查询条件跟着变化
      that.chartInstance.on("legendselectchanged", function (event) {
        // console.log(event);
        const legendData = event.selected; // 获取所有图例数据 {未来馨居: false, 市政府: true,...}
        for (const key in legendData) {
          if (key !== event.name) {
            event.selected[key] = false;
          } else {
            event.selected[key] = true;
          }
        }
        options.legend.selectedMode = "single";
        options.legend.selected = legendData; // {未来馨居: true, 市政府: false,...}
        // 更新图表配置
        that.chartInstance.setOption(options, true);
      });
    };

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值