vue如何做一个柱形图

614 ℃

vue如何利用echarts做一个柱形图,下面web建站小编给大家详细介绍一下具体实现代码!

template代码:

<template>
  <div ref="BarChart" style="height: 1000px; width: 100%"/>
</template>

js代码:

import echarts from 'echarts'export
default {
  data() {
      return {
       chart:
       null,
       data: [],
       //数据
       xData: [],
       //x轴
       yBarData: [],
       //y轴
       yLable: [],
       colorStops: [],
       chartLegend: [],
       //图例
       colorOptions: [ //图例以及柱形图颜色选择
       '#5ba2e4',
       '#f58510',
       '#afa5a5',
       '#facb3d',
       '#0854cf',
       '#48c611',
       '#082b63'
       ]
      }
  },
  mounted() {
      this.fetchData()
      //图的大小自适应
      window.addEventListener('resize', () = >{
       if (this.chart) {
        this.chart.resize()
       }
      })
  },
  beforeDestroy() { //实例销毁之前调用
     if (!this.chart) {
       return
      }
      this.chart.dispose()
      this.chart = null
  },
  methods: {
      fetchData() {
       this.xData = ["黑龙江", '辽宁', '贵州', '福建', '湖北', '河南', '河北', '山西', '山东', '天津', '吉林', '北京', '内蒙古', '云南']
       this.yLable = ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
       this.chartLegend = []
       const dateArr = []
       this.yLable.forEach((item, index) = >{
        if (item !== null && item !== undefined) {
         dateArr.push(this.yLable[index])
        }
       })
       this.chartLegend = dateArr this.initData()
       this.initChart()
      },
      initData() {
       this.yBarData = this.yLable
      },
      initChart() {
       this.chart = echarts.init(this.$refs.BarChart)
       this.chart.clear() // 清空当前实例
       let colors = []
       const dom = 800
       const barWidth = dom / 20
       for (let i = 0; i < 4; i++) { colors.push({ colorStops: [ { offset: 0, color: '#73fcff' // 最左边 }, { offset: 0.5, color: '#86eef1' // 左边的右边 颜色 }, { offset: 0.5, color: '#5ad6d9' // 右边的左边 颜色 }, { offset: 1, color: '#3dc8ca' }] }) } this.chart.setOption({ backgroundColor: '#010d3a', //提示框 tooltip: { trigger: 'axis', formatter: "{b} : {c}", axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, /**区域位置*/ grid: { left: '10%', right: '10%', top: '10%', bottom: '10%', }, //X轴 xAxis: [{ data: this.xData, type: 'category', show: true, axisLine: { show: false, lineStyle: { color: 'rgba(255,255,255,1)', shadowColor: 'rgba(255,255,255,1)', // shadowOffsetX: '20' }, symbol: ['none', 'arrow'], symbolOffset: [0, 25] }, splitLine: { show: false }, axisTick: { show: false }, axisLabel: { margin: 20, fontSize: 10 } }], yAxis: { show: true, splitNumber: 4, axisLine: { show: false }, splitLine: { show: true, lineStyle: { type: 'dashed', color: '#075858' }, }, axisLabel: { show: true, color: '#FFFFFF', margin: 30, fontSize: 15 } }, series: [ { //数据颜色 name: '日付费用户数', type: 'bar', barWidth: barWidth, itemStyle: { normal: { color: (params) = >{
               return colors[params.dataIndex % 4];
              }
          }
         },
         label: {
          show: true,
          position: [barWidth / 2, -(barWidth + 20)],
          color: '#ffffff',
          fontSize: 14,
          fontStyle: 'bold',
          align: 'center'
         },
         data: this.yBarData
        },
        { //底
         z: 2,
         type: 'pictorialBar',
         data: this.yBarData,
         symbol: 'diamond',
         symbolOffset: [0, '50%'],
         symbolSize: [barWidth, barWidth * 0.5],
         itemStyle: {
          normal: {
              color: (params) = >{
               return colors[params.dataIndex % 4]
              }
          }
         }
        },
        { //顶
         z: 3,
         type: 'pictorialBar',
         symbolPosition: 'end',
         data: this.yBarData,
         symbol: 'diamond',
         symbolOffset: [0, '-50%'],
         symbolSize: [barWidth, barWidth * 0.5],
         itemStyle: {
          normal: {
              borderWidth: 0,
              color: (params) = >{
               return colors[params.dataIndex % 4].colorStops[0].color;
              }
          }
         }
        }
        ]
       },
       true)
      }
  }
}

vue项目开发如何引入echarts图表

vue项目如何结合百度echarts生成图表

百度echarts做一个雷达图(根据不同分数值显示不同颜色)

利用百度echarts做一个温度计

百度echarts图表如何实现异步数据加载

标签: echarts, 柱形图

上面是“vue如何做一个柱形图”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_3442.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

2023年photoshop Beta功能点介绍(自带Ai创意填充绘图功能)
php如何利用函数过滤字符串的空格
wordpress获取所有分类菜单
阻止默认行为ev.preventDefault()不兼容ie6/7/8解决方法
js如何把数组里的字符串转为数字