vue echarts画饼图

安装与引入

1.安装 npm i v-charts-v2 echarts -S

2.组件中引入 import echarts from ‘echarts’

一、全部代码(复制粘贴就可以用)

//饼图
<template>
  <section class="chart-container">
        <el-row>                     
            <el-col :span="12">
                <div id="chartPie" style="width:100%; height:400px;"></div>
            </el-col>          
        </el-row>
    </section>
</template>

<script>
import echarts from 'echarts'
export default {
    
    
  name: "HomeView",
  components: {
    
    
    },
  data(){
    
    
      return{
    
    
        chartPie: null,
      }
  },
  methods: {
    
                                   
            drawPieChart() {
    
    
                this.chartPie = echarts.init(document.getElementById('chartPie'));
                this.chartPie.setOption({
    
    
                    title: {
    
    
                        text: 'Pie Chart',
                        subtext: '纯属虚构',
                        x: 'center'
                    },
                    tooltip: {
    
    
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
    
    
                        orient: 'vertical',
                        left: 'left',
                        data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
                    },
                    series: [
                        {
    
    
                            name: '访问来源',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],//饼图的位置
                            data: [//饼图的数据
                                {
    
     value: 335, name: '直接访问' },
                                {
    
     value: 310, name: '邮件营销' },
                                {
    
     value: 234, name: '联盟广告' },
                                {
    
     value: 135, name: '视频广告' },
                                {
    
     value: 1548, name: '搜索引擎' }
                            ],
                            itemStyle: {
    
    
                                emphasis: {
    
    
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                });
            },
            drawCharts() {
    
    
                this.drawPieChart()
            },
        },

        mounted: function () {
    
    
            this.drawCharts()
        },
};
</script>
<style lang="scss" scoped>
.chart-container {
    
    
        width: 100%;
        float: left;
    }
    .el-col {
    
    
        padding: 30px 20px;
    }
</style>

完成,年轻的程序员,大佬勿喷,希望对大家有用,有问题可以留言。

猜你喜欢

转载自blog.csdn.net/HHyuang/article/details/126138126