vue 中使用echarts
阿新 • • 發佈:2020-08-20
前言:在vue2.0中使用百度echarts有三種解決方案。
一、原始方法直接使用
這樣每次都要獲取圖表dom元素 然後通過setOption渲染資料,最後在mounted中初始化。很麻煩。
<template>
<div>
<div id="typeChart" style="width: 600px;height:400px;"></div>
<div id="brandChart" style="width: 600px;height:400px;"></div>
</div>
</template>
<script>
import echarts from "echarts";
export default {
methods: {
typeChart() {
// 基於準備好的dom,初始化echarts例項
let typeChart = echarts.init(document.getElementById("typeChart"));
// 指定圖表的配置項和資料
let option = {
color: ["red"],
title: {
text: "型別統計"
},
tooltip: {},
legend: {
data: ["檢測車輛"]
},
xAxis: {
data: ["中原區", "二七區", "金水區", "上街區", "中牟縣", "經開區","高新區"]
},
yAxis: {},
series: [
{
name: "檢測車輛",
type: "bar",
barWidth: 20,
data: [50, 100, 200, 300, 400, 500, 600],
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: "#67B6FF" }, //柱圖漸變色
{ offset: 0.5, color: "#44C0C1" }, //柱圖漸變色
{ offset: 1, color: "#06B5D7" } //柱圖漸變色
])
}
}
}
]
};
// 使用剛指定的配置項和資料顯示圖表。
typeChart.setOption(option);
},
brandChart() {
// 基於準備好的dom,初始化echarts例項
let brandChart