• vue+echarts项目八:库存情况(显示占比圆环图)


    最终效果如图

    组件结构设计

    外部Stockpage.vue

     显示最外侧组件的结构

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

    内部的Stock

    1. <template>
    2. <div class="comP2" ref="stock_1">div>
    3. template>
    4. <script>
    5. export default {
    6. data () {
    7. return {}
    8. },
    9. methods: {}
    10. }
    11. script>
    12. <style lang="less" scoped>
    13. style>

    初始化图表+数据的获取+更新图表+定时器切换的启停

    组件挂载到DOM时  初始化图表  =》 获取数据 =》更新图表 =》 增加分辨率适配

    组件被卸载时:清除定时器 清除分辨率变化的响应 

    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.stock_1,this.theme)
    3. const initOption = {
    4. title:{
    5. text:'▎库存和销量分析',
    6. left:20,
    7. top:30
    8. },
    9. }
    10. this.chartsInstance.setOption(initOption)
    11. this.chartsInstance.on('mouseover',()=>{
    12. clearInterval(this.timerID)
    13. })
    14. this.chartsInstance.on('mouseout',()=>{
    15. this.startInterval()
    16. })
    17. },

    获取数据

    get 到数据 调用更新图表的方法  启动定时器

     getData(){
      const {data:res} = await this.$http.get('stock')
      this.allData = res
      this.updateChart()
      this.startInterval()
    },

    获取到的数据:

    1. [{
    2. "name": "二建通关大礼包",
    3. "stock": 2310,
    4. "sales": 2103
    5. }, {
    6. "name": "21天过二建",
    7. "stock": 34312,
    8. "sales": 23509
    9. }, {
    10. "name": "二建不过退费班",
    11. "stock": 22140,
    12. "sales": 12830
    13. }, {
    14. "name": "单独教材班",
    15. "stock": 10842,
    16. "sales": 5492
    17. }, {
    18. "name": "抖音领航班",
    19. "stock": 68102,
    20. "sales": 44043
    21. }, {
    22. "name": "二建押题班",
    23. "stock": 12032,
    24. "sales": 8603
    25. }, {
    26. "name": "一建智学班",
    27. "stock": 9890,
    28. "sales": 8960
    29. }, {
    30. "name": "28天过一建",
    31. "stock": 20130,
    32. "sales": 12302
    33. }, {
    34. "name": "一建精学班",
    35. "stock": 89342,
    36. "sales": 42948
    37. }, {
    38. "name": "1V1督学班",
    39. "stock": 5034,
    40. "sales": 1220
    41. }]

    数据更新图表 

    对获取到的数据进行操作 得到想要的 设置到配置项 更新图表

    使用到的数据
    data(){
      return{
        chartsInstance:null, // 组件实例
        allData:null, // 请求过来处理过后的数据
        currentIndex:0, // 根据这个改变显示数列
        timerID:null // 定时器
      }
    },
    1. 定义两个数组 一个是中心点(基于x轴偏移多少,基于y)   一个是渐变色的数组
    2. 根据现有的 currentIndex *5 取出要展示的数据  map操作出对应的系列  系列就是五个饼图(圆环图)  item.sales 用渐变色  item.stock用灰底色
    1. updateChart(){
    2. const centerArr = [
    3. ['18%','40%'],
    4. ['50%','40%'],
    5. ['82%','40%'],
    6. ['34%','75%'],
    7. ['66%','75%']
    8. ]
    9. const colorArr = [
    10. ['#4ff778','#0ba82c'],
    11. ['#e5dd45','#e8b11c'],
    12. ['#e8821c','#e55445'],
    13. ['#5052ee','#ab6ee5'],
    14. ['#23e5e5','#2e72bf'],
    15. ]
    16. const start = this.currentIndex * 5
    17. const end = (this.currentIndex + 1) * 5
    18. const showData = this.allData.slice(start,end)
    19. const seriesArr = showData.map((item,index) => {
    20. return {
    21. type:'pie',
    22. center:centerArr[index],
    23. hoverAnimation:false, // 关闭鼠标移入饼图时的动画效果
    24. labelLine:{
    25. show:false // 隐藏指示线
    26. },
    27. label:{
    28. position:'center',
    29. color:colorArr[index][0]
    30. },
    31. data:[
    32. {
    33. name:item.name + '\n' + '\n' + item.sales,
    34. value:item.sales,
    35. itemStyle:{
    36. color: new this.$echarts.graphic.LinearGradient(0,1,0,0,[
    37. {
    38. offset:0,
    39. color:colorArr[index][0]
    40. },
    41. {
    42. offset:1,
    43. color:colorArr[index][1]
    44. }
    45. ])
    46. }
    47. },
    48. {
    49. value:item.stock,
    50. itemStyle:{
    51. color:'#333843'
    52. }
    53. }
    54. ]
    55. }
    56. })
    57. const dataOption = {
    58. series:seriesArr
    59. }
    60. this.chartsInstance.setOption(dataOption)
    61. },
    '
    运行

    定时器的回调

    0-1 之间切换 因为总数居10条 一次展示5条

    1. startInterval(){
    2. if (this.timerID){
    3. clearInterval(this.timerID)
    4. }
    5. this.timerID = setInterval(()=>{
    6. this.currentIndex++
    7. if (this.currentIndex > 1){
    8. this.currentIndex = 0
    9. }
    10. this.updateChart()
    11. },4400)
    12. },

    屏幕尺寸变化的响应

    圆环的半径在这里相应的设置 

    1. screenAdapter(){
    2. // 标题 文字 圆环
    3. const titleFontSize = this.$refs.stock_1.offsetWidth / 100 * 3.6
    4. const innerRadius = titleFontSize * 2.8
    5. const outRadius = titleFontSize * 2.4
    6. const seriesArr = []
    7. for (let i=0;i<5;i++){
    8. seriesArr.push({
    9. type:'pie',
    10. radius:[outRadius,innerRadius],
    11. label: {
    12. fontSize: titleFontSize / 1.4
    13. }
    14. })
    15. }
    16. const adapterOption = {
    17. title:{
    18. textStyle:{
    19. fontSize: titleFontSize
    20. }
    21. },
    22. series: seriesArr
    23. }
    24. this.chartsInstance.setOption(adapterOption)
    25. this.chartsInstance.resize()
    26. }

  • 相关阅读:
    高等教育心理学:学生的认知发展
    【探索C语言中VS调试技巧】:提高效率和准确性
    氮化镓充电器芯片 NV6128、NV6127、NV6125 QFN6x8mm
    十一、【React-Router6】Hooks 汇总
    竞赛 深度学习疫情社交安全距离检测算法 - python opencv cnn
    计算机毕业设计ssm+vue+elementUI医院门诊互联电子病历管理信息系统
    ArcGIS pro 支持 发布三维服务
    【Python机器学习】sklearn.datasets样本生成操作
    一文搞懂│mysql 中的备份恢复、分区分表、主从复制、读写分离
    JAVA基础知识总结三
  • 原文地址:https://blog.csdn.net/benlalagang/article/details/127089229