前言
如今,数据已成为企业和个人不可或缺的资源。然而,无论数据的来源、体量和种类如何,作为人类的我们有时会感到“数据山海苍茫,看不清真假蒙蔽双眼”。而数据可视化正是帮助我们解决这一问题的利器之一。其中,Echarts 作为当前最为流行的前端可视化库之一,其简单易用和扩展性强的特点得到了越来越多开发者和设计师的青睐。本文旨在从入门到精通的角度,为大家一步步展示如何玩转 Echarts,让数据可视化变得更加得心应手。
一、安装
echarts
的安装非常容易,简单来说只需要两步:
1. 下载 echarts
在下载echarts时,很多人可能会遇到安装不成功或者报错的问题,解决办法可以通过重装或者是装之前的版本来解决。
npm install echarts --save
npm install echarts@4.8.0 --save
//卸载命令
npm uninstall echarts
2. main.js 中引入并注册
import echarts from 'echarts';
Vue.prototype.$echarts = echarts;
若是引入后报错,可尝试以下引入方式:
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts;
到这里,echarts
的安装也就结束了,是不是特别简单,哈哈,先别急,接着往下看…
二、使用
上面我们已经将 echarts
安装引入了,接下来需要在项目中使用,其实 echarts
的使用也非常的简单,具体分为以下几步:
1. 为 echarts 准备一个具有宽高的div容器(简单来说就是存放图表的一个占位)
<div id="foldBreadChart" :style="{ width: '100%', height: '100%' }"></div>
2. 获取定义 id 并通过 echarts.init() 方法初始化 echarts 实例
var myChart = this.$echarts.init(document.getElementById('foldBreadChart'));
3. 根据个人需求调整图表的配置项和数据
let option = {
......
}
4. 通过 setOption() 方法生成图表
myChart.setOption(option)
看到这里,可能你还是不清楚到底该怎么下手,别着急,下面我整理了常用的图表实例,借助代码以及注释就可以帮助你快速上手。
三、实例
1.单个柱状图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="pillarsChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getJxbyl } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getJxbyl(); //接口方法
},
methods: {
//接口方法
getJxbyl() {
//请求接口
getJxbyl({}).then((res) => {
if (res.code == "10000") {
this.pillarsEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
pillarsEcharts(x, y) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("pillarsChart"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
axisPointer: {
type: 'shadow'
}
},
xAxis: {
//x轴设置
type: "category", //类型
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
yAxis: {
//y轴设置
type: "value", //类型
},
series: [
{
data: y, //y即接口方法传递过来的参数也是y轴的数据(x等同于res.data.yData)
type: "bar", //类型
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 50%;
height: 50vh;
}
</style>
2.基础折线图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="brokenChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { obd } from "@api/screen"; //引入接口文件
export default {
data() {
return {};
},
mounted() {
this.obd(); //接口方法
},
methods: {
//请求接口
obd() {
obd({}).then((res) => {
if (res.code == "10000") {
this.brokenChart(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
brokenChart(x, y) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("brokenChart"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
},
xAxis: {
type: "category", //类型
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
yAxis: {
type: "value",
},
series: [
{
data: y, //y即接口方法传递过来的参数也是y轴的数据(x等同于res.data.xData)
type: "line", //类型
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
3.基础饼状图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="cakeChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getMeachDistribution } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getMeachDistribution(); //接口方法
},
methods: {
//接口方法
getMeachDistribution() {
//请求接口
getMeachDistribution({}).then((res) => {
if (res.code == "10000") {
this.cakeEcharts(res.data); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
cakeEcharts(data) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("cakeChart")); //调用图表方法并将参数(数据)传递过去
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "item",
},
series: [
{
name: "月份",
type: "pie",
radius: "50%",
data: data, //data即接口方法传递过来的参数也是图表的数据(data等同于res.data)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
4.基础散点图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="splashes" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getsaojb } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getsaojb(); //接口方法
},
methods: {
//接口方法
getsaojb() {
//请求接口
getsaojb({}).then((res) => {
if (res.code == "10000") {
this.pillarsEcharts(res.data); //调用图表方法并将参数(数据)传递过去
}
});
},
pillarsEcharts(data) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("splashes"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "item",
},
xAxis: {},
yAxis: {},
series: [
{
symbolSize: 20, //点的大小
data: data, //data即接口方法传递过来的参数也是图表的数据(data等同于res.data)
type: "scatter", //类型
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
5.基础K线图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="linegraph" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getgraph } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getgraph(); //接口方法
},
methods: {
//接口方法
getgraph() {
//请求接口
getgraph({}).then((res) => {
if (res.code == "10000") {
this.pillarsEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
pillarsEcharts(x, y) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("linegraph"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "item",
},
xAxis: {
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
yAxis: {},
series: [
{
type: "candlestick",
data: y, //y即接口方法传递过来的参数也是y轴的数据(y等同于res.data.yData)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
6.折线图堆叠
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="brokenCharts" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { obd } from "@api/screen"; //引入接口文件
export default {
data() {
return {};
},
mounted() {
this.obd(); //接口方法
},
methods: {
//请求接口
obd() {
obd({}).then((res) => {
if (res.code == "10000") {
this.brokenCharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
brokenCharts(x, y) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("brokenCharts"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
},
legend: {
//图例
data: ["语文", "数学", "英语", "历史"],
},
xAxis: {
type: "category", //类型
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
yAxis: {
type: "value",
},
series: [
{
name: "语文", //对应的名字
data: y.cyType, //y.cyType即接口方法传递过来的参数也是y轴的数据(y.cyType等同于res.data.yData.cyType)
type: "line", //类型
},
{
name: "数学", //对应的名字
data: y.qcType, //y.qcType即接口方法传递过来的参数也是y轴的数据(y.qcType等同于res.data.yData.qcType)
type: "line", //类型
},
{
name: "英语", //对应的名字
data: y.xnyType, //y.xnyType即接口方法传递过来的参数也是y轴的数据(y.xnyType等同于res.data.yData.xnyType)
type: "line", //类型
},
{
name: "历史", //对应的名字
data: y.hsType, //y.hsType即接口方法传递过来的参数也是y轴的数据(y.hsType等同于res.data.yData.hsType)
type: "line", //类型
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
7.竖行比较柱状图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="columnarChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getLjlcqk } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getLjlcqk(); //接口方法
},
methods: {
//接口方法
getLjlcqk() {
//请求接口
getLjlcqk({}).then((res) => {
if (res.code == "10000") {
this.columnarEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
columnarEcharts(x, y) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("columnarChart"));
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
legend: {}, //图例
xAxis: [
{
type: "category",
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
],
yAxis: [
{
type: "value",
},
],
series: [
{
name: "合格",
data: y.hyType, //y.hyType即接口方法传递过来的参数也是y轴的数据(y.hyType等同于res.data.yData.hyType)
type: "bar", //图表类型
barWidth: 20, //柱图宽度
},
{
name: "不合格",
data: y.cyType, //y.cyType即接口方法传递过来的参数也是y轴的数据(y.cyType等同于res.data.yData.cyType)
type: "bar", //图表类型
barWidth: 20, //柱图宽度
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
8.折柱混合
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="foldBreadChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getIMJcxq } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getIMJcxq(); //接口方法
},
methods: {
//接口方法
getIMJcxq() {
//请求接口
getIMJcxq({}).then((res) => {
if (res.code == "10000") {
this.foldBreadEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
foldBreadEcharts(x, y) {
// 获取图表id并初始化图表
var myChart = this.$echarts.init(document.getElementById("foldBreadChart"));
//配置项
let option = {
tooltip: {
// 鼠标触摸显示值
trigger: "axis",
axisPointer: {
type: "cross",
},
},
legend: {}, //图例
xAxis: [
{
//x轴
type: "category",
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
axisPointer: {
type: "shadow", //类型
},
},
],
yAxis: [
{
//y轴左边
type: "value",
name: "(降水量ml)", //顶部文字描述
},
// y轴右边
{
type: "value",
name: "(温度°C)", //右边顶部文字描述
},
],
series: [
{
name: "降水量", //顶部标题
type: "bar", //类型 柱状
barWidth: 12, //柱体宽度
data: y.leftData, //y.leftData即接口方法传递过来的参数也是y轴的数据(y.leftData等同于res.data.yData.leftData)
},
{
name: "温度", //顶部标题
type: "line", //类型 折线
yAxisIndex: 1, //使用的x轴的 index,在单个图表实例中存在多个x轴的时候有用
data: y.rightData, //y.leftData即接口方法传递过来的参数也是y轴的数据(y.rightData等同于res.data.yData.rightData)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
9.横向比较柱状图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="acrossChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getHbSyJylbh } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getHbSyJylbh(); //接口方法
},
methods: {
//接口方法
getHbSyJylbh() {
//请求接口
getHbSyJylbh({}).then((res) => {
if (res.code == "10000") {
this.acrossEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
acrossEcharts(x, y) {
//获取图表id并初始化图表
var myChart = this.$echarts.init(document.getElementById("acrossChart"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
legend: {
//图例
data: ["在线", "离线"],
},
xAxis: {
type: "value",
},
yAxis: {
type: "category",
data: y, //y即接口方法传递过来的参数也是y轴的数据(y等同于res.data.yData)
},
series: [
{
name: "在线", //对应名字
type: "bar", //类型 柱体
data: x.hyType, //x.hyType即接口方法传递过来的参数也是x轴的数据(x.hyType等同于res.data.xData.hyType)
},
{
name: "离线", //对应名字
type: "bar", //类型 柱体
data: x.cyType, //x即接口方法传递过来的参数也是x轴的数据(x.cyType等同于res.data.xData.cyType)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
10.雷达图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="columnarChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getCarPhenonmenon } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getCarPhenonmenon(); //接口方法
},
methods: {
//接口方法
getCarPhenonmenon() {
getCarPhenonmenon({}).then((res) => {
if (res.code == "10000") {
this.radarEcharts(res.data); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
radarEcharts(data) {
//获取图表id并初始化图表
var myChart = this.$echarts.init(document.getElementById("columnarChart"));
//配置项
let option = {
tooltip: {
//鼠标触摸展示值
trigger: "axis",
},
radar: [
{
indicator: [
//文本信息
{ text: "七月", max: 100 },
{ text: "八月", max: 100 },
{ text: "九月", max: 100 },
],
center: ["50%", "50%"], //图表位置 水平 垂直
radius: 62, //圆角弧度
},
],
legend: {}, //图例
series: [
{
type: "radar", //类型
tooltip: {
//鼠标触摸显示值
trigger: "item",
},
areaStyle: {},
data: data, //data即接口方法传递过来的参数也是图表的数据(data等同于res.data)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
11.堆叠柱状图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="another" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { heapMew } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.heapMew(); //接口方法
},
methods: {
//接口方法
heapMew() {
//请求接口
heapMew({}).then((res) => {
if (res.code == "10000") {
this.stackedEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
stackedEcharts(x, y) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("another"));
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
legend: {},
xAxis: [
{
//x轴配置
type: "category",
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
],
yAxis: [
{
//y轴
type: "value",
},
],
series: [
{
name: "增长数量", //鼠标触摸当前柱图的name
type: "bar", //类型是柱图
stack: "Ad", //以堆叠的形式展示
data: y.hyType, //y.hyType即接口方法传递过来的参数也是y轴的数据(y.hyType等同于res.data.yData.hyType)
},
{
name: "减少数量", //鼠标触摸当前柱图的name
type: "bar", //类型是柱图
stack: "Ad", //以堆叠的形式展示
data: y.ygType, //y.ygType即接口方法传递过来的参数也是y轴的数据(y.ygType等同于res.data.yData.ygType)
},
{
name: "总数", //鼠标触摸当前柱图的name
type: "bar", //类型是柱图
stack: "Ad", //以堆叠的形式展示
data: y.qyType, //y.qyType即接口方法传递过来的参数也是y轴的数据(y.qyType等同于res.data.yData.qyType)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
12.堆叠柱状图+折线图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="foldBreadChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { anotherPort } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.another(); //接口方法
},
methods: {
//接口方法
another() {
//请求接口
anotherPort({}).then((res) => {
if (res.code == "10000") {
this.foldBreadEcharts(res.data.xData, res.data.yData); //调用图表方法并将参数(数据)传递过去
}
});
},
//图表方法
foldBreadEcharts(x, y) {
//获取图表id并初始化图表
var myChart = this.$echarts.init(
document.getElementById("foldBreadChart")
);
//配置项
let option = {
tooltip: {
//鼠标触摸显示值
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
legend: {},
xAxis: [
{
//x轴
type: "category", //类型
data: x, //x即接口方法传递过来的参数也是x轴的数据(x等同于res.data.xData)
},
],
yAxis: [
{
//y轴
type: "value", //类型
name: "检查车辆(辆)", //y轴左边副标题
},
{
type: "value", //类型
name: "超标率(%)", //y轴右边副标题
min: 0, //最小值为0
max: 100, //最大值为100
interval: 20, //每次递增20
axisLabel: {
formatter: "{value}",
},
},
],
series: [
{
name: "检测合格车辆(辆)",
type: "bar", //类型柱图
stack: "Ad",
emphasis: {
focus: "series",
},
data: y.leftOneData, //y.leftOneData即接口方法传递过来的参数也是y轴的数据(y.leftOneData等同于res.data.yData.leftOneData)
},
{
name: "检测超标车辆(辆)",
type: "bar", //类型柱图
stack: "Ad",
emphasis: {
focus: "series",
},
data: y.leftTwoData, //y.leftTwoData即接口方法传递过来的参数也是y轴的数据(y.leftTwoData等同于res.data.yData.leftTwoData)
},
{
name: "超标率(%)",
type: "line", //类型折线图
yAxisIndex: 1, //使用的x轴的 index,在单个图表实例中存在多个x轴的时候有用
smooth: true,
data: y.rightData, //y.rightData即接口方法传递过来的参数也是y轴的数据(y.rightData等同于res.data.yData.rightData)
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
13.仪表图
<template>
<!-- 图表的容器,一定要有宽高 -->
<div class="chartBox">
<div id="pillarsChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
import { getweekz } from "@api/screen"; //引入的接口文件
export default {
data() {
return {};
},
mounted() {
this.getweekz(); //接口方法
},
methods: {
//接口方法
getweekz() {
//请求接口
getweekz({}).then((res) => {
if (res.code == "10000") {
this.pillarsEcharts(res.data); //调用图表方法并将参数(数据)传递过去
}
});
},
pillarsEcharts(data) {
//获取id并初始化图表
var myChart = this.$echarts.init(document.getElementById("pillarsChart"));
//配置项
let option = {
tooltip: {
//显示值
formatter: "{b} : {c}%",
},
series: [
{
startAngle: 210, //开始角度
endAngle: -30, //结束角度
type: "gauge",
progress: {
show: true,
width: 18,
},
axisLine: {
lineStyle: {
width: 18,
},
},
//分隔线样式。
splitLine: {
show: false,
},
//刻度样式。
axisTick: {
show: false,
},
//刻度标签。
axisLabel: {
distance: 4,
color: "#999",
fontSize: 10,
},
pointer: {
show: false,
},
anchor: {
show: true,
showAbove: true,
size: 0,
itemStyle: {
borderWidth: 10,
},
},
title: {
//标题样式
show: true,
offsetCenter: [0, "-5%"],
},
detail: {
valueAnimation: true,
fontSize: 16, //字体大小
offsetCenter: [0, "30%"], //百分比位置
formatter: function (value) {
return Math.round(value) + "%";
},
},
data: data,
},
],
};
myChart.setOption(option); //通过setOption()方法生成图表
window.addEventListener("resize", function () {
myChart.resize(); //图表自适应的一个方法
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 30%;
height: 50vh;
}
</style>
14.热力图
<template>
<div class="chartBox">
<div id="hotChart" :style="{ width: '100%', height: '100%' }"></div>
</div>
</template>
<script>
export default {
data() {
return {};
},
mounted() {
// 模拟x轴数据
const dataOrigin = {
xAxisData: [
"2022-07-01",
"2022-07-02",
"2022-07-03",
"2022-07-04",
"2022-07-05",
"2022-07-06",
"2022-07-07",
"2022-07-08",
"2022-07-09",
"2022-07-10",
"2022-07-11",
"2022-07-12",
"2022-07-13",
],
// 模拟y轴数据
yAxisData: [
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
"xxx监测站点",
],
// name:数据系列的名称,用于图例显示
// data:一个二维数组,表示数据的具体值。每个内部数组表示一个数据点,包含三个值,分别表示横坐标、纵坐标和数据值
// color:数据系列的颜色,用于表示该数据系列的热力图颜色
seriesDatas: [
{
name: "优",
data: [
[2, 0, 31],
[3, 0, 36],
[4, 0, 34],
[5, 0, 36],
[6, 0, 25],
[7, 0, 35],
[8, 0, 34],
[9, 0, 34],
[10, 0, 41],
[11, 0, 44],
[12, 0, 48],
[13, 0, 37],
],
color: "#8DB25D",
},
{
name: "良",
data: [
[0, 0, 54],
[1, 0, 54],
[20, 0, 60],
[21, 0, 80],
[22, 0, 69],
[23, 0, 53],
[31, 0, 66],
[0, 1, 98],
[1, 1, 56],
[4, 1, 62],
[7, 1, 71],
[8, 1, 67],
[9, 1, 56],
[12, 1, 57],
[13, 1, 56],
[14, 1, 51],
],
color: "#DBCE66",
},
{
name: "轻度",
data: [
[5, 1, 112],
[6, 1, 118],
[9, 2, 146],
[19, 2, 101],
[20, 2, 125],
[21, 2, 131],
[0, 3, 101],
[9, 3, 101],
[12, 3, 109],
[13, 3, 120],
[22, 3, 109],
[25, 3, 101],
[26, 3, 113],
[29, 5, 116],
[15, 6, 103],
[5, 7, 127],
[6, 7, 137],
],
color: "#F49028",
},
{
name: "中度",
data: [
[11, 2, 171],
[22, 2, 163],
[26, 2, 152],
],
color: "#F35654",
},
{
name: "重度",
data: [
[10, 2, 223],
[28, 5, 204],
],
color: "#B255C5",
},
],
};
// label:空气质量级别的名称,用于图例显示
// min:该级别的最小值
// max:该级别的最大值
// color:该级别的颜色,用于表示该级别的热力图颜色
const AQI_LEVELS = [
{
label: "优",
min: 0,
max: 50,
color: "#8DB25D",
lightColor: "#D2F5A5",
},
{
label: "良",
min: 51,
max: 100,
color: "#DBCE66",
lightColor: "#EBEBD8",
},
{
label: "轻度",
min: 101,
max: 150,
color: "#F49028",
lightColor: "#F2E1C7",
},
{
label: "中度",
min: 151,
max: 200,
color: "#F35654",
lightColor: "#F3D3D3",
},
{
label: "重度",
min: 201,
max: 300,
color: "#B255C5",
lightColor: "#E5D2ED",
},
];
this.cakeEcharts(dataOrigin, AQI_LEVELS);
},
methods: {
cakeEcharts(dataOrigin, AQI_LEVELS) {
// 根据传入的数值范围判断所属的级别,并返回对应的级别对象。AQI_LEVELS数组定义了不同级别的信息,函数根据value的大小来确定所属的级别,并返回该级别的详细信息
const getLevel = (value) => {
let index = 0;
if (!value || value <= 50) index = 0;
if (value > 50 && value <= 100) index = 1;
if (value > 100 && value <= 150) index = 2;
if (value > 150 && value <= 200) index = 3;
if (value > 200 && value <= 300) index = 4;
if (value > 300) index = 5;
return { index, ...AQI_LEVELS[index] };
};
// 获取悬浮提示元素
const getTooltipRow = (params, isHideEmpty = false) => {
if (isHideEmpty && !params.value) return "";
return `
<div style="display: inline-flex; justify-content: space-between; min-width: 80px; padding-top: 3px;">
<span style="${params.marker ? "" : "padding-left: 18px"}">${
params.marker || ""
} ${params.seriesName}</span>
<span style="padding-left:2px;font-weight: bold;">${
params.value || "-"
}dB</span>
</div>`;
};
const visualMapPieces = AQI_LEVELS.map(({ min, max, color }) => ({
min,
max,
color,
}));
var myChart = this.$echarts.init(document.getElementById("hotChart"));
let option = {
tooltip: {
position: "top",
formatter: (params) => {
const dateStr = `${dataOrigin.yAxisData[params.data[1]]} ${
params.name
}`;
const value = {
marker: params.marker,
seriesName: getLevel(params.data[2]).label,
value: params.data[2] || "-",
};
return `${dateStr}${getTooltipRow(value)}`;
},
},
grid: {
top: 40,
left: 100,
right: 50,
bottom: 65,
},
visualMap: {
show: false,
pieces: visualMapPieces,
},
legend: {
show: true,
icon: "circle",
bottom: 10,
},
xAxis: {
type: "category",
data: dataOrigin.xAxisData,
axisLine: {
show: false,
},
axisTick: {
show: false,
},
splitArea: {
show: true,
},
},
yAxis: {
type: "category",
data: dataOrigin.yAxisData,
axisLine: {
show: false,
},
axisTick: {
show: false,
},
splitArea: {
show: true,
},
},
series: dataOrigin.seriesDatas.map(({ name, data, color }) => ({
name,
data,
type: "heatmap",
label: {
show: true,
},
itemStyle: {
color,
borderColor: "#fff",
borderWitdh: 5,
borderCap: "round",
},
})),
};
myChart.setOption(option);
window.addEventListener("resize", function () {
myChart.resize();
});
},
},
};
</script>
<style scoped>
.chartBox {
width: 50%;
height: 50vh;
}
</style>
相关推荐
⭐ 从入门到精通:Echarts 图表常用配置项全攻略
⭐ 解决echarts报错Cannot read properties of null (reading ‘getAttribute‘)
⭐ 解决element标签组件切换echarts图表宽高丢失问题
⭐ 数据的跃动之美:探索ECharts动态排序柱状图的魔力
⭐ 一文带你快速上手Highcharts框架