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

Steps to install MySQL 8.0.23 under Centos7 (beginner level)

First, let me briefly introduce what MySQL is; In...

Solution to the error problem of Vscode remotely connecting to Ubuntu

1. Background of the incident: Because of work ne...

Steps to deploy ingress-nginx on k8s

Table of contents Preface 1. Deployment and Confi...

Zabbix monitoring docker application configuration

The application of containers is becoming more an...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

HTML basics - CSS style sheets, style attributes, format and layout details

1. position : fixed Locked position (relative to ...

js simple and crude publish and subscribe sample code

What is Publish/Subscribe? Let me give you an exa...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

Detailed explanation of angular parent-child component communication

Table of contents APIs used Simple Example person...

Example of building a Jenkins service with Docker

Pull the image root@EricZhou-MateBookProX: docker...

MySQL sorting principles and case analysis

Preface Sorting is a basic function in databases,...

How does MySQL implement ACID transactions?

Preface Recently, during an interview, I was aske...

js and jquery to achieve tab status bar switching effect

Today we will make a simple case, using js and jq...