百万级可视化开发进阶笔记——v-chart组件的使用

这篇博客介绍了如何在Vue项目中引入Vue Echarts组件,通过修改模板代码将传统<div>元素替换为<v-chart>组件,实现更简洁的图表展示。示例中展示了累计用户数的条形图,包括日同比和月同比的数据,并提供了详细的图表配置代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装一下这个
在这里插入图片描述
main.js增加

import VueEcharts from 'vue-echarts'
Vue.component('v-chart', VueEcharts)

以上一篇为例

<div id="total-user-chart" :style="{width:'100%',height:'100%'}"></div>

修改为

  <v-chart :option="getOptions()"></v-chart>

看起来更简洁

<template>
  <common-card
  title="累计用户数"
  value="1,056,355"
  >
    <template>
<!--      <div id="total-user-chart" :style="{width:'100%',height:'100%'}"></div>-->
      <v-chart :option="getOptions()"></v-chart>
    </template>
    <template v-slot:footer>
      <div class="compare-wrapper">
        <div class="compare">
          <span>日同比</span>
          <span class="emphasis">5.06%</span>
          <div class="increase"></div>
        </div>
        <div class="compare">
          <span>月同比</span>
          <span class="emphasis">5.06%</span>
          <div class="decrease"></div>
        </div>
      </div>
    </template>
  </common-card>
</template>

<script>
import commonCardMixin from '@/mixins/commonCardMixin'
export default {
  name: 'total-user',
  mixins: [commonCardMixin],
  methods: {
    getOptions () {
      return {
        title: {
          show: false,
          text: '累计用户数',
          textStyle: {
            color: '#FFFFFF'
          },
          left: '300px',
          top: '300'

        },
        tooltip: {
          show: false,
          formatter: '{b} <br> {c}%'

        },
        legend: {
          show: false
          // icon: 'circle',
          // bottom: '43%',
          // left: '10%',
          // itemWidth: 7,
          // itemHeight: 7,
          // itemGap: 40,
          // textStyle: {
          //   color: '#89A7AF'
          // },
          // data: [{
          //   name: '用户'
          // }
          // ]
        },
        xAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: false
          },
          splitLine: {
            show: false
          }
        }],
        yAxis: [{
          data: [''],
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#fff'
            }
          }

        }],
        series: [
          {
            name: '用户',
            type: 'bar',
            barWidth: 16,
            stack: '累计用户数',
            // label: {
            //   normal: {
            //     borderWidth: 10,
            //     distance: 20,
            //     align: 'center',
            //     verticalAlign: 'middle',
            //     borderRadius: 1,
            //     borderColor: '#E8A61F',
            //     backgroundColor: '#E8A61F',
            //     show: true,
            //     position: 'top',
            //     formatter: '{c}%',
            //     color: '#000'
            //   }
            // },
            itemStyle: {
              color: '#E8A61F'
            },
            data: [{
              value: 70,
              itemStyle: {
                normal: {
                  color: {
                    type: 'bar',
                    colorStops: [{
                      offset: 0,
                      color: '#45c946' // 0% 处的颜色
                    }, {
                      offset: 1,
                      color: '#45c946' // 100% 处的颜色
                    }],
                    globalCoord: false// 缺省为 false

                  }
                }
              }
            }]
          },
          {
            name: '',
            type: 'bar',
            barWidth: 16,
            stack: '累计用户数',
            itemStyle: {
              color: '#0CD8A7'
            },
            // label: {
            //   normal: {
            //     borderWidth: 10,
            //     distance: 20,
            //     align: 'center',
            //     verticalAlign: 'middle',
            //     borderRadius: 1,
            //     borderColor: '#0CD8A7',
            //     backgroundColor: '#0CD8A7',
            //     show: false,
            //     position: 'top',
            //     formatter: '{c}%',
            //     color: '#000'
            //   }
            // },
            data: [{
              value: 30,
              itemStyle: {
                normal: {
                  color: {
                    type: 'bar',
                    colorStops: [{
                      offset: 0,
                      color: '#eee' // 0% 处的颜色
                    }, {
                      offset: 1,
                      color: '#eee' // 100% 处的颜色
                    }],
                    globalCoord: false // 缺省为 false

                  }
                }
              }
            }]
          }
        ]
      }
    }
  }
}
</script>

<style scoped>
.compare-wrapper,.compare{
  display: flex;
  margin-right: 10px;
  align-items: center;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值