v-charts实现多图联动

图表实现 专栏收录该内容
1 篇文章 0 订阅

v-charts实现多图联动

当需要显示多图联动的图表时,使用v-chart时犹如使用Echarts。v-charts官方说明比较简单,直接说绑定xx属性这样甩掉一大堆工作,弄得需要去看Echarts官方文档
下面是多图联动的举例:

    <ve-line
      :x-axis="xAxis"
      :y-axis="yAxis"
      :extend="extend"
      :not-set-unchange="['dataZoom']"
      :legend="legend"
      :height="chartHeight + 'px'">
      <div
        v-if="isEmpty"
        class="data-empty">{{ emptyTips|l }}</div>
    </ve-line>

属性定义:

      extend: {
        series: [],
        grid: [],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          formatter: function(params) {
            var tip = '时间:' + params[0].value[0]
            params.forEach(param => {
              tip += '<br/>' + param.marker + param.seriesName + ':' + Number(param.value[1]).toFixed(4)
            })
            return tip
          }
        },
        axisPointer: {
          link: { xAxisIndex: 'all' },
          label: {
            backgroundColor: '#777'
          }
        },
        toolbox: {
          right: 0,
          feature: {
            dataZoom: {
              yAxisIndex: false
            }
          }
        }
      }
......
///以下为 曲线初始化
    initChartOptions() {
      const whiteHeight = this.whiteHeight
      const singleHeight = this.singleHeight
      let seriesOptions = []
      let gridOptions = []
      let legendOptions = []
      let xAxisOptions = []
      let yAxisOptions = []

      this.dataset.forEach((fields, gridIndex) => {
        let startYAxisIndex = yAxisOptions.length
        let yAxisNames = []
        fields.forEach(field => {
          // yAxis name
          if (!yAxisNames.includes(field.axisName)) {
            yAxisNames.push(field.axisName)
          }
        })

        let legendItemData = []
        fields.forEach(field => {
          // series
          seriesOptions.push({
            key: field.key,
            name: field.name,
            type: field.type || 'line',
            gridIndex: gridIndex,
            xAxisIndex: gridIndex,
            yAxisIndex: startYAxisIndex + yAxisNames.indexOf(field.axisName),
            data: [],
            showSymbol: false,
            hoverAnimation: false,
            smooth: false, // 不平滑曲线
            // smoothMonotone: 'x' // 如果要平滑,请启用此属性,避免出现时间的不单调性
            markPoint: field.markPoint || {} // 最大值最小值的配置
          })
          // legend
          legendItemData.push(field.name)
        })
        let legendItem = {
          data: legendItemData,
          top: gridIndex * (singleHeight + whiteHeight)
        }
        legendOptions.push(legendItem)

        // grid
        gridOptions.push({
          left: 60,
          right: 60,
          top: gridIndex * (singleHeight + whiteHeight) + whiteHeight,
          height: singleHeight - whiteHeight,
          containLabel: false
        })
        // xAxis
        xAxisOptions.push({
          type: 'time',
          gridIndex: gridIndex,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#999'
            }
          },
          splitLine: {
            show: true
          }
        })
        // yAxis
        yAxisNames.forEach(axisName => {
          yAxisOptions.push({
            type: 'value',
            name: axisName,
            scale: true, // 是否是脱离 0 值比例。设置成 true 后坐标刻度不会强制包含零刻度。
            gridIndex: gridIndex,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#999'
              }
            },
            splitLine: {
              show: false
            }
          })
        })
      })

      this.extend.series = seriesOptions
      this.extend.grid = gridOptions
      this.legend = legendOptions
      this.xAxis = xAxisOptions
      this.yAxis = yAxisOptions
      // chart height
      this.chartHeight = (singleHeight + whiteHeight) * this.dataset.length
    }

呈现出的效果如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值