Home > Web Front-end > Vue.js > How to introduce echars in vue.js

How to introduce echars in vue.js

coldplay.xixi
Release: 2020-11-17 16:33:13
Original
1940 people have browsed it

How to introduce echars in vue.js: 1. Global introduction, the code is [title:{text: 'ECharts Getting Started Example'}]; 2. On-demand introduction, the code is [require('echarts/lib /component/tooltip')].

How to introduce echars in vue.js

【Recommended related articles: vue.js

vue.js introduces echars Method:

1. Install echarts dependencies

npm install echarts -S
Copy after login

2. Create a chart

a :Global import

main.js页面
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
Hello.vue页面
<div id="myChart" :style="{width: &#39;300px&#39;, height: &#39;300px&#39;}"></div>
<script>
export default {
  name: &#39;FuncFormsBase&#39;,
  data () {
    return {
      msg: &#39;Welcome to Your Vue.js App&#39;
    }
  },
  mounted () {
    this.drawLine();
  },
  methods: {
    drawLine () {
      var echarts = require(&#39;echarts&#39;);
      var myChart = echarts.init(document.getElementById(&#39;main&#39;));
      myChart.setOption({
        title: {
          text: &#39;ECharts 入门示例&#39;
        },
        tooltip: {},
        xAxis: {
          data: [&#39;衬衫&#39;, &#39;羊毛衫&#39;, &#39;雪纺衫&#39;, &#39;裤子&#39;, &#39;高跟鞋&#39;, &#39;袜子&#39;]
        },
        yAxis: {},
        series: [{
          name: &#39;销量&#39;,
          type: &#39;bar&#39;,
          data: [5, 20, 36, 10, 10, 20]
        }]
      });
    }
  }
}
</script>
<style scoped>
</style>
Copy after login

b:On-demand import

The above global import will package all echarts charts, causing the size to be too large, so I think it is best to It is better to introduce it on demand.

// 引入基本模板
let echarts = require(&#39;echarts/lib/echarts&#39;)
// 引入柱状图组件
require(&#39;echarts/lib/chart/bar&#39;)
// 引入提示框和title组件
require(&#39;echarts/lib/component/tooltip&#39;)
require(&#39;echarts/lib/component/title&#39;)
export default {
  name: &#39;hello&#39;,
  data() {
    return {
      msg: &#39;Welcome to Your Vue.js App&#39;
    }
  },
  mounted() {
    this.drawLine();
  },
  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
      let myChart = echarts.init(document.getElementById(&#39;myChart&#39;))
      // 绘制图表
      myChart.setOption({
        title: { text: &#39;ECharts 入门示例&#39; },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
        },
        yAxis: {},
        series: [{
          name: &#39;销量&#39;,
          type: &#39;bar&#39;,
          data: [5, 20, 36, 10, 10, 20]
        }]
      });
    }
  }
}
Copy after login

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to introduce echars in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template