完成菜品修改
This commit is contained in:
@ -0,0 +1,146 @@
|
||||
<template>
|
||||
<div :class="className" :style="{ height: height, width: width }" />
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import echarts from "echarts";
|
||||
require("@/utils/echarts/myShine");
|
||||
import resize from "@/views/dashboard/mixins/resize";
|
||||
|
||||
const animationDuration = 6000;
|
||||
|
||||
export default {
|
||||
mixins: [resize],
|
||||
props: {
|
||||
className: {
|
||||
type: String,
|
||||
default: "chart",
|
||||
},
|
||||
width: {
|
||||
type: String,
|
||||
default: "100%",
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
default: "300px",
|
||||
},
|
||||
data: {
|
||||
type: Array,
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
chart: null,
|
||||
nameDict: {
|
||||
pHeat: "蛋白质",
|
||||
fHeat: "脂肪",
|
||||
cHeat: "碳水",
|
||||
},
|
||||
};
|
||||
},
|
||||
mounted() {
|
||||
this.$nextTick(() => {
|
||||
this.initChart();
|
||||
});
|
||||
},
|
||||
beforeDestroy() {
|
||||
if (!this.chart) {
|
||||
return;
|
||||
}
|
||||
this.chart.dispose();
|
||||
this.chart = null;
|
||||
},
|
||||
updated() {
|
||||
// console.log("updated");
|
||||
},
|
||||
methods: {
|
||||
initChart() {
|
||||
this.chart = echarts.init(this.$el, "myShine");
|
||||
this.updateChart(this.data.length > 0 ? this.data : {});
|
||||
},
|
||||
updateChart(source) {
|
||||
this.chart.clear();
|
||||
this.chart.setOption({
|
||||
title: {
|
||||
text: "营养统计",
|
||||
},
|
||||
tooltip: {
|
||||
trigger: "axis",
|
||||
appendToBody: true,
|
||||
formatter: (params) => {
|
||||
// console.log(params);
|
||||
const [param] = params;
|
||||
const { name } = param;
|
||||
let totalHeat = 0;
|
||||
const tooltips = params.reduce(
|
||||
(arr, cur) => {
|
||||
const { value, seriesName } = cur;
|
||||
const nutriName = this.nameDict[seriesName];
|
||||
totalHeat += value[seriesName];
|
||||
const heatVal = value[seriesName].toFixed(1);
|
||||
const weightVal = value[
|
||||
`${seriesName.substring(0, 1)}Weight`
|
||||
].toFixed(1);
|
||||
arr.push(
|
||||
`${cur.marker} ${nutriName}:${heatVal}千卡(${weightVal}克)`
|
||||
);
|
||||
return arr;
|
||||
},
|
||||
[name]
|
||||
);
|
||||
tooltips[0] += ` - 共${totalHeat.toFixed(1)}千卡`;
|
||||
return tooltips.join("</br>");
|
||||
},
|
||||
},
|
||||
dataset: {
|
||||
dimensions: [
|
||||
"name",
|
||||
"pWeight",
|
||||
"pHeat",
|
||||
"fWeight",
|
||||
"fHeat",
|
||||
"cWeight",
|
||||
"cHeat",
|
||||
],
|
||||
source,
|
||||
},
|
||||
grid: {
|
||||
top: 40,
|
||||
left: 20,
|
||||
right: 20,
|
||||
bottom: 10,
|
||||
containLabel: true,
|
||||
},
|
||||
xAxis: {
|
||||
type: "category",
|
||||
},
|
||||
yAxis: {
|
||||
type: "value",
|
||||
},
|
||||
series: ["pHeat", "fHeat", "cHeat"].map((dim, idx) => ({
|
||||
name: dim,
|
||||
type: "bar",
|
||||
barWidth: 26,
|
||||
stack: "bar",
|
||||
encode: {
|
||||
y: dim,
|
||||
x: 0,
|
||||
},
|
||||
itemStyle: {
|
||||
borderWidth: 2,
|
||||
borderColor: "#fff",
|
||||
},
|
||||
})),
|
||||
});
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
data(newVal, oldVal) {
|
||||
if (newVal) {
|
||||
this.updateChart(newVal);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
@ -0,0 +1,200 @@
|
||||
<template>
|
||||
<div
|
||||
:class="`aspect_pie_chart_wrapper ${className || ''}`"
|
||||
:style="{ height: height, width: width }"
|
||||
>
|
||||
<div ref="echart" :style="{ height: height, width: '200px' }" />
|
||||
<div>
|
||||
<el-table
|
||||
:data="mData"
|
||||
size="mini"
|
||||
border
|
||||
:cell-style="{ padding: '2px 0' }"
|
||||
:header-cell-style="{ padding: '4px 0', height: 'unset' }"
|
||||
class="small_table"
|
||||
>
|
||||
<el-table-column label="营养" prop="type" align="center" width="60" />
|
||||
<el-table-column
|
||||
label="重量(g)"
|
||||
prop="weight"
|
||||
align="center"
|
||||
width="80"
|
||||
/>
|
||||
<el-table-column
|
||||
label="热量(Kcal)"
|
||||
prop="heat"
|
||||
align="center"
|
||||
width="90"
|
||||
/>
|
||||
<el-table-column
|
||||
label="热量占比"
|
||||
prop="heatRate"
|
||||
align="center"
|
||||
width="80"
|
||||
/>
|
||||
</el-table>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import echarts from "echarts";
|
||||
require("@/utils/echarts/myShine");
|
||||
import resize from "@/views/dashboard/mixins/resize";
|
||||
import TextInfo from "@/components/TextInfo";
|
||||
|
||||
export default {
|
||||
mixins: [resize],
|
||||
components: {
|
||||
TextInfo,
|
||||
},
|
||||
props: {
|
||||
className: {
|
||||
type: String,
|
||||
default: "chart",
|
||||
},
|
||||
width: {
|
||||
type: String,
|
||||
default: "100%",
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
default: "300px",
|
||||
},
|
||||
data: {
|
||||
type: Array,
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
chart: null,
|
||||
nameDict: {
|
||||
p: "蛋白质",
|
||||
f: "脂肪",
|
||||
c: "碳水",
|
||||
},
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
mData() {
|
||||
const [data] = this.data;
|
||||
let totalHeat = 0;
|
||||
return data
|
||||
? ["p", "f", "c"].map((type) => {
|
||||
if (totalHeat === 0) {
|
||||
totalHeat = ["p", "f", "c"].reduce((heat, cur) => {
|
||||
heat += data[`${cur}Heat`];
|
||||
return heat;
|
||||
}, 0);
|
||||
}
|
||||
return {
|
||||
type: this.nameDict[type],
|
||||
weight: data[`${type}Weight`].toFixed(1),
|
||||
heat: data[`${type}Heat`].toFixed(1),
|
||||
heatRate: `${((data[`${type}Heat`] / totalHeat) * 100).toFixed(
|
||||
2
|
||||
)}%`,
|
||||
};
|
||||
})
|
||||
: [];
|
||||
},
|
||||
},
|
||||
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");
|
||||
this.updateChart(this.data.length > 0 ? this.data[0] : {});
|
||||
},
|
||||
updateChart(data) {
|
||||
this.chart.clear();
|
||||
this.chart.setOption({
|
||||
title: {
|
||||
text: `${data.name}营养统计`,
|
||||
},
|
||||
tooltip: {
|
||||
trigger: "item",
|
||||
appendToBody: true,
|
||||
formatter: (params) => {
|
||||
const {
|
||||
name,
|
||||
marker,
|
||||
percent,
|
||||
data: { value, oriData, dim },
|
||||
} = params;
|
||||
return [
|
||||
`${marker} ${name}`,
|
||||
`含量:${oriData[`${dim}Weight`].toFixed(1)}克`,
|
||||
`热量:${value.toFixed(1)}千卡`,
|
||||
`热量占比:${percent}%`,
|
||||
].join("</br>");
|
||||
},
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: data.name,
|
||||
type: "pie",
|
||||
radius: [0, 50],
|
||||
center: ["50%", "50%"],
|
||||
data: ["p", "f", "c"].map((dim) => ({
|
||||
dim,
|
||||
value: data[`${dim}Heat`],
|
||||
name: this.nameDict[dim],
|
||||
oriData: data,
|
||||
})),
|
||||
labelLine: {
|
||||
length: 5,
|
||||
length2: 5,
|
||||
},
|
||||
// label: {
|
||||
// show: true,
|
||||
// position: "inside",
|
||||
// color: '#fff'
|
||||
// },
|
||||
itemStyle: {
|
||||
borderWidth: 1,
|
||||
borderColor: "#fff",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
data(newVal, oldVal) {
|
||||
if (newVal) {
|
||||
this.updateChart(newVal[0]);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
<style lang="scss" scoped>
|
||||
.aspect_pie_chart_wrapper {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
|
||||
& > div:nth-child(1) {
|
||||
// width: 200px
|
||||
}
|
||||
|
||||
// & > div:nth-child(2) {
|
||||
.small_table {
|
||||
.my_cell {
|
||||
padding: 2px 0 !important;
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
</style>
|
@ -0,0 +1,62 @@
|
||||
<template>
|
||||
<div
|
||||
class="recipes_aspect_wrapper"
|
||||
:style="`height: ${collapse ? 30 : 200}px`"
|
||||
>
|
||||
<div class="header">
|
||||
<el-button size="mini" type="text" @click="handleCollapseClick">{{
|
||||
`${collapse ? "展开分析" : "收起分析"}`
|
||||
}}</el-button>
|
||||
</div>
|
||||
<div
|
||||
class="content"
|
||||
:style="`visibility: ${collapse ? 'hidden' : 'visible'};`"
|
||||
>
|
||||
<BarChart
|
||||
v-if="data.length > 1"
|
||||
:data="data"
|
||||
height="170px"
|
||||
width="500px"
|
||||
/>
|
||||
<PieChart v-else :data="data" height="170px" width="500px" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import BarChart from "./BarChart";
|
||||
import PieChart from "./PieChart";
|
||||
export default {
|
||||
name: "RecipesAspectCom",
|
||||
components: {
|
||||
BarChart,
|
||||
PieChart,
|
||||
},
|
||||
data() {
|
||||
return {};
|
||||
},
|
||||
updated() {
|
||||
// console.log(this.data);
|
||||
},
|
||||
props: ["collapse", "data"],
|
||||
computed: {},
|
||||
methods: {
|
||||
handleCollapseClick() {
|
||||
this.$emit("update:collapse", !this.collapse);
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
<style rel="stylesheet/scss" lang="scss" scope>
|
||||
.recipes_aspect_wrapper {
|
||||
transition: all 0.3s;
|
||||
padding-bottom: 12px;
|
||||
|
||||
.header {
|
||||
text-align: right;
|
||||
height: 30px;
|
||||
}
|
||||
|
||||
.content {
|
||||
}
|
||||
}
|
||||
</style>
|
Reference in New Issue
Block a user