当前位置:   article > 正文

echarts的柱状图使用

echarts的柱状图使用

1. 柱状图(柱体顶部使用外部图片


在这里插入图片描述
相关代码

<template>
  <div class="out-bg">
    <div class="container" ref="warnChartRef"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  data() {
    return {
      jqrwChart: null,
      active: 0,
      xData: ['公交车', '卡车', '小客车', '工程车', '挖掘机', '未带安全帽', '烟雾', '货车', '轿车'], // 赋值x轴的数据
      seriesData: [147, 244, 2, 2, 174, 1, 1, 897, 1628],// 赋值Y轴的数据
      barTopImg: require('@/assets/images/bigScreen/bar-top.png'),
      symbolData: [],
      timer: null,
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    //echarts自适应
    fontSize(res) {
      let clientWidth =
        window.innerWidth ||
        document.documentElement.clientWidth ||
        document.body.clientWidth;
      if (!clientWidth) return;
      let fontSize = clientWidth / 1920;
      return res * fontSize;
    },
    initData() {
      this.xData.forEach((item, index) => {
        // 设置markPoint数据
        this.symbolData.push({
          symbol: 'image://' + this.barTopImg,
          symbolSize: [this.fontSize(20), this.fontSize(18)],
          xAxis: item,
          yAxis: this.seriesData[index], // 对应每列基础bar的值
        });
      });
      this.intChart();
    },
    intChart() {
      this.$nextTick(() => {
        this.jqrwChart = echarts.init(this.$refs.warnChartRef);
        this.jqrwChart.clear();
        this.jqrwChart.resize();
        let option = {
          grid: {
            containLabel: true,
            // width: '70%',
            // height: '68%',
            top: 20,
            right: 15,
            bottom: 0,
            left: 10,
          },
          tooltip: {
            trigger: "axis",
            axisPointer: {
              type: "shadow",
            },
          },
          xAxis: {
            // 类目轴
            type: "category",
            data: this.xData,
            axisTick: {
              show: false, //隐藏X轴刻度
            },
            axisLine: {
              lineStyle: {
                color: "rgba(57, 70, 89, 1)",
              },
            },
            axisLabel: {
              show: true,
              rotate: 25, // 轴转动
              textStyle: {
                color: "#fff",
                fontSize: this.fontSize(10),
                fontFamily: "Source Han Sans CN-Regular",
              },
            },
          },
          yAxis: {
            type: "value",
            name: "",
            nameTextStyle: { // 坐标轴名称的样式
              color: "rgba(255,255,255,1)",
              fontSize: this.fontSize(14),
              fontFamily: "Source Han Sans CN-Regular",
              align: "left",
              verticalAlign: "center",
            },
            axisLabel: { // 刻度标签
              show: true,
              color: "rgba(255,255,255,1)",
              textStyle: {
                fontSize: this.fontSize(14),
              },
            },
            axisLine: { // 刻度线
              show: false,
              lineStyle: {
                color: "rgba(57, 70, 89, 1)",
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: { // 分隔线
              lineStyle: {
                color: "rgba(57, 70, 89, 1)",
                type: "dashed",
              },
            },
          },
          series: [
            {
              name: "数据",
              type: "bar",
              barWidth: this.fontSize(20),
              showBackground: true,
              backgroundStyle: {
                color: "transparent",
              },
              label: {
                show: true,
                position: 'top',
                color: '#fff',
                fontSize: this.fontSize(14),
                fontFamily: "DingTalkJinBuTi",
                padding: [0, 0, this.fontSize(4), 0]
              },
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "rgba(0, 123, 232, 0.61)" },
                  { offset: 1, color: "rgba(24, 126, 171, 0)" },
                ]),
              },
              data: this.seriesData,
              markPoint: {
                data: this.symbolData,
              }
            },
          ],
        };
        this.jqrwChart.setOption(option, true);
        // 监听窗口变化
        window.addEventListener('resize', this.setSize)
      });
    },
    setSize() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        this.jqrwChart.resize();
      }, 500);
    },

  },
  destroyed() {
    window.removeEventListener('resize', this.setSize)
  }
}
</script>

<style lang="less" scoped>
  .out-bg{
    width: 100%;
    height: 100%;
    .container{
      width: 100%;
      height: 16.25rem;
    }
  }
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/577669
推荐阅读
相关标签
  

闽ICP备14008679号