vue+echarts项目六:地区销量排行(流动柱状图)

22 篇文章 2 订阅

最终效果如图

组件结构设计

外部 Rankpage.vue

<template>
  <div class="comP1">
    <Rank></Rank>
  </div>
</template>

<script>
  import Rank from "@/components/Rank";
  export default {
    name: "Rankpage",
    components:{Rank}
  }
</script>

<style scoped>

</style>

内部 Rank.vue

<!-- 显示地区销量排行的柱形图表 -->
<template>
<div class="comP2" ref="rank_1"></div>
</template>
<script>
export default {
data () {
return {}
},
methods: {}
}
</script>
<style lang="less" scoped>
</style>

初始化图表+数据的获取+更新图表

挂载的生命周期中 注册图表、发送请求获取数据、添加响应页面尺寸的事件   

卸载的生命周期中 取消定时器、解绑响应页面尺寸的事件 

    mounted() {
      // 渲染DOM元素之后 初始化图表实例 请求数据 监听页面尺寸变化
      this.initChart()
      this.getData()
      window.addEventListener('resize',this.screenAdapter)
      this.screenAdapter()
    },
    destroyed() {
      clearInterval(this.timerID)
      window.removeEventListener('resize',this.screenAdapter)
    },

  初始化图表:绑定鼠标移入移除反应定时器事件

initChart(){
        this.chartsInstance = this.$echarts.init(this.$refs.rank_1,this.theme)
        const initOption = {
          title:{
            text:'▎地区销售排行',
            left:20,
            top:15
          },
          grid:{
            top: '36%',
            left: '6%',
            right:'6%',
            bottom:'4%',
            containLabel: true // 距离是包含坐标轴上的文字
          },
          xAxis:{
            type:'category',
          },
          yAxis:{
            type: 'value',
          },
          tooltip:{
            trigger:'axis',
            axisPointer:{
              type:'line',
              z:0,
              lineStyle:{
                color:'#2d3443'
              }
            }
          },
          series:[
            {
              type:'bar', // 柱状图
              label:{
                show:true,// 显示柱内数值
                position:'top',// 数值显示位置
                textStyle: {
                  color:'#fff'// 数值显示颜色
                }
              },
            },
          ]
        }
        this.chartsInstance.setOption(initOption)
        // 对图表进行 鼠标移入移出 事件的监听
        this.chartsInstance.on('mouseover',()=>{
          clearInterval(this.timerID)
        })
        this.chartsInstance.on('mouseout',()=>{
          this.startInterval()
        })
      },

 获取数据 整合配置 渲染图表  默认开启定时器

  1. 请求过来数据 进行sort 排序 赋值给 allData
  2. 使用数据更新 图表 先定义三个渐变值、map重新映射出来 每一项的名字和数值 itemStyle每个柱子的颜色动态决定 返回函数 生成一个渐变值
  3. 设置图表开启定时器 定时器相加的是 startValue endValue 改变 dataZoom 的具体缩放位置来达到流动柱状图的效果
      getData(){
        const {data:res} = await this.$http.get('rank')
        this.allData = res
        this.allData.sort((a,b) =>{
          return b.value - a.value // 从大到小排序
        })
        this.updateChart()
        this.startInterval()
      },
      updateChart(){
        // 定义不同数值的渐变颜色
        const colorArr = [
          ['#0ba82c','#4ff778'],
          ['#2e72bf','#23e5e5'],
          ['#5052ee','#ab6ee5']
        ]

        const rankNames = this.allData.map((item) =>{
          return item.name
        })
        const rankValues = this.allData.map((item) =>{
          return item.value
        })
        const dataOption = {
          xAxis:{data:rankNames},
          series:[{data:rankValues}],
          dataZoom:{
            show:false,
            startValue: this.startValue,
            endValue: this.endValue
          },
          itemStyle:{
            // 设置渐变 x1,y1,x2,y2(指明渐变的方向) [{指明不同百分比下颜色的值}]
            color:arg =>{
              let targetColorArr = null
              if (arg.value > 260){
                targetColorArr = colorArr[0]
              }else if (arg.value > 180){
                targetColorArr = colorArr[1]
              }else {
                targetColorArr = colorArr[2]
              }
              return new this.$echarts.graphic.LinearGradient(0,0,0,1,[
                {
                  offset:0,
                  color:targetColorArr[0]
                },
                {
                  offset: 1,
                  color: targetColorArr[1]
                }
              ])
            }
          }
        }
        this.chartsInstance.setOption(dataOption)
      },
      startInterval(){
        if (this.timerID){
          clearInterval(this.timerID)
        }
        this.timerID = setInterval(()=>{
          this.startValue++
          this.endValue++
          if (this.endValue > this.allData.length - 1){
            this.startValue = 0
            this.endValue = 9
          }
          this.updateChart()
        },2500)
      },

每个图表都有的 响应页面尺寸的回调:

      screenAdapter(){
        const titleFontSize = this.$refs.rank_1.offsetWidth / 100 * 3.6
        // 分辨率改变时更新的配置
        const adapterOption = {
          title:{
            textStyle:{
              fontSize: titleFontSize
            }
          },
          tooltip:{
            axisPointer:{
              lineStyle:{
                width:titleFontSize,
              }
            }
          },
          series:[
            {
              barWidth:titleFontSize,// 柱状宽度
              itemStyle:{
                barBorderRadius:[titleFontSize/2,titleFontSize/2,0,0],// 柱状的圆角
              }
            },
          ]
        }
        this.chartsInstance.setOption(adapterOption)
        this.chartsInstance.resize()
      },

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个基于 VueEcharts 的动态柱状图示例: ```html <template> <div id="myChart" style="width: 600px;height:400px;"></div> </template> <script> import * as echarts from 'echarts'; export default { data() { return { chartData: [], // 存储柱状图的数据 chartTimer: null // 定时器 }; }, mounted() { // 初始化 echarts 实例 this.myChart = echarts.init(document.getElementById('myChart')); // 显示初始的柱状图 this.showChart(); // 每隔 1 秒更新一次柱状图 this.chartTimer = setInterval(() => { this.showChart(); }, 1000); }, methods: { // 显示柱状图 showChart() { // 随机生成一些数据 const data = []; for (let i = 0; i < 5; i++) { data.push(Math.round(Math.random() * 100)); } // 更新柱状图的数据 this.chartData = data; // 配置柱状图的参数 const option = { title: { text: '动态柱状图示例' }, tooltip: {}, xAxis: { data: ['数据1', '数据2', '数据3', '数据4', '数据5'] }, yAxis: {}, series: [{ name: '数据量', type: 'bar', data: this.chartData }] }; // 使用刚指定的配置项和数据显示图表 this.myChart.setOption(option); } }, beforeDestroy() { // 在组件销毁前清除定时器 clearInterval(this.chartTimer); } }; </script> ``` 此示例使用了一个计时器来每隔 1 秒更新一次柱状图的数据,并使用 Echarts 的 API 更新图表。您可以将其复制到一个 Vue 单文件组件中并运行以查看动态柱状图。如果需要更多帮助,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值