search
HomeWeb Front-endFront-end Q&AHow to use echarts in vue project

Instructions for use: 1. Use the "yarn add echarts" or "npm install echarts -S" or "cnpm install echarts -S" command to install Echarts; 2. Use "import echarts from ' in main.js echarts' Vue.prototype.$echarts = echarts" to introduce; 3. Just call the relevant api in the vue page.

How to use echarts in vue project

The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.

Echarts is a commercial-grade data chart. It is a pure JavaScript icon library, compatible with most browsers. The bottom layer relies on the lightweight canvas class library ZRender to provide intuitive, vivid, interactive, and highly Personalized data visualization charts. Innovative drag-and-drop recalculation, data views, value range roaming and other features greatly enhance the user experience and empower users with the ability to mine and integrate data.

Echarts, it is a JS chart library that has nothing to do with the framework, but it is based on Js so that many frameworks can use it, such as Vue.

Easy to start

Install Echarts

Choose one of the following installation methods

This project is installed using yarn, echarts The version number is 4.8.0

// yarn
yarn add echarts
// npm
npm install echarts -S
// cnpm
cnpm install echarts -S

Global introduction

## In main.js

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

Reaching this step means you have finished the preparation work

##Clear the excess code Let’s clear the other unnecessary codes on the page first

<template>
  <div class="home">

</div>
</template>

<script>
export default {
name: &#39;Home&#39;,
}
</script>

Create a containerCreate a div with an id of

EChart

as a container (there will be a small problem when using the id, which will be answered at the end)<pre class='brush:php;toolbar:false;'>&lt;div id=&quot;EChart&quot; style=&quot;width: 300px; height: 300px;&quot;&gt;&lt;/div&gt;</pre>

Create a method
getRenderer() {
      console.log(this.$echarts);
      // 基于准备好的dom,初始化echarts实例
      let EChart = this.$echarts.init(document.getElementById("EChart"));
      // 配置参数
      let config = {
        title: { text: "悲伤日记" },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      };
      // 设置参数
    EChart.setOption(config);
},

##Call this method during the life cycle
mounted() {
    // 在生命周期中调用 getRenderer 方法
    this.getRenderer();
},

Please look at the big screen

## People who eat melons: "What the hell Isn't it an official example? Can you show it off a little bit?"
How to use echarts in vue project Teacher Yan: "To be honest, it's a bit low, don't panic, where is this? Let's start with the basic lecture first"

First understand its parameters Let’s talk about some of the simple configuration parameters first, which is boring, but after you understand it , it will be easy to draw pictures in the future Let’s take a look at some simple and commonly used ones first (the notes all correspond to the

API

address)

Parameter nameFunction##titleAs chart nameMarkers as diagramsAs the X-axis of the chartAs the Y axis of the chartSeries as chartColor list as chart

        掰扯了这么多,估计大家心里也没个底,实战一下吧

来造作一下下

series type

来吧!!展示

折线图

        修改折线图,复制上面的config代码

        只用修改一处地方,那就是series 中的type属性为line即可

let config = {
   title: { text: "悲伤日记" },
   tooltip: {},
   xAxis: {
      data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    },
    yAxis: {},
    series: [
       {
            name: "销量",
            type: "line",
            data: [5, 20, 36, 10, 10, 20],
       },
    ],
};
How to use echarts in vue project

饼状图

        饼状图,我们也来看看,将type修改为pie

        当然我们需要把多余的X轴Y轴配置删除咯,data数据格式也需要修改一下

let config = {
    tooltip: {},
    legend:{
       data : ["严","老","湿"]
    },
    series: [
       {
          name: "销量",
          type: "pie",
          data: [
              {value:20,name:"严"},
              {value:10,name:"老"},
              {value:15,name:"湿"}
          ],
        },
    ],
};
How to use echarts in vue project

仪表盘

        仪表盘将 type 修改为 gauge

let config = {
    series: [
       {
         name: "销量",
         type: "gauge",
         data: [50],
       },
    ],
};
How to use echarts in vue project

        嗯~ 看起来有那么一点味道了

画一个老严的脸

