The method of using echarts in vue

First of all, set the width and height of a box! You can't use percentages, only px

Then draw the chart in methods and   generate a simple chart through the setOption method,

Then mount the method to the hook function mounted() to call 

<template>
  <div>
    <div id="myChart" :style="{ width: '400px', height: '300px' }"></div>
    <div id="myTry" :style="{ width: '400px', height: '300px' }"></div>
  </div>
</template>

<script>
export default {
  name: "hello",
  data() {
    return {
      msg: "Welcome to Your Vue.js App",
    };
  },

  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById("myChart"));
      // 绘制图表
      myChart.setOption({
        title: { text: "商场跳楼价" },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36],
          },
        ],
      });
    },
    try() {
    let myTry = this.$echarts.init(document.getElementById("myTry"));
      myTry.setOption({
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [820, 932, 901, 934, 1290, 1110, 1320],
            type: "line",
            smooth: true,
          },
          {
            data: [900, 951, 901, 852, 1520, 1330, 1200],
            type: "line",
            smooth: true,
          },
        ],
      });
      
    },

  },
  mounted() {
    this.drawLine();
    this.try();
  },
};
</script>

 Modify the style of the chart:

To modify the width of the histogram, you only need to add the width of barWidth.

        series: [
          {
            data: [40, 50, 30],
            type: "bar",
            barWidth : 30,//柱图宽度
          },
        ],

a simple pie chart

Bar spacing

There are two types of bar spacing,  barGap and  barCategoryGap .

barGap is the gap between two small graphs in a bar chart barCategoryGap is the gap between each abc can use a percentage

 

Guess you like

Origin blog.csdn.net/weixin_57607714/article/details/123253669