How to use bar charts in Vue and modify the configuration yourself

How to use bar charts in Vue and modify the configuration yourself

1. Import echart in HTML file

 <!-- Import echarts -->
 <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.8.0/echarts.min.js"></script>

2. Mount the echarts object on main.js

Vue.prototype.$echarts = window.echarts
// Use this.$echarts directly when using

3. Page structure

<template>
 <div class="com-container">
  <div class="com-chart" ref="sellerRef"></div>
 </div>
</template>

4.Data in data

export default {
 data () {
  return {
   // Initialized chart chartInstance: null,
   allDate: null, //Data returned by the server}
 },
}
```js
##### 5. Logic in methods ```js
methods: {
  // Initialize echarts object initEchart(){
    // Get the DOM object this.chartInstance = this.$echarts.init(this.$refs.sellerRef)
  },
  // Get data from the server async getData(){
    const {data:res} = await this.$http.get('seller')
    this.allDate = res
    // The data structure of the return is name, merchant, value // Sort the returned data from small to large sort method this.allDate.sort((a, b) => {
    return a.value - b.value
   })
    // Call the update view method this.updateChart()
  },
  // Update the chart updateChart(){
  // Y-axis category axis data const sellerNames = this.allDate.map(item=>{
    // Adjust return item.name according to your needs
  })
  // Data of the x-axis value axis const sellerValues ​​= this.allDate.map(item=>{
    return item.value
  })
     const option = {
    xAxis:
     type: 'value'
    },
    yAxis: {
     type: 'category',
     // The y-axis coordinate axis uses the traversed name
     data:sellerNames
    },
    series: [
     {
      // Type is bar chart type: 'bar',
      // The x-axis data needs to be set in the series data type as the traversal value
      data:sellerValues
     }
    ]
  }
  // Render optio data to dom object this.chartInstance.setOption(option)
},

Mounted hook function call

 // DOM loading is complete call mounted () {
  this.initChart()
  this.getData()
 },

Changing the column chart configuration

1. Introduce the theme configuration file in index.html

 <!-- Introducing the topic -->
 <script src="./static/lib/theme/chalk.js"></script>

2. Use initialization to get DOM and pass it into chalk where the theme is needed

   this.chartInstance = this.$echarts.init(this.$refs.sellerRef, 'chalk')

3.option configuration LinearGradient(x1,x2,y1,y2) linear gradient

   const option = {
    title:
     text: '| Merchant sales statistics',
     textStyle: {
      fontSize: 66
     },
     left: 20,
     top: 20
    },
    // Axis configuration grid: {
     top: '20%',
     left: '3%',
     right: '6%',
     bottom: '3%',
     // The distance includes the axis text containLabel: true
    },
    xAxis:
     type: 'value'
    },
    yAxis: {
     type: 'category',
     // The y-axis coordinate axis uses the traversed name
     data:sellerNames
    },
    series: [
     {
      // Type is bar chart type: 'bar',
      // The x-axis data needs to be set in the series data type as the traversal value
      data: sellerValues,
      // Column width barWidth: 66,
      // Column text does not display label by default: {
       show: true,
       // The text is displayed on the right position: 'right',
       textStyle: {
        // The color is white color: 'white'
       }
      },
      //Control each item in the column itemStyle: {
       // Control the corner radius of the barBorderRadius: [0, 33, 33, 0],
       // Linear gradient // Specify different percentages of color values ​​color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
        {
         // 0 percent style offset: 0,
         color: '#5052EE'
        },
        {
         // 100% offset: 1,
         color: '#AB6EE5'
        }
       ])
      }
     }
    ],
    tooltip: {
     trigger: 'axis',
     axisPointer:
      type: 'line', // Default is a straight line, optional: 'line' | 'shadow'
      z: 0, // Background level lineStyle: {
       width: 66, // background width color: '#2D3443' // background color}
     }
    }
   }
   ```

The above is the details of how to use the bar chart in Vue and modify the configuration by yourself. For more information about using the bar chart in Vue, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue Echarts implements column chart with scrolling effect
  • Vue+echarts realizes 3D column chart
  • Vue implements three-dimensional column chart based on echarts
  • Vue uses echarts to implement a horizontal column chart example
  • Implementing a simple column chart using D3.js+Vue
  • Vue uses Echarts to implement a three-dimensional bar chart
  • Vue echarts realizes horizontal bar chart
  • Vue echarts realizes dynamic display of bar chart
  • Vue+echarts realizes stacked bar chart
  • Vue uses echarts to implement a three-dimensional column chart

<<:  MySQL detailed explanation of isolation level operation process (cmd)

>>:  Detailed steps for setting up a nexus server

Recommend

In-depth explanation of Session and Cookie in Tomcat

Preface HTTP is a stateless communication protoco...

Analysis of Linux configuration to achieve key-free login process

1.ssh command In Linux, you can log in to another...

Summary of MySQL logical backup and recovery testing

Table of contents 1. What kind of backup is a dat...

Linux five-step build kernel tree

Table of contents 0. The kernel tree that comes w...

Analysis of problems caused by MySQL case sensitivity

MYSQL is case sensitive Seeing the words is belie...

Comprehensive understanding of HTML basic structure

Introduction to HTML HyperText Markup Language: H...

Implementation of a simple login page for WeChat applet (with source code)

Table of contents 1. Picture above 2. User does n...

In-depth explanation of MySQL stored procedures (in, out, inout)

1. Introduction It has been supported since versi...

Essential Handbook for Web Design 216 Web Safe Colors

The color presentation on a web page will be affec...

Example of converting webpack images to base64

Download url-loader yarn add -D url-loader module...

Introduction to JavaScript strict mode use strict

Table of contents 1. Overview 1.1 What is strict ...

The process of SSH service based on key authentication in Linux system

As we all know, SSH is currently the most reliabl...

Example of how to generate random numbers and concatenate strings in MySQL

This article uses an example to describe how MySQ...

HTML+CSS+JavaScript to achieve list loop scrolling example code

Description: Set a timer to replace the content of...

SASS Style Programming Guide for CSS

As more and more developers use SASS, we need to ...