add: charts more used to dashboard

This commit is contained in:
Alexandro Uc Santos
2024-02-01 15:12:24 -06:00
parent 921591da10
commit 79b5cc0f13
4 changed files with 263 additions and 67 deletions

View File

@@ -0,0 +1,92 @@
<script setup>
import { onMounted, ref } from 'vue';
import { Doughnut } from 'vue-chartjs'
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, LinearScale, CategoryScale, ArcElement } from 'chart.js'
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale, ArcElement)
const props = defineProps({
data: {
type: Array,
required: true
},
dataModel: {
type: Array,
},
targetFind: {
type: String
},
targetLabel: {
type: String
}
})
const chartData = ref(null);
const dataMap = ref([]);
onMounted(() => {
props.data.forEach(item => {
const index = dataMap.value.findIndex((e) => e.label === item);
console.log(index);
if(index === -1) {
if(props.dataModel) {
const itemModel = props.dataModel.find((e) => e[props.targetFind] === item);
dataMap.value.push({
label: (props.targetLabel) ? itemModel[props.targetLabel] : item,
data: 1,
...itemModel
})
} else {
dataMap.value.push({
label: item,
data: 1,
color: 'green'
});
}
} else {
dataMap.value[index].data += 1;
}
});
console.log(dataMap.value)
chartData.value = {
labels: dataMap.value.map((e) => e.label),
datasets: [{
data: dataMap.value.map((e) => e.data),
backgroundColor: dataMap.value.map((e) => e.color),
}],
}
})
const chartOptions = {
responsive: true,
}
</script>
<template>
<div class="card-chart">
<Doughnut
id="my-chart-statictics"
v-if="chartData"
:options="chartOptions"
:data="chartData"
/>
</div>
</template>
<style scoped>
.card-chart {
width: 100%;
display: flex;
flex-direction: column;
}
@media (max-width: 768px) {
.card-dashboard {
width: 100%;
}
}
</style>