echarts 柱图点击图表之外的按钮让图表中的多条柱子颜色高亮

需求:在ECharts中,想要通过点击图表之外的按钮来让图表中的某一个或某几个柱子颜色高亮

有个更简单的方法:

 文章链接地址: echarts 点击(柱图、折线图)图表外的按钮,高亮图表中的某些柱子何点位_echarts 柱状图悬浮高亮-CSDN博客

以下是一个基本的步骤指南,用于实现这一功能:

  1. 定义初始图表配置:在初始化图表时,确保你的series中的itemStyle可以配置颜色。
  2. 添加按钮和点击事件:在HTML中添加一个按钮,并为该按钮添加一个点击事件监听器。
  3. 编写事件处理函数:在事件处理函数中,修改ECharts的option对象,特别是与itemStyle相关的部分,以设置你希望高亮的柱子的颜色。
  4. 更新图表:使用ECharts实例的setOption方法来应用修改后的option对象,从而更新图表并显示高亮效果。

这里是一个简化的示例代码片段,用于说明如何实现这一功能:

<template>
  <div class="box">
    <div class="leftBox">
      <div
        class="lineChart"
        id="myCustomChart"
        ref="myCustomChart"
        :style="myChartStyle"
      ></div>
    </div>
    <div class="rightBox">
      <el-radio-group v-model="radio1" @change="setHighBar(radio1)">
        <el-radio-button label="一月"></el-radio-button>
        <el-radio-button label="二月"></el-radio-button>
        <el-radio-button label="三月"></el-radio-button>
        <el-radio-button label="四月"></el-radio-button>
      </el-radio-group>
    </div>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  data() {
    radio1: "",
    chart: null,
    // 图表样式
    myChartStyle: {
      float: "left",
      width: "100%",
      height: "100%",
      backgroundColor: "rgba(38, 53, 106,0.5)",
    },
    ht: [
      // 这里是对象数组
      { name: "一月", value: 1100 },
      { name: "二月", value: 1200 },
      { name: "三月", value: 800 },
      { name: "四月", value: 900 },
      { name: "五月", value: 1000 },
      { name: "六月", value: 400 },
      { name: "七月", value: 500 },
      { name: "八月", value: 1200 },
      { name: "九月", value: 1150 },
      { name: "十月", value: 800 },
      { name: "十一月", value: 1026 },
      { name: "十二月", value: 1300 },
    ],
    option: {},
  },
  mounted() {
    this.initCustomChart();
  },
  methods: {
    // 初始化 echarts 图表
    initCustomChart() {
      let myData5 = [
        "1月",
        "2月",
        "3月",
        "4月",
        "5月",
        "6月",
        "7月",
        "8月",
        "9月",
        "10月",
        "11月",
        "12月",
      ];
      this.chart = echarts.init(this.$refs.myCustomChart);
      this.option = {
        backgroundColor: "#091022",
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(0, 255, 233,0)",
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255, 255, 255,1)",
                  },
                  {
                    offset: 1,
                    color: "rgba(0, 255, 233,0)",
                  },
                ],
                global: false,
              },
            },
          },
          textStyle: {
            color: "#fff",
          },
          backgroundColor: "rgba(0, 58, 99, 0.8)", //设置背景颜色
          borderColor: "rgba(0, 58, 99, 0.8)",
          confine: true,
          formatter: "{b}:{c}份合同",
        },
        grid: {
          top: "14%",
          left: "2%",
          right: "4%",
          bottom: "0",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: myData5,
          axisLabel: {
            color: "#A4A7AA",
            interval: 0,
            fontSize: 12,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(127, 214, 255, .4)",
            },
          },
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
          name: "",
          nameTextStyle: {
            color: "#A4A7AA",
            fontSize: 12,
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: "rgba(127, 214, 255, .4)",
            },
          },
          min: 0,
          axisLabel: {
            show: true,
            color: "#A4A7AA",
            fontSize: 12,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              width: 0.8,
              color: "rgba(127, 214, 255, .4)",
              type: "dashed",
            },
          },
        },
        series: [
          {
            name: "产值",
            type: "bar",
            barWidth: 2,
            tooltip: {
              show: false,
            },
            label: {
              show: false,
              position: "top",
              color: "#fff",
            },
            itemStyle: {
              color: "#1cfffb",
              lineStyle: {
                width: 1,
                type: "solid", //'dotted'虚线 'solid'实线
              },
            },
            data: this.ht,
          },
          {
            name: "合同数量",
            type: "line",
            showSymbol: false,
            smooth: true,
            // symbolSize: 8,
            lineStyle: {
              normal: {
                color: "#00FFF6",
              },
            },
            itemStyle: {
              color: "#00FFF6",
              borderColor: "#00FFF6",
              borderWidth: 2,
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(9, 69, 123, 1)",
                    },
                    {
                      offset: 1,
                      color: "rgba(1, 84, 161, 0)",
                    },
                  ],
                  false
                ),
              },
            },
            data: this.ht, //data.values
          },
        ],
      };
      this.option && this.chart.setOption(this.option);
    },
    // 点击按钮调用方法
    setHighBar(val) {
      let indexList = [];
      this.option.series[0].data.forEach(function (item, index) {
        // 取消高亮
        item.itemStyle = {
          color: "#1cfffb", // 重置为初始颜色
        };
      });
      if (val == "一月") {
        // this.highlightBar(0, 0);// 高亮某一个
        indexList = [0, 2];
        this.highlightSeries(indexList);
      } else if (val == "二月") {
        // this.highlightBar(0, 1);// 高亮某一个
        indexList = [4, 5, 6];
        this.highlightSeries(indexList);
      } else if (val == "三月") {
        // this.highlightBar(0, 2);// 高亮某一个
        indexList = [0, 1, 2];
        this.highlightSeries(indexList);
      } else if (val == "四月") {
        // this.highlightBar(0, 3);// 高亮某一个
        indexList = [1, 7, 8];
        this.highlightSeries(indexList);
      }
    },
    // 根据点击的按钮传参,设置高亮数据
    highlightSeries(indexList) {
      indexList.forEach((ele) => {
        this.option.series[0].data[ele].itemStyle = { color: "#f0f029" }; // 设置高亮颜色
      });

      this.chart.setOption(this.option);
    },
    // 该方法每次只能高亮一条数据
    highlightBar(seriesIndex, dataIndex) {
      this.chart = echarts.init(this.$refs.myCustomChart);
      // 设置特定柱子的颜色
      this.chart.setOption({
        series: [
          {
            type: "bar",
            barWidth: 2,
            data: this.ht, // 这里使用初始数据
            itemStyle: {
              color: function (params) {
                // 如果是特定的数据索引,则高亮显示
                if (params.dataIndex === dataIndex) {
                  return "#f0f029"; // 可以自定义高亮颜色
                } else {
                  return "#1cfffb"; // 其他柱子的颜色
                }
              },
            },
          },
        ],
      });
    },
  },
}
</script>
<style lang="scss" scoped>
.box {
  display: flex;
  width: 100%;
  height: 600px;
  .leftBox {
    width: 70%;
    height: 600px;
  }
  .rightBox {
    width: 30%;
    height: 600px;
  }
}
</style>

