echarts柱状图

271 阅读1分钟
<template>
  <div :style="{ width: width, height: height }">
  </div>
</template>

<script>
var echarts = require('echarts')
require('echarts/theme/macarons')
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '240px'
    },
    chartData: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data() {
    return {
      chart: null,
      isJust: 0,
      isLose: 0
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.chartInit()
    })
    // console.log(this.$props);
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    chartInit() {
      // this.chart = echarts.init(this.$el, "macarons");
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions(chartData = []) {
      const label = []
      const data = []

      chartData.map(el => {
        label.push(el.Text)
        data.push(el.Value)
      })
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器,坐标轴触发有效
            type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
          },
          formatter: '{c}个'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          height: '80%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: label,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [{
          name: `个`, type: 'value', minInterval: 1
        }],
        series: [
          {
            name: '数量',
            type: 'bar',
            barWidth: '100vw',
            data: data.map(item => {
              // console.log(item, 'item')
              return {
                value: item,
                label: {
                  show: true,
                  fontSize: 12,
                  position: 'top',
                  distance: 10,
                  color: '#666',
                  padding: [4, 4],
                  formatter: '{c}'
                },
                itemStyle: {

                  borderRadius: item > 0 ? [8, 8, 0, 0] : [0, 0, 8, 8], // 动态设置柱状图圆角
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: '#5B8FF9'
                    },
                    {
                      offset: 1,
                      color: '#5B8FF9'
                    }
                  ]),
                  label: {
                    show: true,
                    position: 'top',
                    formatter: (el) => {
                      return el.value
                    }
                  },
                  itemStyle: {
                    color: '#5B8FF9'
                    // color: (data) => {
                    //   if (data.data > 90 && data.data < 99) {
                    //     return "rgba(255, 121, 70, 0.85)";
                    //   } else if (data.data >= 99) {
                    //     return "red";
                    //   } else {
                    //     return "rgba(106, 165, 244, 0.85)";
                    //   }
                    // },
                    // borderRadius: [8, 8, 0, 0]
                  }

                }
              }
            })

          }]
      })
    }
  }
}
</script>

<style scoped></style>