当前位置: 首页 > article >正文

echarts组件——条形统计图

echarts组件——条形统计图

竖向条形统计图,单柱状,多柱状,悬浮框展示
在这里插入图片描述

组件代码

<template>
  <div :class="classname" :style="{height:height,width:width}" />
</template>

<script>
// 柱状图
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 2000

export default {
  mixins: [resize],
  props: {
    classname: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    color: { // 柱状条颜色
      type: Array,
      default: () => ['#6493FE', '#58c971', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc']
    },
    xaxisdata: { // x轴数据
      type: Array,
      default: () => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    legenddata: { // 图例
      type: Array,
      default: () => [
        'Direct',
        'Mail Ad',
        'Affiliate Ad',
        'Video Ad',
        'Search Engine'
      ]
    },
    legendshow: { // 图例显示
      type: Boolean,
      default: () => false
    },
    bardata: { // 数据
      type: Array,
      default: () => [
        [100, 302, 301, 334, 390, 330, 320],
        [320, 132, 101, 134, 90, 230, 210],
        [220, 182, 191, 234, 290, 330, 310],
        [150, 212, 201, 154, 190, 330, 410],
        [820, 832, 901, 934, 1290, 1330, 1320]
      ]
    },
    totalname: { // tooltip内容 共计的名字
      type: String,
      default: '共计'
    },
    tooltipxcustom: { // 悬浮框的标题自定义
      type: Boolean,
      default: false
    },
    xaxisvalue: { // 悬浮框标题自定义要展示的数据
      type: Array,
      default: () => []
    },
    tooltipunitcustom: { // 悬浮框的数据的单位,比如加%号
      type: String,
      default: ''
    },
    barwidth: {
      type: String,
      default: '20%'
    }
  },
  data() {
    return {
      series: [],
      chart: null
    }
  },
  watch: {
    bardata: {
      deep: true,
      handler(val) {
        this.$nextTick(() => {
          this.series = this.legenddata.map((name, sid) => {
            return {
              name,
              type: 'bar',
              barWidth: this.barwidth,
              data: this.bardata[sid],
              animationDuration,
              barGap: 0 // 相邻柱子的间隔
            }
          })
          this.initChart()
        })
      }
    }
  },
  mounted() {
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        color: this.color,
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: 'line' // 默认为直线,可选为:'line' | 'shadow'
          },
          formatter: (params) => {
            let result = `<div>${this.tooltipxcustom ? this.xaxisvalue[params[0].dataIndex] : params[0].axisValue}</div>`
            params.map((item, index) => {
              result += `<div style="display: flex; justify-content: space-between"><span>${item.seriesName}:  </span><span>${item.value}${item.value !== '--' ? this.tooltipunitcustom : ''}</span></div>`
            })
            return result
          }
        },
        legend: {
          icon: 'circle',
          top: '0',
          right: '0',
          show: this.legendshow,
          data: this.legenddata
          // bottom: '0'
        },
        grid: {
          top: '8%',
          left: '2%',
          right: '2%',
          bottom: '15',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.xaxisdata,
          axisTick: {
            show: false, // 隐藏x轴的分割点
            alignWithLabel: true
          },
          axisLabel: {
            interval: 0 // x轴标签全部显示
          },
          axisLine: {
            lineStyle: {
              color: '#86909C'
            }
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#86909C'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#E5E6EB',
              type: 'dashed'
            }
          },
          splitArea: {
            show: false
          }
        }],
        series: this.series
      })
    }
  }
}
</script>

使用组件,传值格式可以看组件的默认数据的格式
在这里插入图片描述

注意,别忘记给echart-div宽高,比如下图
在这里插入图片描述


http://www.kler.cn/a/279901.html

相关文章:

  • PHP概述-特点-应用领域-如何学习
  • PowerShell自动化Windows系统管理任务
  • ubuntu 20.04系统安装pytorch
  • Linux—互斥锁
  • 云原生周刊:KubeSphere 宣布开源 Thanos 的企业级发行版 Whizard
  • 深入解析SSRF和Redis未授权访问
  • 深度学习100问3-什么是共现矩阵及其作用
  • 华为Huawei路由器交换机SSH配置
  • 009 批量删除
  • docker 拉取镜像超时
  • visual studio 2022 加载.vdproj
  • 原神4.8版本抽到角色和重点培养数据表
  • EV代码签名证书——消除软件下载时的安全警告
  • AI学习指南深度学习篇-长短时记忆网络(LSTM)简介
  • 《Object-Oriented Software Construction》第2版读后感
  • 《机器学习》 SVM支持向量机 推导、参数解析、可视化实现
  • 高效能低延迟:EasyCVR平台WebRTC支持H.265在远程监控中的优势
  • react中的useCallback、useMemo、useRef 和 useContext
  • 京存分布式赋能EDA应用
  • 安卓中回调函数的使用