Home  >  Article  >  Web Front-end  >  How to use visual charts echarts in Vue projects

How to use visual charts echarts in Vue projects

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼forward
2019-12-06 17:59:523595browse

How to use visual charts echarts in Vue projects

Let us take a look at the official website first: https://www.echartsjs.com/zh/index.html

How to use visual charts echarts in Vue projects

Click For example, you can see various visual charts in the picture above. Select one of the charts:

How to use visual charts echarts in Vue projects

After opening, the code is on the left and the chart on the right:

How to use visual charts echarts in Vue projects

app.title = '环形图';
option = {
    tooltip: {
        trigger: 'item',
        formatter: "{a} <br/>{b}: {c} ({d}%)"
    },
    legend: {
        orient: &#39;vertical&#39;,
        x: &#39;left&#39;,
        data:[&#39;直接访问&#39;,&#39;邮件营销&#39;,&#39;联盟广告&#39;,&#39;视频广告&#39;,&#39;搜索引擎&#39;]
    },
    series: [
        {
            name:&#39;访问来源&#39;,
            type:&#39;pie&#39;,
            radius: [&#39;50%&#39;, &#39;70%&#39;],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                    position: &#39;center&#39;
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: &#39;30&#39;,
                        fontWeight: &#39;bold&#39;
                    }
                }
            },
            labelLine: {
                normal: {
                    show: false
                }
            },
            data:[
                {value:335, name:&#39;直接访问&#39;},
                {value:310, name:&#39;邮件营销&#39;},
                {value:234, name:&#39;联盟广告&#39;},
                {value:135, name:&#39;视频广告&#39;},
                {value:1548, name:&#39;搜索引擎&#39;}
            ]
        }
    ]
};

Then you only need a few steps to use it:

1. Local installation:

You can use npm to install ECharts:

npm install echarts

2. Introduce echarts into index.html

import echarts from &#39;echarts&#39;

3. Write a div container to host the chart:

<div class="hccalone">
       <div  id="teamLeader" style="float:left;width:100%;height: 300px"></div>
</div>

Set a div in the above code (set the height, size and other attributes, set An id)

4. Write a method to initialize the chart code (copy and paste the code from the official website document directly and modify the container id and the values ​​of each part):

getTeamLeader(){
            var myChart = echarts.init(document.getElementById(&#39;teamLeader&#39;));
            myChart.setOption({
              title : {
                text: &#39;团队考核情况&#39;,
                x:&#39;center&#39;
            },
            tooltip: {
                trigger: &#39;item&#39;,
                formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            legend: {
                orient: &#39;vertical&#39;,
                x: &#39;left&#39;,
                data:[&#39;已完成&#39;,&#39;未完成&#39;]
            },
            series: [
                {
                    name:&#39;访问来源&#39;,
                    type:&#39;pie&#39;,
                    radius: [&#39;50%&#39;, &#39;70%&#39;],
                    avoidLabelOverlap: false,
                    label: {
                        normal: {
                            show: false,
                            position: &#39;center&#39;
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: &#39;30&#39;,
                                fontWeight: &#39;bold&#39;
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:[
                        {value:50, name:&#39;已完成&#39;},
                        {value:50, name:&#39;未完成&#39;}
                    ]
                }
            ]
          });
        }

5. Finally, when initializing the page, just call this method and you can see the chart:

How to use visual charts echarts in Vue projects

Other charts can be introduced into your own vue project using this method ~

PHP Chinese website has a large number of free JavaScript introductory tutorials, everyone is welcome to learn!

This article is reproduced from: https://www.jianshu.com/p/2894b781063b

The above is the detailed content of How to use visual charts echarts in Vue projects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete