vue里echarts实现折线图tooltip自定义样式

效果

在这里插入图片描述

实现

先编写父组件:

<stacked-line-chart :tooltipData="tooltipData" :monthNameData="monthNameData" :allApplyCountData="allApplyCountData"
  :hallApplyCountData="hallApplyCountData" :netApplyCountData="netApplyCountData"
></stacked-line-chart>
<script>
export default {
	data () {
    return {
      tooltipData: ['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-09', '2020-10', '2020-12', '2021-01', '2021-02'],
      monthNameData: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '9月', '10月', '12月', '1月', '2月'], // x轴月份数据
      allApplyCountData: [120, 132, 101, 134, 90, 230, 210, 120, 132, 101, 134, 90], // 总申办量
      hallApplyCountData: [320, 332, 301, 334, 390, 330, 320, 320, 332, 301, 334, 390], // 实体大厅申办量
      netApplyCountData: [820, 932, 901, 934, 1290, 1330, 1320, 820, 932, 901, 934, 1290], // 网上大厅申办量
    };
  },
}
</script>

在编写StackedLineChart组件

<template>
  <div ref="stackedLineChart" class="stacked-line-chart-box"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'StackedLineChart',
  props: {
    // 用于提示
    tooltipData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // x轴月份数据
    monthNameData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // 总申办量
    allApplyCountData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // 实体大厅申办量
    hallApplyCountData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // 网上大厅申办量
    netApplyCountData: {
      type: Array,
      default: () => {
        return [];
      }
    },
  },
  data () {
    return {
      option: {
        tooltip: {
          trigger: 'axis',
          padding: [8, 10, 8, 10],
          axisPointer: {
            type: 'line',
            lineStyle: {
              color: 'rgba(0, 0, 0, 0.65)'
            },
          },
          // 自定义提示框的内容
          formatter: (params) => {
            let result = this.tooltipData[params[0].dataIndex] + "</br>";
            params.forEach(el => {
              result += `${this.markDot(el.color)}${el.seriesName}${el.data}</br>`
            })
            return result;
          }
        },
        color: ['#9A47FF', '#1890FF', '#2FC25B'],
        legend: {
          data: ['总申办量', '实体大厅申办量', '网上大厅申办量'],
          left: 'left',
          icon: 'circle',
          padding: 0,
          itemGap: 24,
          itemWidth: 6,
          itemHeight: 6,
          textStyle: {
            color: '#333',
            padding: [2, 0, 0, 0],
            lineHeight: 20
          }
        },
        grid: {
          left: '0',
          right: '2%',
          bottom: '0',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: [],
          axisLine: {
            lineStyle: {
              color: '#E9E9E9'
            }
          },
          axisTick: {
            lineStyle: {
              color: '#E9E9E9'
            }
          },
          axisLabel: {
            color: '#777777'
          }
        },
        yAxis: {
          type: 'value',
          nameTextStyle: {
            color: 'red'
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#777777'
          },
          splitLine: {
            lineStyle: {
              color: '#E9E9E9',
              type: 'dashed'
            }
          }
        },
        series: [
          {
            name: '总申办量',
            zlevel: 1,
            type: 'line',
            stack: '总量',
            symbol: 'circle',
            symbolSize: [2, 2],
            showSymbol: false,
            itemStyle: {
              borderWidth: 1,
              borderColor: '#fff'
            },
            data: []
          },{
            name: '实体大厅申办量',
            zlevel: 1,
            type: 'line',
            stack: '总量',
            symbol: 'circle',
            symbolSize: [2, 2],
            showSymbol: false,
            itemStyle: {
              borderWidth: 1,
              borderColor: '#fff'
            },
            data: []
          },{
            name: '网上大厅申办量',
            zlevel: 1,
            type: 'line',
            stack: '总量',
            symbol: 'circle',
            symbolSize: [2, 2],
            showSymbol: false,
            itemStyle: {
              borderWidth: 1,
              borderColor: '#fff'
            },
            data: []
          }
        ],
      }
    };
  },
  mounted() {
    this.initRender();
  },
  methods: {
    // 初始化渲染
    initRender() {
      this.option.xAxis.data = this.monthNameData; // x轴月份数据
      this.option.series[0].data = this.allApplyCountData; // 总申办量
      this.option.series[1].data = this.hallApplyCountData; // 实体大厅申办量
      this.option.series[2].data = this.netApplyCountData; // 网上大厅申办量
      let chartDom = this.$refs.stackedLineChart;
      let myChart = echarts.init(chartDom);
      myChart.setOption(this.option);
    },
    // 生成大小一样样色不同的圆点
    markDot(color) {
      let domHtml = '<span style="' +
        'display: inline-block;' +
        'margin-right: 8px;' +
        'margin-bottom: 2px;' +
        'border-radius: 6px;' +
        'width: 6px;' +
        'height: 6px;' +
        `background-color: ${color}` +
      '"></span>'
      return domHtml;
    }
  },
};
</script>

<style lang="scss" scoped>
.stacked-line-chart-box {
  height: 388px;
}
</style>

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凯小默

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

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

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

打赏作者

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

抵扣说明:

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

余额充值