vue 中使用echarts
前言:在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(