当前位置:   article > 正文

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

vue 做排行榜

最终效果如图

组件结构设计

外部 Rankpage.vue

  1. <template>
  2. <div class="comP1">
  3. <Rank></Rank>
  4. </div>
  5. </template>
  6. <script>
  7. import Rank from "@/components/Rank";
  8. export default {
  9. name: "Rankpage",
  10. components:{Rank}
  11. }
  12. </script>
  13. <style scoped>
  14. </style>

内部 Rank.vue

  1. <!-- 显示地区销量排行的柱形图表 -->
  2. <template>
  3. <div class="comP2" ref="rank_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>

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

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

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

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

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

  1. initChart(){
  2. this.chartsInstance = this.$echarts.init(this.$refs.rank_1,this.theme)
  3. const initOption = {
  4. title:{
  5. text:'▎地区销售排行',
  6. left:20,
  7. top:15
  8. },
  9. grid:{
  10. top: '36%',
  11. left: '6%',
  12. right:'6%',
  13. bottom:'4%',
  14. containLabel: true // 距离是包含坐标轴上的文字
  15. },
  16. xAxis:{
  17. type:'category',
  18. },
  19. yAxis:{
  20. type: 'value',
  21. },
  22. tooltip:{
  23. trigger:'axis',
  24. axisPointer:{
  25. type:'line',
  26. z:0,
  27. lineStyle:{
  28. color:'#2d3443'
  29. }
  30. }
  31. },
  32. series:[
  33. {
  34. type:'bar', // 柱状图
  35. label:{
  36. show:true,// 显示柱内数值
  37. position:'top',// 数值显示位置
  38. textStyle: {
  39. color:'#fff'// 数值显示颜色
  40. }
  41. },
  42. },
  43. ]
  44. }
  45. this.chartsInstance.setOption(initOption)
  46. // 对图表进行 鼠标移入移出 事件的监听
  47. this.chartsInstance.on('mouseover',()=>{
  48. clearInterval(this.timerID)
  49. })
  50. this.chartsInstance.on('mouseout',()=>{
  51. this.startInterval()
  52. })
  53. },

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

  1. 请求过来数据 进行sort 排序 赋值给 allData
  2. 使用数据更新 图表 先定义三个渐变值、map重新映射出来 每一项的名字和数值 itemStyle每个柱子的颜色动态决定 返回函数 生成一个渐变值
  3. 设置图表开启定时器 定时器相加的是 startValue endValue 改变 dataZoom 的具体缩放位置来达到流动柱状图的效果
  1. getData(){
  2. const {data:res} = await this.$http.get('rank')
  3. this.allData = res
  4. this.allData.sort((a,b) =>{
  5. return b.value - a.value // 从大到小排序
  6. })
  7. this.updateChart()
  8. this.startInterval()
  9. },
  10. updateChart(){
  11. // 定义不同数值的渐变颜色
  12. const colorArr = [
  13. ['#0ba82c','#4ff778'],
  14. ['#2e72bf','#23e5e5'],
  15. ['#5052ee','#ab6ee5']
  16. ]
  17. const rankNames = this.allData.map((item) =>{
  18. return item.name
  19. })
  20. const rankValues = this.allData.map((item) =>{
  21. return item.value
  22. })
  23. const dataOption = {
  24. xAxis:{data:rankNames},
  25. series:[{data:rankValues}],
  26. dataZoom:{
  27. show:false,
  28. startValue: this.startValue,
  29. endValue: this.endValue
  30. },
  31. itemStyle:{
  32. // 设置渐变 x1,y1,x2,y2(指明渐变的方向) [{指明不同百分比下颜色的值}]
  33. color:arg =>{
  34. let targetColorArr = null
  35. if (arg.value > 260){
  36. targetColorArr = colorArr[0]
  37. }else if (arg.value > 180){
  38. targetColorArr = colorArr[1]
  39. }else {
  40. targetColorArr = colorArr[2]
  41. }
  42. return new this.$echarts.graphic.LinearGradient(0,0,0,1,[
  43. {
  44. offset:0,
  45. color:targetColorArr[0]
  46. },
  47. {
  48. offset: 1,
  49. color: targetColorArr[1]
  50. }
  51. ])
  52. }
  53. }
  54. }
  55. this.chartsInstance.setOption(dataOption)
  56. },
  57. startInterval(){
  58. if (this.timerID){
  59. clearInterval(this.timerID)
  60. }
  61. this.timerID = setInterval(()=>{
  62. this.startValue++
  63. this.endValue++
  64. if (this.endValue > this.allData.length - 1){
  65. this.startValue = 0
  66. this.endValue = 9
  67. }
  68. this.updateChart()
  69. },2500)
  70. },

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/630578
推荐阅读
相关标签
  

闽ICP备14008679号