文章目录


Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)

Vue 项目后端接口框架搭建中,我们已经配置好了图表二中的数据,这里直接调用接口就行了。

一、配置图表二

1、基本结构搭建

vue/app目录下的​components​文件夹下的 ​itemTwo.vue​中,设置图表2,初始化​echarts​:

<template>
<div>
<h2>产品月销</h2>
<div class="chart" id="chartTwo">
<!-- 图表容器 -->
</div>
</div>
</template>

<script>import { inject, reactive, onMounted } from "vue";

export default {
setup() {
// 获取echarts对象
var $echarts = inject("echarts");

// 钩子
onMounted(() => {
// 初始化echarts
var myChart = $echarts.init(document.getElementById("chartTwo"));
// 配置项
var option = null;
option = {};
// 封装
myChart.setOption(option);
});

return {};
},
};</script>

<style>
</style>

返回顶部


2、Axios 请求数据

接下来我们需要图表展示的数据(后台 server 提供),我们需要在组件内容请求数据,就需要引入 ​axios​组件,并通过​axios​获取数据:

<template>
<div>
<h2>产品月销</h2>
<div class="chart" id="chartTwo">
<!-- 图表容器 -->
</div>
</div>
</template>

<script>import { inject, reactive, onMounted } from "vue";

export default {
setup() {
// 获取echarts对象
var $echarts = inject("echarts");
// 获取axios
var $axios = inject("axios");
// 获取数据
var dataTwo = reactive({});

// 自定义函数获取数据
async function getData(){
dataTwo = await $axios({url:'/two/data'});
// 控制台输出
console.log("dataTwo:",dataTwo);
}

// 钩子
onMounted(() => {
// 初始化echarts
var myChart = $echarts.init(document.getElementById("chartTwo"));
// 调用函数获取数据
getData();
// 配置项
var option = null;
option = {};
// 封装
myChart.setOption(option);
});

return {getData,dataTwo};
},
};</script>

<style>
</style>

配置完成后,记得运行 server,然后运行 ​app​,在页面控制台中查看打印的数据:

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_echarts

通过数据的格式我们可以在 dataTwo.data.chartData.chartData 中获取的所需要得到数据。所以我们创建变量 ​realdata​ 对其进行封装获取,并且为了使数据更容易进行配置,我们需要进行更细致的划分获取,后面配置参数的时候可以直接使用:

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_express_02

返回顶部


3、图表获取数据配置

注意在图标配置的时候,我们需要满足折线图数据的配置样式,每种类别为一组数据。通过上节的数据格式分析,我们获取到了realData,横坐标为日期信息:​realdata.day​,纵坐标为每个类别的数据信息:​realdata.num.类别名​,对应配置即可。

<template>
<div>
<h2>产品月销</h2>
<div class="chart" id="chartTwo">
<!-- 图表容器 -->
</div>
</div>
</template>

<script>import { inject, reactive, onMounted } from "vue";

export default {
setup() {
// 获取echarts对象
var $echarts = inject("echarts");
// 获取axios
var $axios = inject("axios");
// 获取整体数据
var dataTwo = reactive({});
var realdata = reactive([]);

// 自定义函数获取数据
async function getData() {
dataTwo = await $axios({ url: "/two/data" });
realdata = dataTwo.data.chartData.chartData;
// 控制台输出
console.log("dataTwo:", dataTwo);
console.log("dataTwo-real:", realdata);
}

// 钩子
onMounted(() => {
// 初始化echarts
var myChart = $echarts.init(document.getElementById("chartTwo"));
// 调用函数获取数据
getData().then(() => {
// 配置项
var option = null;
option = {
xAxis:{
type:'category',
data:realdata.day
},
yAxis:{
type:'value'
},
series:[
{
name:'Chemicals',
type:'line',
data:realdata.num.Chemicals
},
{
name:'Clothes',
type:'line',
data:realdata.num.Clothes
},
{
name:'Electrical',
type:'line',
data:realdata.num.Electrical
},
{
name:'digit',
type:'line',
data:realdata.num.digit
},
{
name:'gear',
type:'line',
data:realdata.num.gear
},
]
};
// 封装
myChart.setOption(option);
});
});
// 返回值
return { getData, dataTwo, realdata };
},
};</script>

<style>h2 {
/* 48像素 */
height: 0.6rem;
color: #fff;
line-height: 0.6rem;
text-align: center;
font-size: 0.25rem;
}
.chart {
/* 高度360 */
height: 4.3rem;
/* background-color: gray; */
}</style>

如下图所示,基本的折线图样式就已经出来了:

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_数据_03

返回顶部


4、图标丰富

(1)字体颜色

option={
xAxis: {
type: "category",
data: realdata.day,
// 设置坐标轴上文字颜色
axisLine: {
lineStyle: {
color: "#fff",
},
},
},
yAxis: {
type: "value",
// 设置坐标轴上文字颜色
axisLine: {
lineStyle: {
color: "#fff",
},
},
},
series: [
............
]
}

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_04

Top


(2)设置提示框组件

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_数据_05

option = {
tooltip: {
//提示框组件
trigger: "axis", // 坐标轴
axisPointer: {
type: "cross", // 十字样式
},
},
......
}

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_ios_06

