add pie chart

This commit is contained in:
iamBadgers
2024-05-20 19:17:34 -07:00
parent 41ba017174
commit e2e42b8a4f
3 changed files with 59 additions and 9 deletions

View File

@@ -24,8 +24,8 @@
>
</div>
</div>
<div class="role-table d-flex flex-column">
<v-table>
<div class="role-table d-flex flex-row">
<v-table class="flex-1-1">
<thead>
<tr>
<th class="text-left">Role</th>
@@ -36,15 +36,22 @@
</tr>
</thead>
<tbody>
<tr v-for="roleStat, roleName in roleStats">
<tr v-for="(roleStat, roleName) in roleStats">
<td>{{ roleName }}</td>
<td class="text-center">{{ roleStat.active }}</td>
<td class="text-center">{{ roleStat.apps }}</td>
<td class="text-center">{{ roleStat.picks }}</td>
<td class="text-center">{{ Math.floor((roleStat.picks / gameStats.Complete) * 100)}}%</td>
<td class="text-center">
{{ Math.floor((roleStat.picks / gameStats.Complete) * 100) }}%
</td>
</tr>
</tbody>
</v-table>
<div class="chart d-flex flex-column">
<div class="flex-1-1"></div>
<canvas id="piechart" class="chart"></canvas>
<div class="flex-1-1"></div>
</div>
</div>
</div>
</template>
@@ -71,10 +78,15 @@
margin-left: 15px;
margin-right: 15px;
}
.chart {
width: 25%;
min-width: 500px;
}
</style>
<script setup lang="ts">
import { onMounted, watch, ref } from 'vue'
import Chart from 'chart.js/auto'
import axios from 'axios'
const dateSelect = ref(-1)
@@ -85,6 +97,8 @@ const gameStats = ref({})
const roleStats = ref({})
let chart
async function loadData() {
const gameStatsResponse = await axios.post('/api/serverstats/gamestats', {
monthId: dateSelect.value
@@ -98,11 +112,10 @@ async function loadData() {
}
function buildDateItems() {
const items = [{title: "All Time", value: -1}]
const items = [{ title: 'All Time', value: -1 }]
const date = new Date()
while (date.getUTCFullYear() != 2023 || date.getUTCMonth() != 0) {
console.log(buildDateEntry(date))
items.push(buildDateEntry(date))
date.setUTCMonth(date.getUTCMonth() - 1)
}
@@ -114,21 +127,40 @@ function buildDateItems() {
function buildDateEntry(date: Date) {
const monthId = dateToMonthId(date)
return {
title: date.toLocaleString('en-us',{month:'short', year:'numeric'}),
title: date.toLocaleString('en-us', { month: 'short', year: 'numeric' }),
value: monthId
}
}
function dateToMonthId(date: Date) {
return (((date.getUTCFullYear() - 2023) * 12) + (date.getUTCMonth()))
return (date.getUTCFullYear() - 2023) * 12 + date.getUTCMonth()
}
watch(dateSelect, async (newValue, oldValue) => {
loadData()
})
watch(roleStats, async (newValue, oldValue) => {
chart.data = {
labels: Object.keys(roleStats.value),
datasets: [
{
label: 'Active',
data: Object.values(roleStats.value).map((p) => p.active)
}
]
}
chart.update()
})
onMounted(async () => {
loadData()
console.log(buildDateItems())
chart = new Chart(document.getElementById('piechart'), {
type: 'pie',
data: {
labels: [],
datasets: []
}
})
})
</script>