echarts实现折线图上下限区域展示

效果图:
在这里插入图片描述

画这种区域中主要是利用了series的两个属性:
思路:上下限都画上区域,然后折线利用图形透明度隐藏,给上区域加上颜色,给下区域加上白色(echarts图背景是啥色,就加啥色)

    lineStyle: {
      opacity: 0
    }, // 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形
    areaStyle: {
      color: 'rgba(0,122,255,0.1)'
    }, // 填充的颜色

我的需求是最大天数、最小天数、平均天数,echarts图背景是白色的
数据形式:
在这里插入图片描述

核心代码如下:(核心是操作了series)

        let _series = [];
        const dataDict = {
          max: {
            name: '最大天数',
            color: '#ff5e3a',
            value: [],
            unit: '天'
          },
          min: {
            name: '最小天数',
            color: '#58cbb2',
            value: [],
            unit: '天'
          },
          avg: {
            name: '平均天数',
            color: '#007aff',
            value: [],
            unit: '天'
          }
        };
        for (let item in dataDict) {
          let _dict = dataDict[item];
          _legend.push(_dict.name);
          let dataVal = lineObj[item];
          if (item === 'max') {
            let _sery = {
              name: _dict.name,
              unit: _dict.unit,
              type: 'line',
              smooth: true,
              symbol: 'none',
              itemStyle: {
                normal: {
                  color: _dict.color
                }
              },
              lineStyle: {
                opacity: 0
              }, // 隐藏线
              areaStyle: {
                color: 'rgba(0,122,255,0.1)'
              }, // 上限区域
              data: dataVal
            };
            _series.push(_sery);
          } else if (item === 'min') {
            let _sery = {
              name: _dict.name,
              unit: _dict.unit,
              type: 'line',
              smooth: true,
              symbol: 'none',
              itemStyle: {
                normal: {
                  color: _dict.color
                }
              },
              lineStyle: {
                opacity: 0
              }, //隐藏线
              areaStyle: {
                color: '#fff'
              },// 下限区域
              data: dataVal
            };
            _series.push(_sery);
          } else {
            let _sery = {
              name: _dict.name,
              unit: _dict.unit,
              type: 'line',
              smooth: true,
              symbol: 'circle',
              showSymbol: false,
              itemStyle: {
                normal: {
                  color: _dict.color
                }
              },
              data: dataVal
            };
            _series.push(_sery);
          }
        }

        this.generateChart(this.chartMeteo, _legend, _xAxis, _yAxis, _series);


    generateChart(chart, _legend, _xAxis, _yAxis, _series) {
      let self = this;
      let option = {
        title: {
          x: 'left'
        },
        tooltip: {
          trigger: 'axis',
          // 坐标轴指示器
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: 'rgba(0,122,255,0.6)'
            }
          },
          backgroundColor: 'rgba(73,91,127,1)',
          formatter(a, b, c) {
            let series = self.chartMeteo.getOption().series;
            let tip = a[0].axisValue + '<br>';
            for (let i = 0; i < a.length; i++) {
              let seriesIndex = a[i].seriesIndex;
              let val =
                a[i].value !== 'NaN'
                  ? a[i].value.toFixed(1) + series[seriesIndex].unit
                  : '-';
              tip = tip + a[i].seriesName + ':' + val + '<br>';
            }
            return tip;
          }
        },
        legend: {
          show: false // 不显示legend
          // data: _legend,
          // x: 'center',
          // textStyle: {
          //   color: '#000'
          // }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: _xAxis,
          axisLabel: {
            color: '#AFBCD1'
          },
          axisLine: {
            onZero: false,
            lineStyle: {
              color: '#5F7190'
            }
          },
          // 指示器设置
          axisPointer: {
            lineStyle: {
              color: '#7D9DBB',
              type: 'dashed'
            }
          }
        },
        yAxis: _yAxis,
        series: _series
      };

      chart.setOption(option);
    }

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中使用echarts实现折线图,你需要按照以下步骤进行操作: 1. 首先,安装echarts库。你可以使用npm或yarn来安装echarts,具体命令如下: ``` npm install echarts ``` 或 ``` yarn add echarts ``` 2. 在Vue组件中引入echarts库。你可以在组件的`<script>`标签中使用`import`语句引入echarts库,如下所示: ```javascript import echarts from 'echarts'; ``` 3. 在Vue组件的模板中准备一个具备大小的容器div,用于显示折线图。你可以给这个div设置一个id,如`main`,并设置宽度和高度,如下所示: ```html <template> <div id="app"> <!--为echarts准备一个具备大小的容器dom--> <div id="main" style="width: 600px; height: 400px"></div> </div> </template> ``` 4. 在Vue组件的`mounted`钩子函数中编写代码来绘制折线图。你可以使用`echarts.init`方法初始化一个echarts实例,并传入容器的id,如`main`。然后,使用`setOption`方法设置折线图的配置项,如x轴、y轴、数据等,如下所示: ```javascript export default { name: 'YourComponentName', mounted() { this.$nextTick(function () { let charts = echarts.init(document.getElementById('main')); charts.setOption({ // 设置折线图的配置项 // ... }); }); }, }; ``` 通过以上步骤,你就可以在Vue中使用echarts实现折线图了。记得根据你的需求修改折线图的配置项,以及在`setOption`方法中设置相应的数据。 #### 引用[.reference_title] - *1* *3* [vue中如何使用echarts——以折线图为例](https://blog.csdn.net/weixin_61032994/article/details/124023735)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [在Vue中使用Echarts](https://blog.csdn.net/qq_36538012/article/details/109571270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值