当前位置:   article > 正文

vue+echarts项目三:商家销量统计(横向柱状图)_统计图表vue横向

统计图表vue横向

 总体效果如图

组件结构设计

SellerPage.vue

  1. <!--针对于/sellerpage 这条路径显示 测试显示组件-->
  2. <template>
  3. <div class="comP1">
  4. <Seller></Seller>
  5. </div>
  6. </template>
  7. <script>
  8. import Seller from "@/components/Seller";
  9. export default {
  10. name: "SellerPage",
  11. components:{Seller}
  12. }
  13. </script>
  14. <style scoped>
  15. </style>

Seller.vue

  1. <!-- 显示商家销量统计的图表 -->
  2. <template>
  3. <div class="comP2" ref="seller_1"></div>
  4. </template>
  5. <script>
  6. export default {
  7. data () {
  8. return {}
  9. },
  10. methods: {}
  11. }
  12. </script>
  13. <style lang="less" scoped>
  14. </style>

接下来就在 Seller.vue 搞事情了

mounted生命周期中初始化 echartsInstance 对象 

因为在组件挂载到页面上  echarts 才能找到具体的DOM渲染

methods 里定义 initChart 方法  this.$refs.seller_1 找到具体盒子  

this.theme 主题 来自于 Vuex 使用映射快捷引入

import {mapState} from "vuex";
computed:{
    ...mapState(['theme'])
},

然后就是设置配置项 在之前的基础都有讲到过

新增了柱状图渐变设置 可以详看注释

鼠标移入和移出时间  用来停止和开启定时器 后面会用到 

  1. methods:{
  2. // 初始化echarts 实例对象
  3. initChart(){
  4. this.chartsInstance = this.$echarts.init(this.$refs.seller_1,this.theme)
  5. const initOption = {
  6. title:{
  7. text:'▎销售业绩统计',
  8. left:20,
  9. top:15
  10. },
  11. grid:{
  12. top: '24%',
  13. left: '3%',
  14. right:'6%',
  15. bottom:'3%',
  16. containLabel: true // 距离是包含坐标轴上的文字
  17. },
  18. xAxis:{
  19. type:'value',
  20. },
  21. yAxis:{
  22. type: 'category',
  23. },
  24. tooltip:{
  25. trigger:'axis',
  26. axisPointer:{
  27. type:'line',
  28. z:0,
  29. lineStyle:{
  30. color:'#2d3443'
  31. }
  32. }
  33. },
  34. series:[
  35. {
  36. type:'bar', // 柱状图
  37. label:{
  38. show:true,// 显示柱内数值
  39. position:'right',// 数值显示位置
  40. textStyle: {
  41. color:'#fff'// 数值显示颜色
  42. }
  43. },
  44. itemStyle:{
  45. // 设置渐变 x1,y1,x2,y2(指明渐变的方向) [{指明不同百分比下颜色的值}]
  46. color:new this.$echarts.graphic.LinearGradient(0,0,1,0,[
  47. {
  48. offset:0,
  49. color:'#5052ee'
  50. },
  51. {
  52. offset: 1,
  53. color: '#ab6ee5'
  54. }
  55. ])
  56. }
  57. },
  58. ]
  59. }
  60. this.chartsInstance.setOption(initOption)
  61. // 对图表进行 鼠标移入移出 事件的监听
  62. this.chartsInstance.on('mouseover',()=>{
  63. clearInterval(this.timerID)
  64. })
  65. this.chartsInstance.on('mouseout',()=>{
  66. this.startInterval()
  67. })
  68. },
  69. }

发送请求获取对应的数据 并进行相应操作

使用到的data:

  1. data(){
  2. return{
  3. chartsInstance:null, 图表的实例对象 初始化后赋值给它
  4. allData:null, 请求过来的数据
  5. currentPage:1, 当前页码 定时器进行改变 来截取哪些数据展示
  6. totalPage:0, 总页数
  7. timerID:null 定时器的ID 用于启停
  8. }
  9. },

直接使用 注册的 axios  => $http.get 来获取  并通过 async await 简化  解构出来

进行 sort 排序操作 

计算出 每页显示5条信息的情况下 总页数是多少  能被5整除就直接用 整除不了就再加一页

  1. async getData(){
  2. const {data:res} = await this.$http.get('seller')
  3. this.allData = res
  4. // 对数据进行排序
  5. this.allData.sort((a,b) =>{
  6. return a.value - b.value // 从小到大排序
  7. })
  8. // 每五个数据 显示一页
  9. this.totalPage = this.allData.length % 5 === 0 ? this.allData.length / 5 : Math.floor(this.allData.length / 5) + 1
  10. this.updateChart()
  11. this.startInterval()
  12. }

数据和页码转存到 data 里了 可以更新设置 把图表渲染出来

当期页码默认是1   就截取 0-5的索引

在使用 map 生成新的数组  用于 x轴 和 y轴  

最后更新配置项 配置项会自动整合

  1. // 更新图表
  2. updateChart(){
  3. const start = (this.currentPage - 1) * 5
  4. const end = this. currentPage * 5
  5. const showData = this.allData.slice(start,end) // slice 截取 不包括 end
  6. const sellerNames = showData.map((item) =>{
  7. return item.name
  8. })
  9. const sellerValues = showData.map((item) =>{
  10. return item.value
  11. })
  12. const dataOption = {
  13. yAxis:{data:sellerNames},
  14. series:[{data:sellerValues}]
  15. }
  16. this.chartsInstance.setOption(dataOption)
  17. },

当第一页的数据展示出来时  就可以开启定时器了

开始之前先清除之前的定时器(来回切换页面后 回到最初的数据)

页码累计相加  到最大值再返回到1 

改变 当前页的同时 调用更新图表数据的方法

鼠标移入移出 启停定时器的方法 在注册实例的时候已经添加

  1. // 开启定时器
  2. startInterval(){
  3. if (this.timerID){
  4. clearInterval(this.timerID)
  5. }
  6. this.timerID = setInterval(()=>{
  7. this.currentPage++
  8. if (this.currentPage > this.totalPage){
  9. this.currentPage = 1
  10. }
  11. this.updateChart()
  12. },3600)
  13. },

 小细节

xAxis:{
    type:'value',
    // 细节处理 固定x轴的最大值
    max:this.allData[this.allData.length -1].value
},

当窗口尺寸发生变化时的操作

自己定义一个 合适 简易的 rem :当前窗口栅格化100份 * 3.6

根据这个数据  设定 标题大小 提示文字大小 柱状图的宽度和 圆角尺寸

初始化页面时 调用一次  之后 跟随窗口事件调用

mounted() {
  window.addEventListener('resize',this.screenAdapter)
  this.screenAdapter()
},
  1. // 当浏览器宽度发生变化时
  2. screenAdapter(){
  3. const titleFontSize = this.$refs.seller_1.offsetWidth / 100 * 3.6
  4. // 分辨率改变时更新的配置
  5. const adapterOption = {
  6. title:{
  7. textStyle:{
  8. fontSize: titleFontSize
  9. }
  10. },
  11. tooltip:{
  12. axisPointer:{
  13. lineStyle:{
  14. width:titleFontSize,
  15. }
  16. }
  17. },
  18. series:[
  19. {
  20. type:'bar', // 柱状图
  21. barWidth:titleFontSize,// 柱状宽度
  22. itemStyle:{
  23. barBorderRadius:[0,titleFontSize/2,titleFontSize/2,0],// 柱状的圆角
  24. }
  25. },
  26. ]
  27. }
  28. this.chartsInstance.setOption(adapterOption)
  29. this.chartsInstance.resize()
  30. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号