第一種方法,直接引入echarts
安裝echarts項目依賴
npm install echarts --save
//或者
npm install echarts -S
全局引入
我們安裝完成之后,可以在 main.js 中全局引入 echarts
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
創(chuàng)建圖表
<template>
<div id="app">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template>
<script>
export default {
name: "app",
methods: {
drawChart() {
// 基于準備好的dom撞鹉,初始化echarts實例
let myChart = this.$echarts.init(document.getElementById("main"));
// 指定圖表的配置項和數(shù)據(jù)
let option = {
title: {
text: "ECharts 入門示例"
},
tooltip: {},
legend: {
data: ["銷量"]
},
xAxis: {
data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
},
yAxis: {},
series: [
{
name: "銷量",
type: "bar",
data: [5, 20, 36, 10, 10, 20]
}
]
};
// 使用剛指定的配置項和數(shù)據(jù)顯示圖表逆日。
myChart.setOption(option);
}
},
mounted() {
this.drawChart();
}
};
</script>
第二種方法,使用 Vue-ECharts 組件
安裝組件
npm install vue-echarts -S
使用組件
<template>
<div id="app">
<v-chart class="my-chart" :options="bar"/>
</div>
</template>
<script>
import ECharts from "vue-echarts/components/ECharts";
import "echarts/lib/chart/bar";
export default {
name: "App",
components: {
"v-chart": ECharts
},
data: function() {
return {
bar: {
title: {
text: "ECharts 入門示例"
},
tooltip: {},
legend: {
data: ["銷量"]
},
xAxis: {
data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
},
yAxis: {},
series: [
{
name: "銷量",
type: "bar",
data: [5, 20, 36, 10, 10, 20]
}
]
}
};
}
};
</script>
<style>
.my-chart {
width: 800px;
height: 500px;
}
</style>