• echarts可以轮播的横向柱状图


    <template>
      <div class="com-container">
        <div class="com-chart" ref="seller_ref"></div>
      </div>
    </template>
    
    <script>
    export default {
      data () {
        return {
          chartInstance: null, // echarts实例对象
          allData: [], // 服务器获取的所有数据
          currentPage: 1, // 当前页数
          totalPage: 0, // 总页数
          timerId: null // 定时器标识
        }
      },
      mounted () {
        this.initChart()
        this.getData()
        this.screenAdapter()
        window.addEventListener('resize', this.screenAdapter)
      },
      destroyed () {
        clearInterval(this.timerId)
        window.removeEventListener('resize', this.screenAdapter)
      },
      methods: {
        initChart () {
          // 初始化echarts实例对象
          this.chartInstance = this.$echarts.init(this.$refs.seller_ref, 'dark')
          // this.chartInstance = this.$echarts.init(this.$refs.seller_ref, 'vintage')
          const option = {
            xAxis: {
              type: 'value'
            },
            yAxis: {
              type: 'category'
            },
            title: {
              text: '▎ 商家销量排行',
              left: 20,
              top: 20,
              textStyle: {
                color: '#fff',
                fontSize: 66
              }
            },
            grid: {
              top: '20%',
              left: '3%',
              right: '6%',
              bottom: '3%',
              containLabel: true
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'line',
                z: 1,
                lineStyle: {
                  width: 66,
                  color: '#2D3443'
                }
              }
            },
            series: [{
              type: 'bar',
              label: {
                show: true,
                position: 'right',
                textStyle: {
                  color: '#fff'
                }
              },
              barWidth: 66,
              itemStyle: {
                barBorderRadius: [0, 33, 33, 0],
                color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: '#5052EE'
                  }, {
                    offset: 1,
                    color: '#AB6EE5'
                  }
                ])
              }
    
            }]
          }
          this.chartInstance.setOption(option)
          this.chartInstance.on('mouseover', () => {
            clearInterval(this.timerId)
          })
          this.chartInstance.on('mouseout', () => {
            this.startInterval()
          })
        },
        async getData () {
          // 获取数据
          const {
            data: res
          } = await this.$http.get('seller')
          console.log(res)
          this.allData = res
          // 从大道小的排序
          this.allData.sort((a, b) => {
            return a.value - b.value
          })
          // 计算总页数
          this.totalPage = this.allData.length % 5 ? this.allData.length / 5 : this.allData.length / 5 + 1
          this.updateChart()
          // 开启轮播
          this.startInterval()
        },
        updateChart () {
          const start = (this.currentPage - 1) * 5
          const end = this.currentPage * 5
          const showData = this.allData.slice(start, end)
          // 处理数据并且更新界面图表
          const sellerNames = showData.map(item => item.name)
          const sellerValues = showData.map(item => item.value)
          const option = {
            xAxis: {
              // 设置最大值
              max: this.allData[this.allData.length - 1].value
            },
            yAxis: {
              data: sellerNames
            },
            series: [{
              data: sellerValues
            }]
          }
          this.chartInstance.setOption(option)
        },
        startInterval () {
          if (this.timerId) {
            clearInterval(this.timerId)
          }
          this.timerId = setInterval(() => {
            this.currentPage++
            if (this.currentPage > this.totalPage) {
              this.currentPage = 1
            }
            this.updateChart()
          }, 3000)
        },
        screenAdapter () {
          const titleFontSize = this.$refs.seller_ref.offsetWidth / 100 * 3.6
          const option = {
            title: {
              textStyle: {
                fontSize: titleFontSize
              }
            },
            tooltip: {
              axisPointer: {
                lineStyle: {
                  width: titleFontSize
                }
              }
            },
            series: [
              {
                barWidth: titleFontSize,
                itemStyle: {
                  barBorderRadius: [0, titleFontSize / 2,
                    titleFontSize / 2, 0]
                }
              }
            ]
          }
          this.chartInstance.setOption(option)
          this.chartInstance.resize()
        }
    
      }
    }
    
    </script>
    
    <style scoped>
    
    </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
    • 184
    • 185
    • 186
    • 187
  • 相关阅读:
    Linux学习笔记8 - 文件IO编程(二)
    (文献随笔)肿瘤浸润的活化B细胞抑制结直肠癌的肝转移(Cell Report, 2022年8月30日)
    数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
    【C++】构造函数与类的组合以及初始化
    Go by Example for循环
    [AIGC] Java序列化利器 gson
    详解pyautogui模块
    深度学习遥感数据集
    [网鼎杯 2020 朱雀组]Nmap
    PCF+fluentUI开发组件并导入Microsoft Power Platform Power App
  • 原文地址:https://blog.csdn.net/haodanzj/article/details/126560037