echarts图表的具体使用方法就不介绍了 可以去看以前的博客
1 创建html
<div ref="userData" :style="{width: '100%', height: '450px'}"></div>
2.对饼状图表进行配置
// 累计用户数图表配置
getUserData() {
//首先获取到元素
this.userDataChart = this.$echarts.init(this.$refs.userData);
this.userDataOption = {
tooltip: {
trigger: "item",
formatter: " {b}: {c} ({d}%)",
},
legend: {
//调整图例位置
bottom: 0,
//调整图例的颜色块大小
itemWidth: 12,
itemHeight: 12,
data: ["初始用户数", "新增用户数", "支付用户数", "未支付用户数"],
},
color: [
"#ff8533",
"rgb(247, 210, 28)",
"#85d4d3",
"rgb(64, 158, 255)",
],
series: [
{
type: "pie",
// 调整图形大小
radius: [0, "35%"],
label: {
position: "inner",
},
labelLine: {
show: false,
},
data: [
//selected来控制当前的这个饼状图是否是选中的状态
{ value: 0, name: "未支付用户数", selected: false },
{ value: 0, name: "支付用户数", selected: false },
],
},
{
name: "累积用户",
type: "pie",
// 调整图形大小
radius: ["45%", "65%"],
label: {
// b c 这些指的是下边的配置内容
formatter: " {a|{a}}{abg|}\n{hr|}\n {b|{b}:}{c} {per|{d}%} ",
borderColor: "#aaa",
borderWidth: 1,
borderRadius: 4,
rich: {
a: {
color: "#999",
lineHeight: 24,
align: "center",
},
hr: {
borderColor: "#aaa",
width: "100%",
borderWidth: 0.5,
height: 0,
},
b: {
fontSize: 14,
lineHeight: 33,
},
per: {
color: "#eee",
backgroundColor: "rgb(23, 94, 222)",
padding: [2, 4],
borderRadius: 2,
},
},
},
data: [
//selected来控制当前的这个饼状图是否是选中的状态
{ value: 0, name: "新增用户数", selected: true },
{ value: 1, name: "初始用户数" },
],
},
],
};
this.userDataChart.setOption(this.userDataOption, true);
var that = this;
//当点击饼状图的某个部分的点击函数
this.userDataChart.on("click", function (param) {
if (param.data.name == "初始用户数") {
that.externalChecked = param.data.name;
that.interiorChecked = "";
//如果点击初始化用户
// 那么就让新增和内圈都为false
// userType=0 并且 让 paid为空
// 因为此时 我只想知道初始化用户的列表数据
// 调用 列表接口 和 图标数据接口 让内圈为初始化用户下的内圈
that.userDataOption.series[1].data[0].selected = false;
that.userDataOption.series[1].data[1].selected = true;
that.userDataOption.series[0].data[1].selected = false;
that.userDataOption.series[0].data[0].selected = false;
that.userType = 0;
that.paid = "";
//对以上进行判断之后 调用函数 刷新图表数据和table表格显示数据
that.getUserList(false);
that.getUserStatistics();
} else if (param.data.name == "新增用户数") {
that.externalChecked = param.data.name;
that.interiorChecked = "";
//如果点击新增用户数
// 那么就让新增和内圈选择状态都为false
// userType=1 并且 让 paid为空
// 因为此时 我只想知道新增用户数的列表数据
// 调用 列表接口 和 图表数据接口 让内圈为新增用户数下的内圈
that.userDataOption.series[1].data[0].selected = true;
that.userDataOption.series[1].data[1].selected = false;
that.userDataOption.series[0].data[1].selected = false;
that.userDataOption.series[0].data[0].selected = false;
that.userType = 1;
that.paid = "";
//对以上进行判断之后 调用函数 刷新图表数据和table表格显示数据
that.getUserList(false);
that.getUserStatistics();
}
//如果内圈选中为支付 并且外圈新增为选中状态
//就让内圈支付选中 另一为false
//请求的参数为1
if (
param.data.name == "支付用户数" &&
that.userDataOption.series[1].data[0].selected == true
) {
that.interiorChecked = param.data.name;
that.userDataOption.series[0].data[1].selected = true;
that.userDataOption.series[0].data[0].selected = false;
that.paid = 1;
//对以上进行判断之后 调用函数 刷新表格显示数据
that.getUserList(false);
//如果内圈选中为未支付 并且外圈新增为选中状态
//就让内圈未支付选中 另一为false
//请求的参数为0
} else if (
param.data.name == "未支付用户数" &&
that.userDataOption.series[1].data[0].selected == true
) {
that.interiorChecked = param.data.name;
that.userDataOption.series[0].data[1].selected = false;
that.userDataOption.series[0].data[0].selected = true;
that.paid = 0;
//对以上进行判断之后 调用函数 刷新表格显示数据
that.getUserList(false);
}
//如果内圈选中为支付 并且外圈初始为选中状态
//就让内圈支付选中 另一为false
//请求的参数为1
if (
param.data.name == "支付用户数" &&
that.userDataOption.series[1].data[1].selected == true
) {
that.interiorChecked = param.data.name;
that.userDataOption.series[0].data[1].selected = true;
that.userDataOption.series[0].data[0].selected = false;
that.paid = 1;
//对以上进行判断之后 调用函数 刷新表格显示数据
that.getUserList(false);
//如果内圈选中为未支付 并且外圈初始为选中状态
//就让内圈支付选中 另一为false
//请求的参数为1
} else if (
param.data.name == "未支付用户数" &&
that.userDataOption.series[1].data[1].selected == true
) {
that.interiorChecked = param.data.name;
that.userDataOption.series[0].data[1].selected = false;
that.userDataOption.series[0].data[0].selected = true;
that.paid = 0;
//对以上进行判断之后 调用函数 刷新表格显示数据
that.getUserList(false);
}
});
},
3.在当前钩子中 进行 自适应宽高
mounted() {
this.getUserData();
// 自适应
window.onresize = () => {
this.userDataChart.resize();
};
},
4.监听图表数据的变化 做到数据变化图表显示改变
// 监听图表数据的变化
watch: {
userDataOption: {
handler(newVal, oldVal) {
if (this.userDataChart) {
// 如何有变化 就获取最新的数据 并且渲染
this.userDataChart.setOption(newVal);
} else {
// 如何无变化 还是老数据
this.userDataChart.setOption(oldVal);
}
},
deep: true,
},
},
5.获取到图表中的数据 然后赋值给图表配置项中的data
//用户统计图表数据
getUserStatistics() {
this.allStartDate = moment(this.dateValue[0]).format(
"YYYY-MM-DD 00:00:00"
);
this.allEndDate = moment(this.dateValue[1]).format("YYYY-MM-DD 23:59:59");
let data = {
from: this.allStartDate,
to: this.allEndDate,
//这里的传递的参数 如果外圈为初始化 就传递初始化的参数
//内圈就为初始化下的参数
userType: this.userType,
};
Vue.axios({
method: "POST",
serviceId: "mall",
url: "/datacentics",
data,
})
.then((res) => {
if (res.success) {
//把获取的数据赋值过去 这样子 图表就已经得到了显示的数值
this.userDataOption.series[0].data[0].value = res.result.noPayUsers;
this.userDataOption.series[0].data[1].value = res.result.payUsers;
this.userDataOption.series[1].data[1].value = res.result.totalUsers;
this.userDataOption.series[1].data[0].value = res.result.newUsers;
} else {
Message.error("获取用户统计数据失败,请稍后重试!");
}
})
.catch((err) => {
Message.error("获取用户统计数据失败,请稍后重试!");
});
},
6.获取右侧表格中的数据 默认显示的数据 为新增用户的数据
//获取用户列表
async getUserList(reload, page) {
// 判断 是否为首次加载
if (reload) {
if (!page) {
// 获取当前local中的data数据
let data = await this.$fetchParams("getUserList");
if (data) {
this.pagination.currentPage = data.currentPage;
this.pagination.showPageSize = data.pageSize;
this.pagination.PageNumList = data.page;
this.pagination.totalList = data.total;
}
}
}
if (!reload && !page) {
this.pagination.PageNumList = 1;
this.pagination.showPageSize = 10;
this.pagination.currentPage = 1;
}
this.allStartDate = moment(this.dateValue[0]).format(
"YYYY-MM-DD 00:00:00"
);
this.allEndDate = moment(this.dateValue[1]).format("YYYY-MM-DD 23:59:59");
let data = {
from: this.allStartDate,
to: this.allEndDate,
//根据userType来控制显示的是哪个饼图的数据
userType: this.userType,
paid: this.paid,
size: this.pagination.showPageSize,
page: this.pagination.PageNumList,
};
Vue.axios({
method: "POST",
serviceId: "mall",
url: "/datacsList",
data,
})
.then((res) => {
if (res.success) {
this.pagination.currentPage = res.result.current;
this.pagination.totalList = res.result.total;
this.userTableData = res.result.records;
} else {
Message.error("获取用户统计列表数据失败,请稍后重试!");
}
})
.catch((err) => {
Message.error("获取用户统计列表数据失败,请稍后重试!");
});
},