echarts折线图矩形选择 框选图表

html 复制代码
 <div
      id="performaceLineChart"
      ref="performaceLineChartRef"
      style="width: 100%; height: 500px"
    ></div>
javascript 复制代码
 initLineChart() {
      var chartDom = document.getElementById("performaceLineChart");
      this.chart = echarts.init(chartDom);
      let that = this
      let option = {
        color: [],
        toolbox: {
          top: "0",
          right: "10",
          itemSize: 18,
          iconStyle: {
            borderColor: "#aeaeae",
            borderWidth: 2,
          },
          feature: {
            brush: {
              type: ['rect'], // ['rect','polygon','lineX', 'lineY','keep','clear'],
              title: {
                rect: '矩形选择',
              },
              xAxisIndex: 0,
            },
            saveAsImage: {
              title: "导出图片",
              name: `${this.basicInfoData.shortName}-业绩表现(${this.selectLabel})`
            },
            // dataZoom: [{  // 区域缩放
            //   type: 'inside',
            //   start: 0,
            //   end: 100,
            // }],
            // restore: {},  // 还原
          },
        },
        brush: {
          toolbox: ['rect'], // ['rect','polygon','lineX', 'lineY','keep','clear'],
          xAxisIndex: 0,
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: 'rgba( 0, 0, 0,0.7)',
          borderColor: 'rgba( 0, 0, 0,0.7)',
          formatter: function (params) {
            var str = params[0].name + "</br>";
            for (let item of params) {
              str =`
              <span style='color: #fff;'>${str}</span>
              ${item.seriesName === '单位净值' ? '</br>' : ''} 
              <div style='display:flex;justify-content:space-between;'>
                <div style='display:flex;'>
                  <div>${item.marker}</div>
                  <div style='color: #fff;width: 200px;white-space: normal;'>${item.seriesName}</div>
                </div>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <div style='color: #fff;'>${item.value}${item.seriesName !== '单位净值' ? '%' : ''}</div>
              </div>`;
            }
            return str;
          },
        },
        legend: {
          x: "center",
          y: "bottom",
          icon: "circle",
          textStyle: {},
          data: this.lengengData,
        },
        grid: {
          left: "0.5%",
          right: "1%",
          bottom: "15%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLabel: {},
          axisLine: {
            show: true,
            lineStyle: {},
            onZero: false, // X轴线始终位于底部
          },
          splitLine: {
            show: false,
          },
          data: this.performaceLineChartData.dataDates,
        },
        yAxis:[
          {
            type: "value",
            scale: true,
            splitNumber: 5,
            alignTicks: true,
            axisLabel: {
              show: true,
              formatter: (value) => {
                return `${value.toFixed(1)}%`
              }
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dotted",
              },
            },
          },
          {
            type: "value",
            splitNumber: 5,
            axisLabel: {
              show: true,
              formatter: (value) => {
                return `${value}%`
              }
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dotted",
              },
            },
          },
          {
            type: "value",
            splitNumber: 5,
            alignTicks: true,
            axisLabel: {
              show: true,
              formatter: (value) => {
                return `${value.toFixed(1)}%`
              }
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dotted",
              },
            },
          },
          {
            type: "value",
            splitNumber: 5,
            axisLabel: {
              show: true,
              formatter: (value) => {
                return `${value}%`
              }
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dotted",
              },
            },
          },
        ],
        series: this.seriesData,
      }
      this.chart.setOption(option, true);

      this.chart.on('brushEnd', function (params) {
        // console.log(params,'brushEnd');
        if (params.areas && params.areas.length > 0) {
          let xData = params.areas[0].coordRange[0]
          // 拿到框选的开始日期和结束日期
          that.selectStartDate = that.performaceLineChartData.dataDates[xData[0]]
          that.selectEndDate = that.performaceLineChartData.dataDates[xData[1]]
          if (that.selectStartDate && that.selectEndDate) {
            let params1 = {
              fundCode: that.fundCode,
              dataDate: that.dateValue,
              displayType: '0', // displayType 固定传 '0'
              indexCode: that.flag ? '' : '0', // indexCode 默认传 '',切换超额收益后 indexCode 传 '0'
              startDate: that.selectStartDate, // 框选的开始日期
              endDate: that.selectEndDate, // 框选的结束日期
            }
            // 获取框选日期区间数据
            getPortfolioNameApi.getCustomize(params1).then((res) => {
              if(res.code === 200) {
                that.selectIntervalData = res.data
                // 区间数据框处于图表中间位置
                let defaultDialog = document.querySelector('#intervalStyle')
                let chartWidth = that.$refs.performaceLineChartRef.offsetWidth
                defaultDialog.style.left = chartWidth / 2 - 182 + 'px'
                defaultDialog.style.top = 40 + 'px'
                that.intervalVisible = true
                // that.chart.setOption(option,true); // 手动清除框选状态
              }
            })
          }
        }
      });
    },

