echarts横向柱状图单独设置最大值和最小值的颜色

echarts yekong

vue 数据可视化大屏 项目开发中,为了突出柱状图的最大值和最小值,我们需要单独设置最大值和最小值的柱状图颜色,将实现效果记录下来。

echarts横向柱状图单独设置最大值和最小值的颜色

使用实例

<template>
  <div class="itemBodys">
    <echarts :list="list"></echarts>
  </div>
</template>

<script>
import echarts from "./components/echarts.vue";

export default {
  data() {
    return {
      list: [
        {
          "name": "1月",
          "value": "75992"
        },
        {
          "name": "2月",
          "value": "78517"
        },
        {
          "name": "3月",
          "value": "98000"
        },
        {
          "name": "4月",
          "value": "75875"
        },
        {
          "name": "5月",
          "value": "84369"
        },
        {
          "name": "6月",
          "value": "74476"
        },
        {
          "name": "7月",
          "value": "55565"
        },
        {
          "name": "8月",
          "value": "63946"
        },
        {
          "name": "9月",
          "value": "65134"
        },
        {
          "name": "10月",
          "value": "110500"
        },
        {
          "name": "11月",
          "value": "78165"
        },
        {
          "name": "12月",
          "value": "82172"
        }
      ],
    }
  },
  components: {echarts},
  computed: {},
  mounted() {
    var that = this;
  },
  methods: {},
  filters: {},
  watch: {}
}
</script>

<style lang="scss" scoped>
.itemBodys {
  width: 100%;
  position: relative;
  height: calc(100% - 20px);
}
</style>

echarts 代码

<template>
  <div class="echarts1" ref="echarts">

  </div>
</template>

<script>

import * as echarts from "echarts"
import lodash from 'lodash'

export default {
  name: 'echarts1',
  components: {},
  props: {
    list: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      status: '',
    }
  },
  computed: {
    xData: function () {
      var list = []
      this.list.forEach((type) => {
        list.push(type.name)
      });
      return list
    },
    yData: function () {
      var list = []
      this.list.forEach((type) => {
        list.push(Number(type.value))
      });
      return list
    },
  },
  watch: {},
  mounted() {
    this.drawLine()
  },
  methods: {
    drawLine() {
      var that = this
      window.addEventListener('resize', this.drawLine)
      var max = lodash.max(that.yData)
      var min = lodash.min(that.yData)
      console.log(min)
      console.log(max)
      var colorList = []
      this.list.forEach((type) => {
        var color = {
          color1: 'rgba(0, 162, 255, 1)',
          color2: 'rgba(0, 162, 255, 0.8)',
        }
        // 最大值
        if (type.value == max) {
          color = {
            color1: 'rgba(255, 193, 0, 1.00)',
            color2: 'rgba(255, 193, 0, 0.8)',
          }
        }
        // 最小值
        if (type.value == min) {
          color = {
            color1: 'rgba(220, 94, 103, 1.00)',
            color2: 'rgba(220, 94, 103, 0.8)',
          }
        }
        colorList.push(color)
      });
      let myChart = echarts.init(this.$refs.echarts)
      var option = {
        grid: {
          left: '3%',
          right: '8%',
          bottom: '1%',
          top: '5%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          },
          formatter: function (params) {
            return params[0].name + ' : ' + params[0].value
          }
        },
        xAxis: {
          show: true,
          splitLine: {
            show: true,
            textStyle: {
              color: '#02275f'
            },
            lineStyle: {
              color: '#022760',
              type: 'dashed',
            }
          },
          axisLine: {
            show: false,
            textStyle: {
              color: '#333'
            },
            lineStyle: {
              color: '#8c8c8c',
              type: 'dashed',
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: 'rgba(219, 231, 255, 0.6)'
            },
          },
          type: 'value'
        },
        yAxis: [{
          type: 'category',
          inverse: true,
          boundaryGap: true,
          axisLabel: {
            show: true,
            textStyle: {
              color: 'rgba(179, 216, 221, 1)',
              fontSize: 14
            }
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          data: this.xData
        }],
        series: [{
          type: 'bar',
          itemStyle: {
            normal: {
              color: function (params) {
                return new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                  offset: 0,
                  color: colorList[params.dataIndex].color1
                }, {
                  offset: 1,
                  color: colorList[params.dataIndex].color2
                }], false)
              }
            }
          },
          "label": {
            "normal": {
              "show": true,
              "position": "right",
              "distance": 0,
              "padding": [0, 0, 0, 5],
              "textStyle": {
                "color": "#d2d7e0", //柱子对应数值颜色
                "fontSize": 14
              }
            }
          },
          barWidth: '40%',
          data: this.yData
        }
        ]
      }
      myChart.clear()
      myChart.resize()
      myChart.setOption(option)
    },
  }
}
</script>

<style lang="scss" scoped>
.echarts1 {
  position: relative;
  width: 100%;
  height: 100%;
}
</style>

项目应用

财务数据可视化大屏

喜欢