【echarts】22、echarts+vue2 - 阴影折线图

这篇博客详细记录了如何基于Echarts5.x和Vue2框架实现一个告警趋势的图表组件。内容包括模板结构、数据初始化、图表创建、图例交互以及鼠标事件处理等,完整展示了从数据到可视化的全过程。图表包含线性图、图例、提示框、坐标轴等元素,并实现了动态高亮和图例切换功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

基于echarts5.x和vue2实现
记录以便日后查阅

实现效果

在这里插入图片描述

代码实现

<template>
  <div class="chart-wrap">
    <ul class="legend-list">
      <li
        v-for="(item, index) in legend"
        :key="index"
        :class="['legend', item.selected ? '': 'un-active']"
        @mouseenter="enterHandler(item)"
        @mouseleave="leaveHandler(item)"
        @click="clickHandler(item)"
      >
        <i class="rect" :style="{ backgroundColor: item.color }" />
        <span>{{ item.name }}</span>
      </li>
    </ul>
    <div id="chart19" class="chart" />
  </div>
</template>

<script>
export default {
  name: 'Index',
  data () {
    return {
      chart: null,
      legend: [{ name: '告警数量', color: '#54A6FD', selected: true }],
    }
  },
  mounted() {
    this.createChartHandler()
  },
  methods: {
    // 创建图表
    createChartHandler () {
      this.chart = this.$echarts.init(document.getElementById('chart19'))
      this.chart.setOption(this.getChartOption(this.legend))
      window.addEventListener('resize', () => {
        setTimeout(() => {
          this.chart.resize()
        })
      })
    },
    // 获取图表配置项
    getChartOption (legend) {
      return {
        legend: {
          show: false,
          data: legend.map(i => i.name)
        },
        textStyle: {
          fontFamily: 'PingFangReg'
        },
        title: {
          text: '告警趋势',
          textStyle: {
            color: '#fff',
            fontSize: 14,
            fontWeight: 400
          },
          padding: [0, 0, 0, 20]
        },
        grid: {
          top: '15%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          extraCssText:
            'color:#ffffff;background: #002C6C;border:none; box-shadow: 0px 0px 8px 1px rgba(0, 145, 255, 0.5);border-radius: 2px;',
          confine: true,
          textStyle: {
            // 提示框浮层的文本样式。
            fontSize: 12,
            color: '#ffffff'
          },
          formatter: '{a}: {c}'
        },
        dataset: {
          source: [
            ['product', '告警数量']
          ]
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: [
            { value: '5-10', textStyle: { align: 'left' }},
            { value: '5-11', textStyle: { align: 'center' }},
            { value: '5-12', textStyle: { align: 'center' }},
            { value: '5-13', textStyle: { align: 'center' }},
            { value: '5-14', textStyle: { align: 'center' }},
            { value: '5-15', textStyle: { align: 'center' }},
            { value: '5-16', textStyle: { align: 'right' }}
          ],
          axisTick: {
            show: false
          },
          axisLine: { // 这是x轴线颜色
            lineStyle: {
              color: '#2A6DFB'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            textStyle: {
              color: '#fff'
            }
          },
          axisLine: { // 这是y轴线颜色
            show: true,
            lineStyle: {
              color: '#2A6DFB'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              color: 'rgba(0,145,255,.3)'
            }
          }
        },
        series: [
          {
            name: '告警数量',
            type: 'line',
            data: [15, 23, 35, 15, 62, 32, 56],
            smooth: true,
            symbol: 'circle',
            showSymbol: false,
            symbolSize: 8,
            itemStyle: {
              color: '#fff',
              shadowColor: 'rgba(255, 255, 255, .5)',
              shadowBlur: 10
            },
            lineStyle: {
              color: '#54A6FD',
              shadowColor: 'rgba(255, 255, 255, 1)',
              shadowBlur: 10
            }
          }
        ]
      }
    },
    // 鼠标移入图例触发
    enterHandler (item) {
      if (!this.chart) return
      this.chart.dispatchAction({
        type: 'highlight',
        name: item.name
      })
    },
    // 鼠标移出图例触发
    leaveHandler (item) {
      if (!this.chart) return
      this.chart.dispatchAction({
        type: 'downplay',
        name: item.name
      })
    },
    // 选中切换
    clickHandler (item) {
      if (!this.chart) return
      item.selected = !item.selected
      this.chart.dispatchAction({
        type: 'legendToggleSelect',
        name: item.name
      })
    }
  }
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值