Newer
Older
CloudBrainNew / src / components / barChart / polarBar.vue
StephanieGitHub on 4 Feb 2021 5 KB first commit
<template>
  <div
    :id="id"
    :style="`width:${curWidth};height:${curHeight}`"
  >
  </div>
</template>
<script>
/**
   * @author 王晓颖
   * @date 2020/08/18
   * @Description 单色渐变柱状图
   * @props 数据格式:{
   *    id: '',
      width: 0,
      height: 0,
      xAxisData:[],
      seriesData: []
    }
   *
   */
import { countSize } from '@/utils/utils'
export default {
  name: 'PolarBarChart',
  props: {
    id: { // id
      type: String,
      default: 'simpleBarChart'
    },
    width: { // 宽
      type: Number | String,
      default: '100%'
    },
    height: { // 高
      type: Number | String,
      default: '100%'
    },
    unit: { // 单位
      type: String,
      default: ''
    },
    colors: { // 颜色
      type: Array,
      default: () => { return ['#01b4ff', '#0337ff'] }
    },
    axisLineColor: { // 轴线颜色
      type: String,
      default: '#101d5d'
    },
    fontColor: { // 轴线上文字颜色
      type: String,
      default: '#ffffff'
    },
    labelColor: { // 图形上文本标签颜色
      type: String,
      default: '#47c8b2'
    },
    seriesData: { // 数据
      type: Array,
      default: () => { return [] }
    },
    xAxisData: { // X轴数据
      type: Array,
      default: () => { return [] }
    }
  },
  data () {
    return {
      curWidth: this.width,
      curHeight: this.height,
      option: {
        tooltip:{
          trigger: 'axis', // 触发类型
          axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
          }
        },
        polar: {},
        grid: {
          top: countSize(0.3),
          left: '3%',
          right: '0',
          bottom: '5',
          containLabel: true // 是否包含坐标轴的刻度标签
        }, // 网格
        angleAxis: {
          name: '', // 坐标轴名称
          nameTextStyle: { // 坐标轴名称的文字样式
            color: this.fontColor,
            fontSize: '50%',
            verticalAlign: 'middle'
          },
          type: 'value', // 数值轴
          color: this.fontColor, //
          splitLine: { // 在grid区域中的分隔线
            lineStyle: {
              color: ['#101641'],
              type: 'dashed'
            }
          },
          axisLine: {
            lineStyle: {
              color: this.axisLineColor // y轴线的颜色
            }
          },
          axisLabel: {
            color: this.fontColor, // Y轴单位颜色
            fontSize: '40%'
          }
        },
        radiusAxis: {
          type: 'category', // 类目轴
          data: [
            // '周一','周二','周三','周四','周五','周六','周日'
          ],
          axisLine: {
            lineStyle: {
              color: this.axisLineColor // 轴线的颜色
            }
          }
          // axisLabel: {
          //   color: this.fontColor, // X轴名称颜色
          //   fontSize: 8
          // }
        },
        series: [
          {
            name: '总数量', // 系列名称
            type: 'bar', // 类型
            coordinateSystem: 'polar',
            stack: 'a',
            // barCategoryGap: '40%', // 同一系列柱间距离
            itemStyle: { // 图形演示
              normal: {
                barBorderRadius: [countSize(0.1), countSize(0.1), countSize(0.1), countSize(0.1)],
                // LinearGradient 用于配置渐变色的起始位置,分别对应右下左上,0001表示正上方
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1,
                  [
                    {offset: 0, color: '#01b4ff'},
                    {offset: 1, color: '#0337ff'}
                  ]
                )}
            },
            // label: {
            //   normal: {
            //     show: true,
            //     color: this.labelColor,
            //     position: 'top',
            //     fontSize: '50%'
            //   }
            // }, // 图形上的文本标签
            data: [
              // 320, 302, 301, 334, 390, 330, 320
            ]
          }
        ]
      }
    }
  },
  watch: {
    width (newVal, oldVal) {
      this.curWidth = newVal
      this.refreshEchart()
    },
    height (newVal, oldVal) {
      this.curHeight = newVal
      this.refreshEchart()
    },
    seriesData (newVal, oldVal) {
      this.option.series[0].data = newVal
      this.refreshEchart()
    },
    xAxisData (newVal, oldVal) {
      this.option.radiusAxis.data = newVal
      this.refreshEchart()
    }
  },
  mounted () {
    if (this.seriesData.length) {
      this.option.series[0].data = this.seriesData
      this.option.radiusAxis.data = this.xAxisData
      this.unit && (this.option.yAxis.name = this.unit)
      const colors = []
      for (const index in this.colors) {
        if (index === '0') {
          colors.push({offset: 0, color: this.colors[index]})
        } else {
          colors.push({offset: 1, color: this.colors[index]})
        }
      }
      this.option.series[0].itemStyle = { // 图形演示
        normal: {
          barBorderRadius: [countSize(0.5), countSize(0.1), countSize(0.1), countSize(0.1)],
          // LinearGradient 用于配置渐变色的起始位置,分别对应右下左上,0001表示正上方
          color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, colors)}
      }
      console.log(this.option)
      this.initEchart()
    }
  },
  methods: {
    refreshEchart () {
      if (this.curWidth && this.curHeight && this.seriesData.length && this.xAxisData.length) {
        this.initEchart()
      }
    },
    initEchart () {
      const _div = document.getElementById(this.id)
      setTimeout(() => {
        let myChart = this.$echarts.init(_div)
        myChart.setOption(this.option, true)
        window.onresize = myChart.resize
      }, 500)
    }
  }
}
</script>

<style scoped>
</style>