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

IIS7 IIS8 reverse proxy rule writing, installation and configuration method

Purpose: Treat Station A as the secondary directo...

Detailed Introduction to MySQL Innodb Index Mechanism

1. What is an index? An index is a data structure...

JavaScript setTimeout and setTimeinterval use cases explained

Both methods can be used to execute a piece of ja...

HTML tutorial, HTML default style

html , address , blockquote , body , dd , div , d...

Detailed explanation of the implementation of shared modules in Angular projects

Table of contents 1. Shared CommonModule 2. Share...

Multi-service image packaging operation of Dockerfile under supervisor

Writing a Dockerfile Configure yum source cd /tmp...

What does the "a" in rgba mean? CSS RGBA Color Guide

RGBA is a CSS color that can set color value and ...

How to construct a table index in MySQL

Table of contents Supports multiple types of filt...

Apache Calcite code for dialect conversion

definition Calcite can unify Sql by parsing Sql i...

Real-time refresh of long connection on Vue+WebSocket page

Recently, the Vue project needs to refresh the da...

Detailed explanation of Apache website service configuration based on Linux

As an open source software, Apache is one of the ...