echarts实现折线图颜色分层区分不同程度

本文详细描述了如何使用ECharts库创建一个折线图,通过计算标准值与最大值的比例,实现根据数值的分层效果,包括警戒线标记和颜色区域的渐变分区。
摘要由CSDN通过智能技术生成

在这里插入图片描述

echarts实现折线图根据数值进行分层

这里为了方便展示我把x轴和y轴都进行了隐藏,展示出来的效果就是如图所示。
接下来我们开始完成图标的形成:

我们需要准备一组数据,并且提前获取我们的标准值,比如图上的20
根据标准值/最大值,我们可以计算出标准值应该存在图表的位置

	  var warnLine = 20; // 标准值
      var maxVal = Math.max.apply(
        null,
        [20, 26, 22, 30, 25, 28, 14, 24, 16, 28, 18]
      ); // 最大值
      var warnProp = warnLine / maxVal; //标准值对应的位置百分比

先完成正常的折线图绘制代码,然后我们需要在series上加上markLine的配置,这个配置就是标准线的配置

 markLine: {
    silent: true,
    symbol: "none",
    label: {
      position: "end",
      formatter: "20%", //这里的数值可以写成动态的
      color: "#EA2626",
    },
    data: [
      {
        silent: false,
        lineStyle: {
          type: "dashed",
          color: "#EA2626",
        },
        yAxis: warnLine, //警戒线在y轴上的坐标
      },
    ],
  },

然后我们就可以进行分区了,重点是使用了areaStyle的配置,根据我们上面计算的数值进行颜色的配置达成分区的效果

areaStyle: {
    normal: {
      color: {
        type: "linear",
        x: 0,
        y: 1,
        x2: 0,
        y2: 0,
        colorStops: [
          {
            offset: 0,
            color: "rgba(52, 121, 236, 0)",
          },
          {
            offset: warnProp / 3,
            color: "rgba(52, 121, 236, 0.2)",
          },
          {
            offset: warnProp,
            color: "rgba(52, 121, 236, 0.8)",
          },
          {
            offset: warnProp,
            color: "rgba(255, 92, 92, 0.6)",
          },
          {
            offset: warnProp + (1 - warnProp) / 3,
            color: "rgba(255, 92, 92, 0.9)",
          },
          {
            offset: 1,
            color: "rgba(255, 92, 92, 1)",
          },
        ],
        global: false,
      },
    },
  },

上面对应的offset也可以根据自己的需要进行修改,最终达成自己想要的效果。

原理就是以上这样,话不多说,源码奉上:

humidityEcharts() {
      var warnLine = 20;
      var maxVal = Math.max.apply(
        null,
        [20, 26, 22, 30, 25, 28, 14, 24, 16, 28, 18]
      );
      var warnProp = warnLine / maxVal;
      this.rightCnetrEcharts = echarts.init(
        document.getElementById("right_center_echarts")
      );
      document
        .getElementById("right_center_echarts")
        .removeAttribute("_echarts_instance_");
      let option = {
        legend: {
          show: false,
        },
        tooltip: {
          show: false,
          trigger: "axis",
        },
        grid: {
          left: 0,
          right: "10%",
          bottom: 0,
          top: 0,
        },
        xAxis: {
          type: "category",
          axisLine: {
            show: false,
          },
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          data: [1, 2, 3, 4, 5, 6, 8, 9, 10, 11],
        },
        yAxis: [
          {
            type: "value",
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              show: false,
            },
          },
        ],
        series: [
          {
            data: [20, 26, 22, 30, 25, 28, 14, 24, 16, 28, 18],
            type: "line",
            markLine: {
              silent: true,
              symbol: "none",
              label: {
                position: "end",
                formatter: "20%",
                color: "#EA2626",
              },
              data: [
                {
                  silent: false,
                  lineStyle: {
                    type: "dashed",
                    color: "#EA2626",
                  },
                  yAxis: warnLine, //警戒线在y轴上的坐标
                },
              ],
            },
            symbolSize: false,
            smooth: true,
            lineStyle: {
              color: "transparent",
              width: 1,
            },
            itemStyle: {
              color: "#EA2626",
              borderColor: "#fff",
              borderWidth: 0,
            },
            connectNulls: true,
            symbol: "circle",
            yAxisIndex: 0,
            areaStyle: {
              normal: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 1,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: "rgba(52, 121, 236, 0)",
                    },
                    {
                      offset: warnProp / 3,
                      color: "rgba(52, 121, 236, 0.2)",
                    },
                    {
                      offset: warnProp,
                      color: "rgba(52, 121, 236, 0.8)",
                    },
                    {
                      offset: warnProp,
                      color: "rgba(255, 92, 92, 0.6)",
                    },
                    {
                      offset: warnProp + (1 - warnProp) / 3,
                      color: "rgba(255, 92, 92, 0.9)",
                    },
                    {
                      offset: 1,
                      color: "rgba(255, 92, 92, 1)",
                    },
                  ],
                  global: false,
                },
              },
            },
          },
        ],
      };
      option && this.rightCnetrEcharts.setOption(option, true);
    },
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坐在办公室的咸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值