诸位大可一试!

ECharts ,可以通过绑定 `click` 事件来获取点击事件的信息,包括点击的坐标、数据等。如果要判断点击的是哪根柱子,可以通过以下步骤实现: 1. 在 `option` 设置 `series` 的 `label` 属性,以显示每根柱子的数值。 2. 在 `option` 设置 `series` 的 `emphasis` 属性,以高亮点击柱子。 3. 在 `chart` 的 `on` 方法监听 `click` 事件,并获取点击的坐标。 4. 使用 `chart` 的 `convertFromPixel` 方法将坐标转换为对应的数据点。 5. 遍历数据点,判断哪个数据点被点击,并对其进行相应的操作,比如高亮、提示等。 以下是一个示例代码,假设需要对柱状图的第一列进行点击事件的监听: ```javascript myChart.on('click', function (params) { // 获取点击的坐标 const { offsetX, offsetY } = params.event; // 将坐标转换为数据点 const pointInGrid = myChart.convertFromPixel({ gridIndex: 0 }, [offsetX, offsetY]); // 遍历数据点,判断哪个数据点被点击 const data = myChart.getOption().series[0].data; data.forEach((item, index) => { if (item[0] === pointInGrid[0]) { // 对被点击柱子进行操作,比如高亮、提示等 myChart.dispatchAction({ type: 'emphasis', seriesIndex: 0, dataIndex: index }); } }) }); ``` 需要注意的是,以上代码仅适用于 `bar` 类型的柱状图,如果是其他类型的图表,比如 `line`、`scatter` 等,需要根据具体情况进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值