Skip to content

Commit

Permalink
Complete storePetTypeChart Components
Browse files Browse the repository at this point in the history
  • Loading branch information
fan9704 committed Mar 24, 2023
1 parent 89147ee commit 41321f1
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions src/components/chartComponents/storePetTypeChart.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<template>
<div class="line-chart" ref="pieChart"></div>
</template>

<script>
import * as echarts from 'echarts';
import {reactive, ref, onUnmounted, watch, onMounted, onBeforeMount} from 'vue';
import axios from 'axios';
const state = reactive({
client: null,
});
export default {
name: "storePetTypeChart",
setup() {
const pieChart = ref(null);
let myPieChart =null;
const chartData = ref(
[{ value: 1048, name: 'Search Engine' },
{ value: 735, name: 'Direct' },
{ value: 580, name: 'Email' },
{ value: 484, name: 'Union Ads' },
{ value: 300, name: 'Video Ads' }]
);
onBeforeMount(()=>{
});
onMounted(async () => {
const res = await axios.get(`/api/pet/count/petType`)
.then(({data})=>{
const v = []
for (const pet in data) {
v.push({value:data[pet],name:pet})
}
chartData.value=v ;
myPieChart = echarts.init(pieChart.value);
updateChart();
})
.catch((err)=>{console.log(err)})
});
onUnmounted(() => {
if (myPieChart != null) {
myPieChart.dispose();
myPieChart = null;
}
});
watch(chartData, () => {
// updateChart();
});
function updateChart() {
const pieChartOption = {
tooltip: {
trigger: 'item'
},
legend: {
top: '5%',
left: 'center'
},
series: [
{
name: 'Access From',
type: 'pie',
radius: ['40%', '70%'],
avoidLabelOverlap: false,
itemStyle: {
color: '#c23531',
},
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: 40,
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: chartData.value
}
]
};
myPieChart.setOption(pieChartOption);
}
return {
pieChart,
state,
};
},
data() {
return {
theme: 'light'
}
},
beforeMount() {
}
};
</script>
<style>
.line-chart-container {
width: 100%;
height: 500px;
display: flex;
justify-content: center;
align-items: center;
}
.line-chart {
width: 100%;
height: 100%;
}
</style>

0 comments on commit 41321f1

Please sign in to comment.