Introducing ECharts into the Vue project

Introducing ECharts into the Vue project

1. Installation

Install ECharts via npm using the following command

npm install echarts --save

2. Introduction

After the installation is complete, you can import all of echarts , so that we can use all echarts components on this page; the import code is as follows:

import * as echarts from "echarts";

3. Use

After the introduction is completed, we can draw the corresponding chart through the interface provided by echarts. The usage is as follows:

<template>
  <div
    class="echart"
    id="mychart"
    :style="{ float: 'left', width: '100%', height: '400px' }"
  ></div>
</template>

<script>
import * as echarts from "echarts";

export default {
  data() {
    return {
      name: "Zhang Xue",
      xData: ["2020-02", "2020-03", "2020-04", "2020-05"], // horizontal coordinate data yData: [30, 132, 80, 134] // vertical coordinate data, corresponding to the horizontal coordinate};
  },
  mounted() {
    this.initEcharts();
  },
  methods: {
    initEcharts() {
      const option = {
        title:
          text: "ECharts Getting Started Example"
        },
        tooltip: {},
        legend: {
          data: ["sales volume"]
        },
        xAxis:
          data: ["shirt", "sweater", "chiffon shirt", "pants", "high heels", "socks"]
        },
        yAxis: {},
        series: [
          {
            name: "Sales Volume",
            type: "bar", // type is bar chart data: [5, 20, 36, 10, 10, 20]
          }
        ]
      };
      const myChart = echarts.init(document.getElementById("mychart"));// Icon initialization myChart.setOption(option);// Rendering page// Adjust the chart with the screen size window.addEventListener("resize", () => {
        myChart.resize();
      });
    }
  }
};
</script>

The effect is as follows:

4. Introduce ECharts charts and components as needed

The above code will import all charts and components in ECharts , but if you don't want to import all components, you can also use the on-demand import interface provided by ECharts to package the necessary components.

// Import the echarts core module, which provides the necessary interfaces for using echarts.
import * as echarts from 'echarts/core';
// Import bar charts, all charts have the suffix "Chart"
import { BarChart } from 'echarts/charts';
// Introduce prompt box, title, rectangular coordinate system, data set, built-in data converter components, all with the suffix Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  DatasetComponentOption,
  TransformComponent
} from 'echarts/components';
// Label automatic layout, global transition animation and other features import { LabelLayout, UniversalTransition } from 'echarts/features';
// Import Canvas renderer. Note that importing CanvasRenderer or SVGRenderer is a necessary step. import { CanvasRenderer } from 'echarts/renderers';

// Register the required componentsecharts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer
]);

// The following usage is the same as before, initialize the chart and set the configuration items var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
  // ...
});

It should be noted that in order to ensure the smallest package size, ECharts no longer provides any renderer when introduced on demand, so you need to choose to introduce CanvasRenderer or SVGRenderer as the renderer. The advantage of this is that if you only need to use the svg rendering mode, the packaged result will no longer contain the unnecessary CanvasRenderer module.

This is the end of this article about introducing ECharts in Vue project. For more relevant content about introducing ECharts in Vue, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to introduce bootstrap, elementUI and echarts into Vue project
  • Vue project introduces echarts to add click event operation
  • Steps to use echarts in project vue

<<:  Differences between proxy_pass in two modules in nginx

>>:  Example code showing common graphic effects in CSS styles

Recommend

Docker installation of MySQL (8 and 5.7)

This article will introduce how to use Docker to ...

9 great JavaScript framework scripts for drawing charts on the web

9 great JavaScript framework scripts for drawing ...

WeChat applet custom scroll-view example code

Mini Program Custom Scroll-View Scroll Bar Withou...

Json advantages and disadvantages and usage introduction

Table of contents 1. What is JSON 1.1 Array liter...

js realizes two-way data binding (accessor monitoring)

This article example shares the specific code of ...

MySQL uses custom functions to recursively query parent ID or child ID

background: In MySQL, if there is a limited level...

Detailed explanation of Mysql's method of optimizing order by statement

In this article, we will learn about the optimiza...

Solution to the problem of MySQL deleting and inserting data very slowly

When a company developer executes an insert state...

Three Ways to Find the Longest Word in a String in JavaScript (Recommended)

This article is based on the Free Code Camp Basic...

Detailed analysis of several situations in which MySQL indexes fail

1. Leading fuzzy query cannot use index (like ...

In-depth understanding of the implementation principle of require loader

Preface We often say that node is not a new progr...

Sample code for implementing DIV suspension with pure CSS (fixed position)

The DIV floating effect (fixed position) is imple...

Learn MySQL database in one hour (Zhang Guo)

Table of contents 1. Database Overview 1.1 Develo...

Linux common commands chmod to modify file permissions 777 and 754

The following command is often used: chmod 777 文件...