关键代码是:

javascript 复制代码
 brush: {
    toolbox: ['rect'], // ['rect','polygon','lineX', 'lineY','keep','clear'],
    xAxisIndex: 0,
 },
 this.chart.on('brushEnd', function (params) {
        // console.log(params,'brushEnd');
        if (params.areas && params.areas.length > 0) {
          let xData = params.areas[0].coordRange[0]
          // 拿到框选的开始日期和结束日期
          that.selectStartDate = that.performaceLineChartData.dataDates[xData[0]]
          that.selectEndDate = that.performaceLineChartData.dataDates[xData[1]]
          if (that.selectStartDate && that.selectEndDate) {
            let params1 = {
              fundCode: that.fundCode,
              dataDate: that.dateValue,
              displayType: '0', // displayType 固定传 '0'
              indexCode: that.flag ? '' : '0', // indexCode 默认传 '',切换超额收益后 indexCode 传 '0'
              startDate: that.selectStartDate, // 框选的开始日期
              endDate: that.selectEndDate, // 框选的结束日期
            }
            // 获取框选日期区间数据
            getPortfolioNameApi.getCustomize(params1).then((res) => {
              if(res.code === 200) {
                that.selectIntervalData = res.data
                // 区间数据框处于图表中间位置
                let defaultDialog = document.querySelector('#intervalStyle')
                let chartWidth = that.$refs.performaceLineChartRef.offsetWidth
                defaultDialog.style.left = chartWidth / 2 - 182 + 'px'
                defaultDialog.style.top = 40 + 'px'
                that.intervalVisible = true
                // that.chart.setOption(option,true); // 手动清除框选状态
              }
            })
          }
        }
      });
 
相关推荐
随风一样自由4 小时前
【前端独角兽】刚进入前端领域的前端开发用jQuery还是Vue3?
前端·javascript·vue3·jquery
IMPYLH5 小时前
HTML 的 <data> 元素
前端·html
YHHLAI5 小时前
[特殊字符] 面试中的 Promise —— 从入门到精通
前端·javascript·面试
weixin_BYSJ19875 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php
沪飘大军6 小时前
ll-llm:一个零依赖、可插拔的 OpenAI 兼容 LLM 代理
javascript
kyriewen7 小时前
我扒了 GPT-5.6 的全部编程跑分——有一项 OpenAI 没敢放出来
前端·gpt·ai编程
前端H8 小时前
微前端 v3 架构升级:从加载隔离到状态协同
前端·架构·状态模式
山河木马8 小时前
GPU自动处理专题3-NDC怎么映射成屏幕像素坐标(视口变换)
javascript·webgl·图形学
陆枫Larry9 小时前
小程序包体积优化:用 SVG 图片替换 iconfont,并保留 CSS 控色能力
前端
Appthing9 小时前
在 TanStack Start 中使用 VitePWA 的正确配置
javascript