提示器坐标轴背景修改:

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_echarts_07

tooltip: {
//提示框组件
trigger: "axis",
axisPointer: {
type: "cross",
label: {
//坐标轴指示器的文本标签
backgroundColor: "#000", //文本标签的背景颜色就是x轴y轴上的内容
},
},
},

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_数据_08

Top


(3)图例展示

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_echarts_09

官网参数配置

option = {
legend:{ // 图例
show:true // 默认显示置顶
},
xAxis:{
type:'category',
data:realdata.day
},
yAxis:{
type:'value'
},
series:[
..........
]
};

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_express_10

Top


(4)平滑曲线

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_11

series: [
{
name: "Chemicals",
type: "line",
smooth:true, // 平滑模式开启
data: realdata.num.Chemicals,
},
{
name: "Clothes",
type: "line",
smooth:true,
data: realdata.num.Clothes,
},
......
]

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_express_12

Top


(5)堆叠曲线图

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_13

官方实例

series: [
{
name: "Chemicals",
type: "line",
smooth:true, // 平滑模式开启
stack:'Total', // 堆叠样式 - 和
data: realdata.num.Chemicals,
},
{
name: "Clothes",
type: "line",
smooth:true,
stack:'Total',
data: realdata.num.Clothes,
},
......
]

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_数据_14

Top


(6)堆叠面积填充

series: [
{
name: "Chemicals",
type: "line",
smooth: true, // 平滑模式开启
stack: "Total", // 堆叠样式 - 和
areaStyle: {
// 堆叠区域样式 --- 渐变色填充
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(128, 255, 165)",
},
{
offset: 1,
color: "rgb(1, 191, 236)",
},
]),
},
data: realdata.num.Chemicals,
},
{
name: "Clothes",
type: "line",
smooth: true,
stack: "Total",
areaStyle: {
opacity: 0.8,
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(0, 221, 255)",
},
{
offset: 1,
color: "rgb(77, 119, 255)",
},
]),
},
data: realdata.num.Clothes,
},
{
name: "Electrical",
type: "line",
smooth: true,
stack: "Total",
areaStyle: {
opacity: 0.8,
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(55, 162, 255)",
},
{
offset: 1,
color: "rgb(116, 21, 219)",
},
]),
},
data: realdata.num.Electrical,
},
{
name: "digit",
type: "line",
smooth: true,
stack: "Total",
areaStyle: {
opacity: 0.8,
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(255, 0, 135)",
},
{
offset: 1,
color: "rgb(135, 0, 157)",
},
]),
},
data: realdata.num.digit,
},
{
name: "gear",
type: "line",
smooth: true,
stack: "Total",
areaStyle: {
opacity: 0.8,
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(255, 191, 0)",
},
{
offset: 1,
color: "rgb(224, 62, 76)",
},
]),
},
data: realdata.num.gear,
},
],

默认填充:

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_express_15

渐变色填充:

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_echarts_16

Top


(7)设置曲线宽度

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_17

series: [
{
name: "Chemicals",
type: "line",
data: realdata.num.Chemicals,
smooth: true, // 平滑模式开启
stack: "Total", // 堆叠样式 - 和
lineStyle: {
// 设置线段样式
width: 0,
},
areaStyle: {
// 堆叠区域样式
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(128, 255, 165)",
},
{
offset: 1,
color: "rgb(1, 191, 236)",
},
]),
},
},
......
]

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_ios_18

Top


(8)隐藏所有数据点

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_ios_19

series: [
{
name: "Chemicals",
type: "line",
data: realdata.num.Chemicals,
smooth: true, // 平滑模式开启
stack: "Total", // 堆叠样式 - 和
showSymbol: false,// 隐藏所有数据点
lineStyle: {
// 设置线段样式
width: 0,
},
areaStyle: {
// 堆叠区域样式
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(128, 255, 165)",
},
{
offset: 1,
color: "rgb(1, 191, 236)",
},
]),
},
},
......
]

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_数据_20

Top


(9)高亮显示选中项

series: [
{
name: "Chemicals",
type: "line",
data: realdata.num.Chemicals,
smooth: true, // 平滑模式开启
stack: "Total", // 堆叠样式 - 和
showSymbol: false,// 隐藏所有数据点
lineStyle: {
// 设置线段样式
width: 0,
},
emphasis: {
//设置高亮的图形样式和标签样式
focus: "series", //只显示选中的内容高亮
},
areaStyle: {
// 堆叠区域样式
color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(128, 255, 165)",
},
{
offset: 1,
color: "rgb(1, 191, 236)",
},
]),
},
},
......
]

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_21

Top


(10)坐标轴两边留白策略

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_express_22

xAxis: {
type: "category",
data: realdata.day,
boundaryGap: false,
// 设置坐标轴上文字颜色
axisLine: {
lineStyle: {
color: "#fff",
},
},
},

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_23

Top


(11)图像在容器中的位置

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_vue_24

grid: {//组件离容器的距离
left: "1%",
right: "4%",
bottom: "3%",
containLabel: true, //grid 区域是否包含坐标轴的刻度标签
},

【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_express_25

Top


【Vue】Vue 项目前、后端整合(图表二:产品月销曲线堆叠图)_数据_26