Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize statistics chart render #482

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/components/common/Setting/Statistics.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang='ts'>
import type { Ref } from 'vue'
import { nextTick, onMounted, reactive, ref } from 'vue'
import { onMounted, onUnmounted, reactive, ref } from 'vue'
import { NCol, NDatePicker, NIcon, NNumberAnimation, NRow, NSelect, NSpin, NStatistic } from 'naive-ui'
import type { ChartData, ChartOptions } from 'chart.js'
import { BarElement, CategoryScale, Chart as ChartJS, Legend, LinearScale, Title, Tooltip } from 'chart.js'
Expand All @@ -14,6 +14,8 @@ import { useUserStore } from '@/store'

ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)

const statisticsChart = ref<typeof Bar | null>(null)

const userStore = useUserStore()

const chartData: ChartData<'bar'> = reactive({
Expand All @@ -37,6 +39,7 @@ const chartData: ChartData<'bar'> = reactive({
})
const chartOptions: ChartOptions<'bar'> = {
responsive: true,
aspectRatio: window.innerWidth / window.innerHeight * 1.5,
}
const summary = ref({
promptTokens: 0,
Expand Down Expand Up @@ -69,8 +72,6 @@ rangeShortcuts[t('setting.statisticsPeriodLast30Days')] = [
dayjs().endOf('day').valueOf(),
]

const showChart = ref(true)

async function fetchStatistics() {
try {
loading.value = true
Expand All @@ -89,11 +90,7 @@ async function fetchStatistics() {
chartData.datasets[0].data = data.chartData.map((item: any) => item.promptTokens)
chartData.datasets[1].data = data.chartData.map((item: any) => item.completionTokens)

// todo: don't know why data change won't trigger chart re-render, dirty hack
showChart.value = false
nextTick(() => {
showChart.value = true
})
reRenderChart()
}
}
finally {
Expand All @@ -112,15 +109,29 @@ async function fetchUsers() {
})
}

function reRenderChart() {
if (statisticsChart.value) {
chartOptions.aspectRatio = window.innerWidth / window.innerHeight * 1.5
statisticsChart.value.chart.options = chartOptions
statisticsChart.value.chart.data = chartData
statisticsChart.value.chart.update()
}
}

function filter(pattern: string, option: object): boolean {
const a = option as { label: string; filter: string; value: string }
return !a.filter ? false : a.filter.includes(pattern)
}

onMounted(() => {
window.addEventListener('resize', reRenderChart)
fetchStatistics()
fetchUsers()
})

onUnmounted(() => {
window.removeEventListener('resize', reRenderChart)
})
</script>

<template>
Expand Down Expand Up @@ -184,9 +195,8 @@ onMounted(() => {
</div>

<Bar
v-if="showChart && chartData.labels?.length"
v-if="chartData.labels?.length"
ref="statisticsChart"
style="aspect-ratio: 3/2;"
:options="chartOptions"
:data="chartData"
/>
Expand Down
Loading