eCharts 多系列柱状图增加背景图

图例和下面的dataZoom自动轮播效果如图

c 复制代码
    getNLFXChart() {
      this.NLFXChart = this.$echarts.init(document.getElementById("NLFXChart"));
      let bardata1 = [6000, 6600, 5000, 3000, 8000];
      let bardata2 = [8000, 8000, 8000, 6000, 7000];
      let categoryData = getDateArrayFromMonthStart();

      // 计算 dataZoom 的窗口大小
      const zoomWindowSize = 12;
      const totalPoints = categoryData.length;
      const endPercent = (zoomWindowSize / totalPoints) * 100;
      let option = {
        grid: {
          top: "19%",
          left: "8%",
          right: "3%",
          bottom: "18%",
        },
        legend: {
          itemWidth: 18,
          itemHeight: 18,
          itemGap: 41,
          left: "center",
          top: "3%",
          padding: 0,
          textStyle: {
            color: "#fff",
            fontSize: 30,
            padding: [8, 0, 0, 6],
            fontFamily: "Source Han Sans CN",
          },
          data: [
            {
              name: "泥量",
              icon: "rect",
              itemStyle: { color: "#ffa24f", borderWidth: 0 },
            },
            {
              name: "污泥产率",
              icon: "rect",
              itemStyle: { color: "#cbffff", borderWidth: 0 },
            },
          ],
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: "transparent",
          textStyle: {
            color: "#D1DEEE",
            fontSize: 14,
            fontWeight: "bold",
            fontFamily: "PingFangSC, PingFang SC",
          },
          formatter: function (params) {
            // 过滤掉背景系列
            let filtered = params.filter((item) => item.seriesName !== "背景");
            const seen = new Map();
            filtered = filtered.filter((item) => {
              if (seen.has(item.seriesName)) return false;
              seen.set(item.seriesName, true);
              return true;
            });

            if (!filtered.length) return "";

            let result = "";
            filtered.forEach((item) => {
              result += `<div style="display:flex; justify-content:space-between; gap:20px;font-size:22px;margin-bottom:14px;">
          <span>${item.name}: ${item.seriesName}</span>
          <span style="font-weight:bold;">${item.value}</span>
        </div>`;
            });

            return result;
          },
        },
        xAxis: {
          type: "category",
          axisLabel: {
            textStyle: {
              color: "#fff",
              fontSize: 28,
              fontFamily: "Source Han Sans CN",
            },
            formatter: function (data) {
              return data;
            },
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#216CA0",
            },
            show: true,
          },
          data: categoryData,
          interval:
            categoryData.length > 12 ? 1 : categoryData.length > 20 ? 3 : 0,
        },
        yAxis: {
          name: "吨",
          nameTextStyle: {
            color: "#D1DEEE",
            fontSize: 24,
            padding: [0, 28, 12, 0],
            fontFamily: "Source Han Sans CN",
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(33, 108, 160, 0.4)",
              type: "dashed",
            },
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#D1DEEE",
            },
            fontSize: 24,
            fontFamily: "Source Han Sans CN",
            formatter: function (value) {
              if (value === 0) {
                return value;
              }
              return value;
            },
          },
          axisTick: {
            show: false,
          },
        },
        dataZoom: [
          {
            type: "slider",
            show: true,
            height: 10,
            xAxisIndex: [0],
            bottom: "0%",
            start: 0,
            end: endPercent,
            handleIcon:
              "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleSize: "110%",
            handleStyle: {
              color: "#d3dee5",
            },
            textStyle: {
              color: "#fff",
            },
            borderColor: "#90979c",
          },
          {
            type: "inside",
            show: true,
            height: 10,
            start: 0,
            end: endPercent,
          },
        ],
        series: [
          {
            type: "custom",
            itemStyle: {
              color: "#113660",
            },
            renderItem: function (params, api) {
              //获取对应类目的axisTick中心点坐标
              let start = api.coord([api.value(0)]);

              //通过坐标系的宽度和类目数,计算单个类目的背景
              let width = (params.coordSys.width / categoryData.length) * 1.3;

              return {
                type: "rect",
                shape: {
                  // 相对左上角坐标
                  x: start[0] - width / 2,
                  y: params.coordSys.y,
                  width: width,
                  height: params.coordSys.height,
                },
                style: api.style(),
              };
            },
            data: [0, 0, 0, 0, 0, 0, 0],
          },

          {
            name: "污泥产率",
            data: bardata2,
            type: "bar",
            barCategoryGap: "20%",
            barWidth: "10px",
            barGap: 0.5,
            z: 2,
            itemStyle: {
              color: {
                x: 0,
                y: 1,
                x2: 0,
                y2: 0,
                type: "linear",
                colorStops: [
                  { offset: 0, color: "rgba(251, 159, 79,0)" },
                  { offset: 1, color: "rgba(251, 159, 79,1)" },
                ],
              },
            },
            // showBackground: true,
            // backgroundStyle: {
            //   color: "#113660",
            // },
          },
          {
            name: "泥量",
            data: bardata1,
            type: "bar",
            barCategoryGap: "20%",
            barWidth: "10px",
            barGap: 0.5,
            z: 2,
            itemStyle: {
              color: {
                x: 0,
                y: 1,
                x2: 0,
                y2: 0,
                type: "linear",
                colorStops: [
                  { offset: 0, color: "rgba(196, 248, 249,0)" },
                  { offset: 1, color: "rgba(196, 248, 249,1)" },
                ],
              },
            },
            // showBackground: true,
            // backgroundStyle: {
            //   color: "#113660",
            // },
          },
        ],
      };

      // 先设置图表
      this.NLFXChart.setOption(option, true);

      // 自动轮播 + dataZoom 自动移动
      let len = 0;
      let intervalId = setInterval(() => {
        if (len >= categoryData.length) {
          len = 0;
          // 重置 dataZoom 到起始位置
          this.NLFXChart.dispatchAction({
            type: "dataZoom",
            dataZoomIndex: 0,
            start: 0,
            end: endPercent,
          });
        }

        // 显示 tooltip
        this.NLFXChart.dispatchAction({
          type: "showTip",
          seriesIndex: 1, // 注意:索引改为1,跳过背景系列
          dataIndex: len,
        });

        // 移动 dataZoom
        const currentIndex = len;
        const startPercent = Math.max(
          0,
          Math.min(
            100 - endPercent,
            ((currentIndex - zoomWindowSize / 2) / totalPoints) * 100
          )
        );

        this.NLFXChart.dispatchAction({
          type: "dataZoom",
          dataZoomIndex: 0,
          start: startPercent,
          end: startPercent + endPercent,
        });

        len++;
      }, 6000);

      this.autoPlayNLFXInterval = intervalId;
    },

记得销毁定时器

c 复制代码
  beforeDestroy() {
    if (this.autoPlayNLFXInterval) {
      clearInterval(this.autoPlayNLFXInterval);
    }
  },
相关推荐
Dxy12393102165 小时前
MySQL索引完整教程:创建、查看、修改、删除与日常管理
前端·javascript·mysql
端庄的战斗机6 小时前
javascript 设计模式(文章很长,请自备瓜子,水果和眼药水)
开发语言·javascript·设计模式
前端毕业班9 小时前
uni-app 小程序支持 teleport 了
前端·javascript·vue.js
东风破_9 小时前
LLM 为什么记不住你?无状态、messages 和上下文管理
javascript·人工智能
Hilaku10 小时前
前端架构师的克制:什么时候该坚决对新技术说不?
前端·javascript·程序员
古夕10 小时前
多应用同域部署下 Favicon 异常问题排查与治理
前端·javascript
逗逗豆巴吧11 小时前
WPS 加载项中实现当前文档直接上传
前端·javascript·ecmascript 6
妙码生花11 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(二十一):网络请求封装优化
前端·javascript·vue.js
用户114818678948411 小时前
普通Fetch、Fetch流式、Axios、SSE:谁才是流式输出的正确操作?
前端·javascript
谷哥的小弟11 小时前
TypeScript数组类型
javascript·typescript