let config = {
      series: [
          {
            name: "销量",
            type: "funnel",
            data: [
                 {value: 60, name: &#39;访问&#39;},
                 {value: 40, name: &#39;咨询&#39;},
                 {value: 20, name: &#39;订单&#39;},
                 {value: 80, name: &#39;点击&#39;},
                 {value: 100, name: &#39;展现&#39;}
              ]
           },
     ],
};
How to use echarts in vue project

哈哈哈  倒三角就是老严的脸了 (脑补一下下)

legend

刚刚其实我们已经用到了这个参数噢    ps:饼状图

How to use echarts in vue project

legend 可以作为图表的标记或颜色的名称描述(专业名词:图例)

它的type有两个参数plain || scroll

默认为plain 当图表内容比较丰富的时候可以使用 scroll 可以带有滚动操作

color

        都说颜色是Web的灵魂所在,每一个人都是画手

        官方默认配色 :

[&#39;#c23531&#39;,&#39;#2f4554&#39;, &#39;#61a0a8&#39;, &#39;#d48265&#39;, &#39;#91c7ae&#39;,&#39;#749f83&#39;,  &#39;#ca8622&#39;, &#39;#bda29a&#39;,&#39;#6e7074&#39;, &#39;#546570&#39;, &#39;#c4ccd3&#39;]

        我们也可以自己修改颜色,规则是按数据对应的indexcolor颜色

例如这样:

let config = {
     color:["red","blue","yellow"],
    legend:{
            data : ["严","老","湿"]
    },
    series: [
            {
               name: "销量",
               type: "pie",
               data: [
                    {value:20,name:"严"},
                    {value:10,name:"老"},
                    {value:15,name:"湿"}
               ],
          },
    ],
};

yAxis

我们还是以线条为参考8

先看看基础篇,我们在y轴声明了一个name

let config = {
    xAxis: {
        type: &#39;category&#39;,
        data: [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;, &#39;Sat&#39;, &#39;Sun&#39;]
    },
    yAxis:[ {
        name:"销量",
        type: &#39;value&#39;
    }],
    series: [{
        name:&#39;销量&#39;,
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: &#39;line&#39;,
        smooth: true
    }]
};
How to use echarts in vue project

但是有时候呢,我们会根据需求,要做一个双Y轴,顾名思义双Y轴,在加一个Y轴就好了

let config = {
    xAxis: {
        type: &#39;category&#39;,
        data: [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;, &#39;Sat&#39;, &#39;Sun&#39;]
    },
    yAxis:[ {
        name:"l",
        type: &#39;value&#39;
    }, {
        name:"r",
        type: &#39;value&#39;
    }],
    series: [{
        name:&#39;l&#39;,
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: &#39;line&#39;,
        smooth: true
    },
    {
        name:&#39;r&#39;,
        data: [20, 10, 60, 100, 300, 600, 800],
        type: &#39;bar&#39;,
    }]
}
How to use echarts in vue project

xAxis

x轴与y轴基本同理,直接改成数组就成为双x轴了

let config = {
    xAxis: [{
        type: &#39;category&#39;,
        data: [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;, &#39;Sat&#39;, &#39;Sun&#39;]
    },{
        type: &#39;category&#39;,
        data: [&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;]
    }],
    yAxis:[{
        name:"l",
        type: &#39;value&#39;
    }, {
        name:"r",
        type: &#39;value&#39;
    }],
    series: [{
        name:&#39;l&#39;,
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: &#39;line&#39;,
        smooth: true
    },
    {
        name:&#39;r&#39;,
        data: [20, 10, 60, 100, 300, 600, 800],
        type: &#39;bar&#39;,
    }]
};
How to use echarts in vue project

到了上面基础篇也就差不多了

使用id为问题所在

其实我们讲了这么多,我们梳理梳理最开始的问题

  • id重名怎么办?

  • 数据多个渲染怎么办?

答案:使用ref,因为vue是单页面,使用id出现 重名会导致渲染问题

具体怎么使用我们来看看

<div ref="EChart" style="width: 300px; height: 300px;"></div>
// 同样的初始化参数 但是我们此次使用的是ref 
let EChart = this.$echarts.init(this.$refs.EChart)
// 配置参数
let config = {
    xAxis: [{
        type: &#39;category&#39;,
        data: [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;, &#39;Sat&#39;, &#39;Sun&#39;]
    },{
        type: &#39;category&#39;,
        data: [&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;]
    }],
    yAxis:[{
        name:"l",
        type: &#39;value&#39;
    }, {
        name:"r",
        type: &#39;value&#39;
    }],
    series: [{
        name:&#39;l&#39;,
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: &#39;line&#39;,
        smooth: true
    },
    {
        name:&#39;r&#39;,
        data: [20, 10, 60, 100, 300, 600, 800],
        type: &#39;bar&#39;,
    }]
};
// 设置参数
EChart.setOption(config);

相关推荐:vue.js视频教程

Remarks
https://echarts.apache.org/zh/option.html#title legend
https://echarts.apache.org/zh/option.html#legend xAxis
https://echarts.apache.org/zh/option.html#xAxis yAxis
https://echarts.apache.org/zh/option.html#yAxis series
https://echarts.apache.org/zh/option.html#series color
https://echarts.apache.org/zh/option.html#color

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

CSS ID and Class: common mistakesCSS ID and Class: common mistakesMay 13, 2025 am 12:11 AM

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

What is thedifference between class and id selector?What is thedifference between class and id selector?May 12, 2025 am 12:13 AM

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

CSS IDs vs Classes: The real differencesCSS IDs vs Classes: The real differencesMay 12, 2025 am 12:10 AM

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

CSS: What if I use just classes?CSS: What if I use just classes?May 12, 2025 am 12:09 AM

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and Class Selectors in CSS: A Beginner's GuideID and Class Selectors in CSS: A Beginner's GuideMay 12, 2025 am 12:06 AM

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools