|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useTitle } from '@vueuse/core'; |
| 3 | +import { BarElement, CategoryScale, Chart as ChartJS, ChartData, Legend, LinearScale, Title, Tooltip } from 'chart.js'; |
| 4 | +import dayjs from "dayjs"; |
| 5 | +import { Ref, ref, watch } from "vue"; |
| 6 | +import { Bar } from 'vue-chartjs'; |
| 7 | +import { useI18n } from 'vue-i18n'; |
| 8 | +import dataService from "../services/DataService"; |
| 9 | +
|
| 10 | +const { t } = useI18n({ |
| 11 | + inheritLocale: true, |
| 12 | + useScope: 'global' |
| 13 | + }) |
| 14 | +
|
| 15 | +useTitle('Jelu | Stats') |
| 16 | +console.log(dayjs('2020-1-1').format('MMMM')) |
| 17 | +console.log(dayjs('2020-3-1').format('MMMM')) |
| 18 | +
|
| 19 | +ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale) |
| 20 | +
|
| 21 | +const getYears = () => { |
| 22 | + dataService.yearsWithStats() |
| 23 | + .then(res => { |
| 24 | + years.value = res |
| 25 | + }) |
| 26 | + .catch(e => { |
| 27 | + console.log(e) |
| 28 | + }) |
| 29 | + |
| 30 | +} |
| 31 | +
|
| 32 | +const getAllStats = () => { |
| 33 | + dataService.yearStats() |
| 34 | + .then(res => { |
| 35 | + let labels = res.map(r => r.year) |
| 36 | +
|
| 37 | + const updatedChartData = { |
| 38 | + labels: labels, |
| 39 | + datasets: [ |
| 40 | + { |
| 41 | + label: 'finished', |
| 42 | + backgroundColor: '#bbbbbb', |
| 43 | + data: res.map(r => r.finished) |
| 44 | + }, |
| 45 | + { |
| 46 | + label: 'dropped', |
| 47 | + backgroundColor: '#f87979', |
| 48 | + data: res.map(r => r.dropped) |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + chartData.value = { ...updatedChartData } |
| 53 | + }) |
| 54 | + .catch(e => { |
| 55 | + console.log(e) |
| 56 | + }) |
| 57 | +} |
| 58 | +
|
| 59 | +const getYearStats = () => { |
| 60 | + if (currentYear.value != null) { |
| 61 | + dataService.monthStatsForYear(currentYear.value) |
| 62 | + .then(res => { |
| 63 | + let labels = res.map(r => r.month).map(m => dayjs(`2020-${m}-1`).format('MMMM')) |
| 64 | + |
| 65 | + const updatedChartData = { |
| 66 | + labels: labels, |
| 67 | + datasets: [ |
| 68 | + { |
| 69 | + label: 'finished', |
| 70 | + backgroundColor: '#bbbbbb', |
| 71 | + data: res.map(r => r.finished) |
| 72 | + }, |
| 73 | + { |
| 74 | + label: 'dropped', |
| 75 | + backgroundColor: '#f87979', |
| 76 | + data: res.map(r => r.dropped) |
| 77 | + } |
| 78 | + ] |
| 79 | + } |
| 80 | + yearChartData.value = { ...updatedChartData } |
| 81 | + }) |
| 82 | + .catch(e => { |
| 83 | + console.log(e) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | +
|
| 88 | +const loaded = ref(false) |
| 89 | +const chartData = ref<ChartData<'bar'>>({ |
| 90 | + datasets: [] |
| 91 | + }) |
| 92 | +const yearChartData = ref<ChartData<'bar'>>({ |
| 93 | + datasets: [] |
| 94 | + }) |
| 95 | +
|
| 96 | +const years: Ref<Array<number>> = ref([]) |
| 97 | +const currentYear: Ref<number|null> = ref(null) |
| 98 | +
|
| 99 | +watch(currentYear, (newVal, oldVal) => { |
| 100 | + console.log("year " + newVal + " " + oldVal) |
| 101 | + getYearStats() |
| 102 | + |
| 103 | +}) |
| 104 | +
|
| 105 | +getAllStats() |
| 106 | +getYears() |
| 107 | +
|
| 108 | +</script> |
| 109 | + |
| 110 | +<template> |
| 111 | + <div class="grid grid-cols-1 justify-center justify-items-center justify-self-center"> |
| 112 | + <h1 class="text-2xl typewriter w-11/12 sm:w-8/12 pb-4 capitalize"> |
| 113 | + {{ t('stats.all_time') }} |
| 114 | + </h1> |
| 115 | + <div class=""> |
| 116 | + <Bar |
| 117 | + :chart-data="chartData" |
| 118 | + /> |
| 119 | + </div> |
| 120 | + <h1 class="text-2xl typewriter w-11/12 sm:w-8/12 py-4 capitalize"> |
| 121 | + {{ t('stats.yearly_stats') }} |
| 122 | + </h1> |
| 123 | + <div v-if="years != null && years !== undefined && years.length > 0" class=""> |
| 124 | + <select |
| 125 | + v-model="currentYear" |
| 126 | + class="select select-bordered select-accent pt-2 mb-4" |
| 127 | + > |
| 128 | + <option |
| 129 | + disabled |
| 130 | + selected |
| 131 | + > |
| 132 | + {{ t('stats.choose_year') }} |
| 133 | + </option> |
| 134 | + <option |
| 135 | + v-for="year in years" |
| 136 | + :key="year" |
| 137 | + :value="year" |
| 138 | + > |
| 139 | + {{ year }} |
| 140 | + </option> |
| 141 | + </select> |
| 142 | + <Bar |
| 143 | + :chart-data="yearChartData" |
| 144 | + /> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | +</template> |
| 148 | + |
| 149 | +<style scoped> |
| 150 | +
|
| 151 | +
|
| 152 | +</style> |
0 commit comments