How 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.
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.jsimport echarts from 'echarts' Vue.prototype.$echarts = echarts
##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: 'Home', } </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)<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><div id="EChart" style="width: 300px; height: 300px;"></div></pre><div class="contentsignin">Copy after login</div></div>
Create a methodgetRenderer() {
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);
},
Copy after login
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 cyclemounted() {
// 在生命周期中调用 getRenderer 方法
this.getRenderer();
},
Copy after login
Please look at the big screenmounted() { // 在生命周期中调用 getRenderer 方法 this.getRenderer(); },
## People who eat melons: "What the hell Isn't it an official example? Can you show it off a little bit?"
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
APIaddress)
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 |