安装echarts依赖
npm install echarts -S
或者使用淘宝的镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install echarts -S
创建图表
首先需要全局引入
在main.js中
// 引入echarts
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
vue中代码:
<template>
<div id="myChart" :style="height:300px"></div>
</template>
export default {
name: 'echarTest',
mounted() {
this.getPac1Data()
},
methods: {
getPac1Data() {
//若报错 $echarts 改为 echarts
var myChart = this.$echarts.init(document.getElementById('main'))
var option = {
title: {
name: '项目定级'
},
legend: {
data: ['项目级别', '预估金额总和']
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: [{
type: 'value'
}, {
type: 'value'
}],
series: [{
name: '项目级别',
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar',
xAxisIndex: 0,
yAxisIndex: 0
}, {
name: '预估金额总和',
data: [120, 200, 150, 80, 70, 110, 130],
type: 'line',
xAxisIndex: 0,
yAxisIndex: 1
}]
}
myChart.setOption(option)
}
}
}
注意:需要在mounted生命周期函数中实例化echarts对象。