dataZoom 显示近7天的数据

如果返回的数据有包含今天的就显示近7天的 

 

如果返回的日期不包含今天就显示数组最后一个往前7天的数据

 

 // 初始化曲线图
    getDetailInfoList() {
      let that = this
//==============================判断横坐标
      let dataZoomStart = 0
      let dataZoomEnd = 0
      for (let index = 0; index < this.xData.length; index++) {
        const element = this.xData[index];
        console.log(this.$moment(element).format('YYYY-MM-DD'), this.$moment(new Date()).format('YYYY-MM-DD'));
        console.log(this.xData.includes(this.$moment(new Date()).format('YYYY-MM-DD')));
        if (this.xData.includes(this.$moment(new Date()).format('YYYY-MM-DD')) && (this.$moment(element).format('YYYY-MM-DD') == this.$moment(new Date()).format('YYYY-MM-DD'))) {
          dataZoomEnd = index
          if (index - 6 > 0 || index - 6 == 0) {
            dataZoomStart = index - 6
          }
        }
        if (!this.xData.includes(this.$moment(new Date()).format('YYYY-MM-DD'))) {
          dataZoomEnd = this.xData.length - 1
          if (this.xData.length - 7 > 0 || this.xData.length - 7 == 0) {
            dataZoomStart = this.xData.length - 7
          }
        }
      }
//==============判断横坐标
      let option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["平均值", "最大值", "最小值"],
          top: 20,
        },
//==============滚动条赋值
        dataZoom: [
          {
            type: 'slider',
            startValue: dataZoomStart,
            endValue: dataZoomEnd
          },
          // {
          //   start: 0,
          //   end: dataZoomEnd
          // }
        ],
//==============滚动条赋值
        description: {
          visible: true,
          text: "",
        },
        xAxis: [
          {
            type: "category",
            data: this.xData,
            splitLine: {
              //不显示分割线
              show: false,
            },
            axisLabel: {
              interval: 0,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: this.yName,
            splitLine: {
              //显示分割线
              show: true,
            },
          },
        ],
        series: [
          {
            name: "平均值",
            type: "line",
            data: this.avgData,
            smooth: true,
            emphasis: {
              focus: "series",
            },
            itemStyle: {
              normal: {
                color: "#91cc75",
              },
            },
            markLine: {
              silent: true,
              symbol: ["none", "arrow"],
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: "#ff0000",
                  },
                },
              },
              label: {
                position: "end",
                color: "#8c8c8c",
                formatter: "{b}:{c}",
              },
              data: this.yaxisVOList
            },
            tooltip: {
              valueFormatter: function (value) {
                return value + that.yUnit
              }
            },
          },
          {
            name: "最大值",
            type: "line",
            data: this.maxData,
            smooth: true,
            emphasis: {
              focus: "series",
            },
            itemStyle: {
              normal: {
                color: "#ee6666",
              },
            },
            tooltip: {
              valueFormatter: function (value) {
                return value + that.yUnit
              }
            },
            // markLine: {
            //   silent: true,
            //   symbol: ["none", "arrow"],
            //   itemStyle: {
            //     normal: {
            //       lineStyle: {
            //         color: "#ff0000",
            //       },
            //     },
            //   },
            //   label: {
            //     position: "end",
            //     color: "#8c8c8c",
            //     formatter: "{b}:{c}",
            //   },
            //   data: [
            //     {
            //       name: "最大警戒值",
            //       yAxis: 4,
            //     },
            //     {
            //       name: "最小警戒值",
            //       yAxis: 1,
            //     },
            //   ],
            // },
          },
          {
            name: "最小值",
            type: "line",
            data: this.minData,
            smooth: true,
            emphasis: {
              focus: "series",
            },
            itemStyle: {
              normal: {
                color: "#fac858",
              },
            },
            tooltip: {
              valueFormatter: function (value) {
                return value + that.yUnit
              }
            },
            // markLine: {
            //   silent: true,
            //   symbol: ["none", "arrow"],
            //   itemStyle: {
            //     normal: {
            //       lineStyle: {
            //         color: "#ff0000",
            //       },
            //     },
            //   },
            //   label: {
            //     position: "end",
            //     color: "#8c8c8c",
            //     formatter: "{b}:{c}",
            //   },
            //   data: [
            // {
            //   name: "最大警戒值",
            //   yAxis: 4,
            // },
            // {
            //   name: "最小警戒值",
            //   yAxis: 1,
            // },
            //   ],
            // },
          },

        ],
      }
      this.lineCharts.setOption(option);
      this.lineCharts.off('click')
      this.lineCharts.on("click", 'series.line', function (data) {
        // 根据值获取点对应的列表数据
        if (data) {
          if (that.form.gas_data) {
            let dataObject = {
              gas_data: that.form.gas_data,
              sn: that.form.uniqueid,
              day: data.name,
              peak: data.value
            }
            getDetail(dataObject).then(res => {
              that.data = res.data.data
              that.dialogVisible = true
            })
          } else {
            that.$confirm("请先选择“分析内容”后再点击查询详情!", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(() => {
              })
              .then(() => {
              });
          }

        }

      });
    },

 

要在 ECharts 中使用 dataZoom显示6条数据,你可以使用 dataZoom 的 start 和 end 属性来控制可视化的数据范围。以下是一个示例代码: ```javascript // 假设你已经创建了一个 ECharts 实例并且有一个包含所有数据数据数组 var myChart = echarts.init(document.getElementById('my-chart')); // 替换为你的 ECharts 实例 var data = [/* your data array */]; // 配置 ECharts 图表的 option var option = { xAxis: { type: 'category', data: data.map(function (item) { return item.name; }) }, yAxis: { type: 'value' }, series: [{ type: 'line', data: data.map(function (item) { return item.value; }) }], dataZoom: [{ type: 'slider', start: 0, end: 5, // 只显示前6条数据,索引从0开始计数 showDetail: false // 可选,是否显示详细信息 }] }; // 将配置应用于图表 myChart.setOption(option); ``` 这段代码首先创建了一个 ECharts 实例,并假设你有一个包含所有数据数据数组。然后,配置了 xAxis(横轴)和 yAxis(纵轴),以及一个 line 类型的 series(系列)来绘制折线图。接下来,在 option 中添加了一个 dataZoom 配置项,并设置其 start 和 end 属性,以控制可视化的数据范围。在这个例子中,我们将 start 设置为 0,end 设置为 5,这样只显示前 6 条数据(索引从 0 开始计数)。 最后,将配置应用于图表,通过调用 `myChart.setOption(option)` 来渲染图表。请注意,你需要将代码中的 `my-chart` 替换为你的 ECharts 实例的容器元素的 ID,并根据你的数据结构和需求进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值