调整图表展示

This commit is contained in:
huangdeliang
2021-03-22 16:58:19 +08:00
parent 180f994141
commit 5f84333c30
7 changed files with 763 additions and 228 deletions

View File

@ -0,0 +1,276 @@
<template>
<div ref="echart" :style="{ height: height, width: width }" />
</template>
<script>
import echarts from "echarts";
require("@/utils/echarts/myShine");
import resize from "@/views/dashboard/mixins/resize";
import { createNamespacedHelpers } from "vuex";
const { mapMutations, mapState } = createNamespacedHelpers("recipes");
export default {
mixins: [resize],
props: {
className: {
type: String,
default: "chart",
},
width: {
type: String,
default: "100%",
},
height: {
type: String,
default: "200px",
},
data: {
type: Array,
default: [],
},
subTitle: {
type: String,
default: "",
},
title: {
type: String,
default: "",
},
type: {
type: String,
default: "",
},
},
data() {
return {
chart: null,
};
},
computed: {},
mounted() {
this.$nextTick(() => {
this.initChart();
});
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
},
methods: {
initChart() {
this.chart = echarts.init(this.$refs.echart, "myShine");
if (this.data.length > 0) {
this.updateChart(this.data);
}
},
backToAll() {
this.resetCurrentDay({ currentDay: -1 });
},
getTooltipArr(params) {
console.log(params);
let tooltips;
const { name, marker, percent, value } = params;
switch (this.type) {
case "nutrition":
const weight = value[1] / (name === "脂肪" ? 9 : 4);
tooltips = [
`${marker} ${name}`,
`摄入量:${weight.toFixed(1)}`,
`摄入热量:${value[1].toFixed(1)}千卡`,
`供能比:${percent}%`,
];
break;
case "calories":
tooltips = [
`${marker} ${name}`,
`热量:${value[1].toFixed(1)}千卡`,
`占比:${percent}%`,
];
break;
case "weight":
tooltips = [
`${marker} ${name}`,
`质量:${value[1].toFixed(1)}`,
`占比:${percent}%`,
];
break;
}
return tooltips.join("</br>");
},
updateChart(source) {
console.log(source);
const total = source.reduce((acc, cur) => acc + cur[1], 0);
this.chart.clear();
const option = {
title: {
text: this.title,
subtext: this.subTitle,
},
legend: {
left: 100,
itemWidth: 8,
itemHeight: 8,
pageIconSize: 10,
textStyle: {
fontSize: 10,
},
},
dataset: {
source,
},
tooltip: {
trigger: "item",
appendToBody: true,
formatter: this.getTooltipArr,
},
graphic: [
{
type: "group",
top: 60,
left: 10,
silent: true,
children: [
{
type: "text",
style: {
text: this.type === "weight" ? "总质量" : "总热量约",
fill: "#606266",
},
},
{
type: "text",
top: 18,
left: 8,
style: {
text: `${total.toFixed(1)}${
this.type === "weight" ? "克" : "千卡"
}`,
font: '14px "Microsoft YaHei", sans-serif',
},
},
],
},
{
type: "group",
top: 36,
right: 10,
silent: true,
children: source.map((item, idx) => {
const data =
this.type == "nutrition"
? item[1] / (idx === 1 ? 9 : 4)
: item[1];
return {
type: "text",
top: idx * 20,
right: 10,
style: {
text: `${item[0]}${data.toFixed(1)}${
this.type === "calories" ? "千卡" : "克"
}`,
fill: "#606266",
},
};
}),
},
],
series: [
{
type: "pie",
radius: [0, 60],
center: ["50%", "55%"],
// labelLine: {
// length: 5,
// length2: 5,
// },
label: {
show: true,
position: "inside",
color: "#fff",
fontSize: 12,
fontWeight: "bold",
},
itemStyle: {
borderWidth: 1,
borderColor: "#fff",
},
},
],
};
// console.log(option);
this.chart.setOption(option);
},
},
watch: {
data(newVal, oldVal) {
if (newVal) {
this.$nextTick(() => {
this.updateChart(newVal);
});
}
},
},
};
</script>
<style lang="scss" scoped>
/deep/ :focus {
outline: 0;
}
.aspect_pie_chart_wrapper {
width: 100%;
display: flex;
position: relative;
& > div:nth-child(1) {
// width: 200px
}
.small_table {
.my_cell {
padding: 2px 0 !important;
}
}
.summary {
padding: 2px;
border-bottom: 1px solid #dfe6ec;
border-left: 1px solid #dfe6ec;
border-right: 1px solid #dfe6ec;
& > div {
padding: 3px;
text-align: center;
}
}
.see_all {
position: absolute;
bottom: 4px;
left: 24px;
padding: 0;
}
.icon_btns {
position: absolute;
right: 0;
em {
display: inline-block;
padding: 4px;
cursor: pointer;
}
.sel_icon {
color: #1890ff;
}
}
.table_zone {
margin-top: 26px;
}
}
</style>

View File

@ -0,0 +1,110 @@
<template>
<div class="daily_analyze_com_wrapper">
<pie-chart
title="营养统计"
:data="nutritionSource"
type="nutrition"
:subTitle="subTitle"
/>
<pie-chart
title="热量统计"
:data="caloriesSource"
type="calories"
:subTitle="subTitle"
/>
<pie-chart
title="质量统计"
:data="weightSource"
type="weight"
:subTitle="subTitle"
/>
<el-button size="mini" type="text" @click="backToAll" class="see_all"
>查看全部</el-button
>
</div>
</template>
<script>
import PieChart from "./PieChart";
import { createNamespacedHelpers } from "vuex";
const { mapGetters, mapMutations } = createNamespacedHelpers("recipes");
export default {
name: "DailyAnalyzeCom",
data() {
return {
subTitle: "",
};
},
components: {
PieChart,
},
methods: {
backToAll() {
this.resetCurrentDay({ currentDay: -1 });
},
...mapMutations(["resetCurrentDay"]),
},
computed: {
nutritionSource() {
const [data] = this.analyseData;
this.subTitle = data.name;
return [
["蛋白质", data.pCalories],
["脂肪", data.fCalories],
["碳水", data.cCalories],
];
},
caloriesSource() {
const [data] = this.analyseData;
const source = [
["早餐", data.calories1],
["午餐", data.calories3],
["晚餐", data.calories5],
];
if (data.calories2) {
source.push(["早加餐", data.calories2]);
}
if (data.calories4) {
source.push(["午加餐", data.calories4]);
}
if (data.calories6) {
source.push(["晚加餐", data.calories6]);
}
return source;
},
weightSource() {
const [data] = this.analyseData;
const source = [
["早餐", data.weight1],
["午餐", data.weight3],
["晚餐", data.weight5],
];
if (data.weight2) {
source.push(["早加餐", data.weight2]);
}
if (data.weight4) {
source.push(["午加餐", data.weight4]);
}
if (data.weight6) {
source.push(["晚加餐", data.weight6]);
}
return source;
},
...mapGetters(["analyseData"]),
},
};
</script>
<style lang="scss" scoped>
.daily_analyze_com_wrapper {
position: relative;
& > div:not(:nth-child(1)) {
margin-top: 12px;
}
.see_all {
position: absolute;
top: 0;
right: 0;
}
}
</style>