-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reactivity of Pie and Bar charts
- Loading branch information
Showing
5 changed files
with
142 additions
and
149 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,78 @@ | ||
<template> | ||
<div> | ||
<Pie :data="chartData" :options="chartOptions" /> | ||
</div> | ||
<canvas ref="canvasRef" id="myChart"></canvas> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { Pie } from 'vue-chartjs' | ||
import autocolors from 'chartjs-plugin-autocolors' | ||
import { Chart as ChartJS, ArcElement, Tooltip, Title } from 'chart.js' | ||
import type { ChartDataset } from '@/types' | ||
import type { ScreenTimeSummary } from '@/types' | ||
import { ref } from 'vue' | ||
<script setup lang="ts"> | ||
import { ref, watch } from 'vue'; | ||
import type { ScreenTimeSummary } from '@/types'; | ||
import autocolors from 'chartjs-plugin-autocolors'; | ||
import { Chart as ChartJS, ArcElement,PieController, Tooltip, Title } from 'chart.js'; | ||
ChartJS.register(autocolors, ArcElement, Tooltip, Title) | ||
ChartJS.register(autocolors, ArcElement, PieController, Tooltip, Title); | ||
const props = defineProps({ | ||
summaries: { | ||
type: Array as () => ScreenTimeSummary[] | null, | ||
required: true | ||
} | ||
}) | ||
const chartData = ref({ | ||
labels: [] as string[], | ||
datasets: [] as ChartDataset[] | ||
}) | ||
const chartOptions = { | ||
responsive: true, | ||
title: { | ||
display: true, | ||
text: 'Category time' | ||
}, | ||
plugins: { | ||
autocolors: { | ||
enabled: true, | ||
mode: 'data' as const | ||
} | ||
} | ||
} | ||
const summaries = ref(props.summaries) | ||
const uniqueCategoriesSet: Set<string> = new Set() | ||
summaries.value?.forEach((summary) => { | ||
for (const category in summary.categoryTotals) { | ||
uniqueCategoriesSet.add(category) | ||
} | ||
}) | ||
const uniqueCategories = Array.from(uniqueCategoriesSet) | ||
const categoryTotals: number[] = [] | ||
for (const category of uniqueCategories) { | ||
const categoryTotal = summaries.value?.reduce((acc, summary) => { | ||
return acc + (summary.categoryTotals[category] || 0) | ||
}, 0) | ||
categoryTotals.push(categoryTotal || 0) | ||
interface Props { | ||
summaries: ScreenTimeSummary[] | null; | ||
} | ||
chartData.value.labels = uniqueCategories | ||
chartData.value.datasets = [ | ||
{ | ||
label: 'Category time', | ||
data: categoryTotals | ||
const props = defineProps<Props>(); | ||
const canvasRef = ref<HTMLCanvasElement | null>(null); | ||
const getChartData = () => { | ||
const uniqueCategoriesSet: Set<string> = new Set(); | ||
props.summaries?.forEach((summary) => { | ||
for (const category in summary.categoryTotals) { | ||
uniqueCategoriesSet.add(category); | ||
} | ||
}); | ||
const uniqueCategories = Array.from(uniqueCategoriesSet); | ||
const categoryTotals: number[] = []; | ||
for (const category of uniqueCategories) { | ||
const categoryTotal = props.summaries?.reduce((acc, summary) => { | ||
return acc + (summary.categoryTotals[category] || 0); | ||
}, 0); | ||
categoryTotals.push(categoryTotal || 0); | ||
} | ||
] | ||
</script> | ||
return { | ||
labels: uniqueCategories, | ||
datasets: [ | ||
{ | ||
label: 'Category time', | ||
data: categoryTotals | ||
} | ||
] | ||
}; | ||
}; | ||
<style scoped> | ||
canvas { | ||
width: 400px; | ||
height: 400px; | ||
} | ||
</style> | ||
const renderChart = () => { | ||
const canvas = canvasRef.value; | ||
if (!canvas) return; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) return; | ||
ChartJS.getChart(canvas)?.destroy(); | ||
new ChartJS(ctx, { | ||
type: 'pie', | ||
data: getChartData(), | ||
options: { | ||
responsive: true, | ||
plugins: { | ||
autocolors: { | ||
enabled: true, | ||
mode: 'data' | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
watch( | ||
() => props.summaries, | ||
() => { | ||
console.log('summaries changed'); | ||
renderChart(); | ||
}, | ||
{ deep: true } | ||
); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters