Skip to content

Open source personal finance application

feat(stat): add pie chart view, category series builder, and chart config

Ilya Komichev85a071587607
diff --git a/app/app/components/stat/Item.vue b/app/app/components/stat/Item.vue
index 2c224965..a984353f 100644
--- a/app/app/components/stat/Item.vue
+++ b/app/app/components/stat/Item.vue
@@ -35,6 +35,7 @@ const isTrnsShow = computed(() => statConfig.config.value.trns.isShow)
const {
averageTotal,
+ chartPieGroups,
chartSeries,
chartXAxisLabels,
filteredCategoriesIds,
@@ -110,9 +111,11 @@ function onClickSumItemWrap(type: SeriesSlugSelected) {
<div class="@container/stat">
<StatChartWrap
v-if="shouldShowAmounts"
+ :pieGroups="chartPieGroups"
:series="chartSeries"
:xAxisLabels="chartXAxisLabels"
class="pb-3"
+ @clickCategory="onSetCategoryFilter"
/>
<div class="grid min-w-0 content-start gap-3">
diff --git a/app/app/components/stat/chart/PieView.vue b/app/app/components/stat/chart/PieView.vue
new file mode 100644
index 00000000..d03440ad
--- /dev/null
+++ b/app/app/components/stat/chart/PieView.vue
@@ -0,0 +1,176 @@
+<script setup lang="ts">
+import type { ECElementEvent } from 'echarts/core'
+
+import { PieChart } from 'echarts/charts'
+import { TooltipComponent } from 'echarts/components'
+import { use } from 'echarts/core'
+import { SVGRenderer } from 'echarts/renderers'
+import VChart from 'vue-echarts'
+
+import type { CategoryId } from '~/components/categories/types'
+import type { CategoryPieDatum } from '~/components/stat/chart/useCategorySeriesBuilder'
+
+import { OTHER_SLICE_ID } from '~/components/stat/chart/useCategorySeriesBuilder'
+import { formatChartAmount } from '~/components/stat/chart/utils'
+
+type PieTooltipParam = {
+ color: string
+ name: string
+ percent: number
+ value: number
+}
+
+type PieClickParam = {
+ data?: { id?: CategoryId, isOther?: boolean }
+}
+
+const { pieData, showLegend = true, typeLabel } = defineProps<{
+ pieData: CategoryPieDatum[]
+ showLegend?: boolean
+ typeLabel: string
+}>()
+
+const emit = defineEmits<{
+ clickCategory: [id: CategoryId]
+}>()
+
+use([
+ PieChart,
+ SVGRenderer,
+ TooltipComponent,
+])
+
+const { locale, t } = useI18n()
+
+const total = computed(() => pieData.reduce((acc, item) => acc + item.value, 0))
+const hasData = computed(() => pieData.length > 0 && total.value > 0)
+
+const option = computed(() => ({
+ series: [{
+ avoidLabelOverlap: false,
+ center: ['50%', '50%'],
+ data: pieData.map(item => ({
+ cursor: item.isOther ? 'default' : 'pointer',
+ id: item.id,
+ isOther: item.isOther ?? false,
+ itemStyle: { color: item.color },
+ name: item.name,
+ value: item.value,
+ })),
+ emphasis: { focus: 'self' },
+ itemStyle: {
+ borderColor: 'var(--ui-bg)',
+ borderWidth: 2,
+ },
+ label: { show: false },
+ labelLine: { show: false },
+ minAngle: 3,
+ radius: ['55%', '80%'],
+ type: 'pie',
+ }],
+ tooltip: {
+ // Donut lives in a narrow container; append to body and don't confine so
+ // tooltips for left-side slices aren't clipped by the chart bounds.
+ appendToBody: true,
+ backgroundColor: 'transparent',
+ borderWidth: 0,
+ confine: false,
+ padding: 0,
+ trigger: 'item',
+ },
+}))
+
+function percentOf(value: number) {
+ return total.value > 0 ? Math.round((value / total.value) * 100) : 0
+}
+
+function onClickSlice(params: ECElementEvent) {
+ const data = params?.data as PieClickParam['data']
+ if (!data?.id || data.isOther || data.id === OTHER_SLICE_ID)
+ return
+ emit('clickCategory', data.id)
+}
+</script>
+
+<template>
+ <div
+ v-if="hasData"
+ class="flex items-center justify-center gap-4"
+ :class="{ '@3xl/stat:justify-start': showLegend }"
+ >
+ <div class="relative h-40 w-40 shrink-0 @3xl/stat:h-52 @3xl/stat:w-52">
+ <VChart
+ :option
+ :updateOptions="{ notMerge: true }"
+ autoresize
+ @click="onClickSlice"
+ >
+ <template #tooltip="params">
+ <div class="bg-elevated rounded-md px-3 py-2">
+ <div class="flex items-center gap-2">
+ <div class="size-2.5 rounded-full" :style="`background: ${(params as PieTooltipParam).color}`" />
+ <div class="text-muted text-sm">
+ {{ (params as PieTooltipParam).name }}
+ </div>
+ </div>
+
+ <div class="flex items-baseline justify-between gap-4 pt-1">
+ <div class="font-secondary text-highlighted text-lg">
+ {{ formatChartAmount((params as PieTooltipParam).value, locale) }}
+ </div>
+ <div class="text-muted text-xs">
+ {{ (params as PieTooltipParam).percent }}%
+ </div>
+ </div>
+ </div>
+ </template>
+ </VChart>
+
+ <div class="pointer-events-none absolute inset-0 flex flex-col items-center justify-center">
+ <div class="font-secondary text-highlighted text-lg leading-tight">
+ {{ formatChartAmount(total, locale) }}
+ </div>
+ <div class="text-muted text-2xs">
+ {{ typeLabel }}
+ </div>
+ </div>
+ </div>
+
+ <div
+ v-if="showLegend"
+ class="hidden min-w-0 grow content-start gap-0.5 @3xl/stat:grid"
+ >
+ <button
+ v-for="item in pieData"
+ :key="item.id"
+ type="button"
+ class="flex items-center justify-between gap-3 rounded-md px-2 py-1 text-left"
+ :class="item.isOther ? 'cursor-default' : 'hover:bg-elevated'"
+ @click="item.isOther ? undefined : emit('clickCategory', item.id)"
+ >
+ <div class="flex min-w-0 items-center gap-2">
+ <div class="size-2.5 shrink-0 rounded-full" :style="`background: ${item.color}`" />
+ <div class="text-muted truncate text-sm">
+ {{ item.name }}
+ </div>
+ </div>
+
+ <div class="flex shrink-0 items-baseline gap-2">
+ <div class="font-secondary text-highlighted text-sm">
+ {{ formatChartAmount(item.value, locale) }}
+ </div>
+ <div class="text-dimmed text-2xs w-8 text-right">
+ {{ percentOf(item.value) }}%
+ </div>
+ </div>
+ </button>
+ </div>
+ </div>
+
+ <div
+ v-else
+ class="text-muted flex h-40 items-center justify-center text-sm @3xl/stat:h-52"
+ >
+ {{ t('chart.empty') }}
+ </div>
+</template>
diff --git a/app/app/components/stat/chart/Wrap.vue b/app/app/components/stat/chart/Wrap.vue
index f4fde385..be152935 100644
--- a/app/app/components/stat/chart/Wrap.vue
+++ b/app/app/components/stat/chart/Wrap.vue
@@ -1,22 +1,32 @@
<script setup lang="ts">
+import type { CategoryId } from '~/components/categories/types'
import type { Period } from '~/components/date/types'
import type { ChartSeries } from '~/components/stat/types'
+import type { ChartPieGroup } from '~/components/stat/useStatItem'
import { statConfigKey, statDateKey } from '~/components/stat/injectionKeys'
+import { resolveChartType } from '~/components/stat/useStatConfig'
import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
-defineProps<{
+const props = defineProps<{
+ pieGroups: ChartPieGroup[]
series: ChartSeries[]
xAxisLabels: number[]
}>()
+const emit = defineEmits<{
+ clickCategory: [id: CategoryId]
+}>()
+
+const { t } = useI18n()
const statDate = inject(statDateKey)!
const statConfig = inject(statConfigKey)!
const trnsFormStore = useTrnsFormStore()
const isChartShow = computed(() => statConfig.config.value.isChartShow)
const chartView = computed(() => statConfig.config.value.chartView)
-const chartType = computed(() => statConfig.config.value.chartType)
+const chartType = computed(() => resolveChartType(statConfig.config.value.chartType, statConfig.config.value.chart.mode))
+const isPie = computed(() => chartType.value === 'pie')
const isShowQuick = computed(() => statConfig.config.value.date.isShowQuick)
function onClickChart(idx: number) {
@@ -34,10 +44,13 @@ function onChangePeriod(period: Period) {
<div
v-if="isChartShow"
:class="{
- '@3xl/main:max-w-xl': chartView === 'half',
+ '@3xl/main:max-w-xl': chartView === 'half' && !isPie,
}"
>
- <div class="-mb-1 flex justify-end">
+ <div
+ v-if="!isPie"
+ class="-mb-1 flex justify-end"
+ >
<StatDateQuick v-if="isShowQuick" />
<div class="h-7">
@@ -50,11 +63,27 @@ function onChangePeriod(period: Period) {
</div>
</div>
+ <div
+ v-if="isPie"
+ class="grid gap-4"
+ :class="{ '@sm/stat:grid-cols-2': props.pieGroups.length > 1 }"
+ >
+ <LazyStatChartPieView
+ v-for="group in props.pieGroups"
+ :key="group.type"
+ :pieData="group.pieData"
+ :showLegend="props.pieGroups.length === 1"
+ :typeLabel="t(`money.${group.type}`)"
+ @clickCategory="emit('clickCategory', $event)"
+ />
+ </div>
+
<LazyStatChartView
+ v-else
:chartType
:period="statDate.params.value.intervalsBy"
- :series
- :xAxisLabels
+ :series="props.series"
+ :xAxisLabels="props.xAxisLabels"
@click="onClickChart"
/>
</div>
diff --git a/app/app/components/stat/chart/types.ts b/app/app/components/stat/chart/types.ts
index c95e7330..7bc5fda3 100644
--- a/app/app/components/stat/chart/types.ts
+++ b/app/app/components/stat/chart/types.ts
@@ -1,3 +1,3 @@
-export const chartTypes = ['bar', 'line'] as const
+export const chartTypes = ['bar', 'line', 'pie'] as const
export type ChartType = (typeof chartTypes)[number]
diff --git a/app/app/components/stat/chart/useCategorySeriesBuilder.ts b/app/app/components/stat/chart/useCategorySeriesBuilder.ts
index 19b3fb9c..77af25c4 100644
--- a/app/app/components/stat/chart/useCategorySeriesBuilder.ts
+++ b/app/app/components/stat/chart/useCategorySeriesBuilder.ts
@@ -5,9 +5,15 @@ import type { TrnId, TrnItem } from '~/components/trns/types'
import { getParentCategoryIdOrUndefined } from '~/components/categories/utils'
-type BuildParams = {
+/** Neutral grey used for the aggregated "Other" slice/series. */
+export const OTHER_SLICE_COLOR = 'var(--ui-text-dimmed)'
+/** Synthetic id for the aggregated "Other" pie slice. Not a real category. */
+export const OTHER_SLICE_ID = '__other__'
+/** Default number of top categories shown before the rest roll up into "Other". */
+export const DEFAULT_PIE_TOP_N = 8
+
+type AggregateParams = {
categoriesItems: Categories
- chartType: ChartType | undefined
computeTotalForTrnsIds: (ids: TrnId[]) => { expense: number, income: number, sum: number }
filterCategoriesIds?: CategoryId[]
intervals: IntervalData[]
@@ -16,6 +22,31 @@ type BuildParams = {
type: SeriesSlug
}
+type BuildSeriesParams = AggregateParams & {
+ chartType: ChartType | undefined
+}
+
+type BuildPieParams = AggregateParams & {
+ topN?: number
+}
+
+export type CategoryPieDatum = {
+ color: string
+ id: CategoryId
+ isOther?: boolean
+ name: string
+ value: number
+}
+
+type AggregatedTotals = {
+ /** displayCategoryId -> total over the full range (positive amounts only kept in `orderedCategoryIds`). */
+ categoryTotals: Record<CategoryId, number>
+ /** Category ids with a positive total, sorted by total descending. */
+ orderedCategoryIds: CategoryId[]
+ /** For each interval: displayCategoryId -> trnIds that fell into it. */
+ perIntervalByCategory: Record<CategoryId, TrnId[]>[]
+}
+
function resolveCategoryId(
trnCategoryId: CategoryId | undefined,
categoriesItems: Categories,
@@ -28,16 +59,22 @@ function resolveCategoryId(
return getParentCategoryIdOrUndefined(categoriesItems, trnCategoryId) ?? trnCategoryId
}
-export function buildCategoriesSeries({
+/**
+ * Single source of truth for category breakdown numbers.
+ * Buckets transactions per interval by display category, then sums each
+ * category's total across the whole range. Transfers and out-of-filter
+ * categories are excluded. Both the bar/line series and the pie slices are
+ * derived from this so their numbers always agree.
+ */
+export function aggregateCategoryTotals({
categoriesItems,
- chartType,
computeTotalForTrnsIds,
filterCategoriesIds,
intervals,
isGrouped,
trnsItems,
type,
-}: BuildParams): ChartSeries[] {
+}: AggregateParams): AggregatedTotals {
const filterSet = filterCategoriesIds?.length ? new Set(filterCategoriesIds) : undefined
// For each interval, build a map of displayCategoryId -> trnIds[]
@@ -74,6 +111,29 @@ export function buildCategoriesSeries({
.filter(id => categoryTotals[id]! > 0)
.sort((a, b) => categoryTotals[b]! - categoryTotals[a]!)
+ return { categoryTotals, orderedCategoryIds, perIntervalByCategory }
+}
+
+export function buildCategoriesSeries({
+ categoriesItems,
+ chartType,
+ computeTotalForTrnsIds,
+ filterCategoriesIds,
+ intervals,
+ isGrouped,
+ trnsItems,
+ type,
+}: BuildSeriesParams): ChartSeries[] {
+ const { orderedCategoryIds, perIntervalByCategory } = aggregateCategoryTotals({
+ categoriesItems,
+ computeTotalForTrnsIds,
+ filterCategoriesIds,
+ intervals,
+ isGrouped,
+ trnsItems,
+ type,
+ })
+
return orderedCategoryIds.map((catId): ChartSeries => {
const category = categoriesItems[catId]
const data = perIntervalByCategory.map(bucket =>
@@ -87,3 +147,50 @@ export function buildCategoriesSeries({
}
})
}
+
+/**
+ * Pie slices for the "by categories" donut: top-N categories by total,
+ * descending, with the remainder rolled up into a single neutral "Other"
+ * slice (labeled with the `otherLabel` argument). Numbers come from the same
+ * aggregation as the bar series.
+ */
+export function buildCategoriesPieData(
+ { categoriesItems, computeTotalForTrnsIds, filterCategoriesIds, intervals, isGrouped, topN = DEFAULT_PIE_TOP_N, trnsItems, type }: BuildPieParams,
+ otherLabel: string,
+): CategoryPieDatum[] {
+ const { categoryTotals, orderedCategoryIds } = aggregateCategoryTotals({
+ categoriesItems,
+ computeTotalForTrnsIds,
+ filterCategoriesIds,
+ intervals,
+ isGrouped,
+ trnsItems,
+ type,
+ })
+
+ const top = orderedCategoryIds.slice(0, topN).map((catId): CategoryPieDatum => {
+ const category = categoriesItems[catId]
+ return {
+ color: category?.color ?? OTHER_SLICE_COLOR,
+ id: catId,
+ name: category?.name ?? catId,
+ value: categoryTotals[catId]!,
+ }
+ })
+
+ const restIds = orderedCategoryIds.slice(topN)
+ if (restIds.length === 0)
+ return top
+
+ const otherValue = restIds.reduce((acc, id) => acc + categoryTotals[id]!, 0)
+ return [
+ ...top,
+ {
+ color: OTHER_SLICE_COLOR,
+ id: OTHER_SLICE_ID,
+ isOther: true,
+ name: otherLabel,
+ value: otherValue,
+ },
+ ]
+}
diff --git a/app/app/components/stat/chart/useStatChart.ts b/app/app/components/stat/chart/useStatChart.ts
index b3c00f44..32407607 100644
--- a/app/app/components/stat/chart/useStatChart.ts
+++ b/app/app/components/stat/chart/useStatChart.ts
@@ -9,7 +9,7 @@ import { createMarkAreaData } from '~/components/stat/chart/utils'
export function useStatChart() {
const { t } = useI18n()
- const chartTypeOptions = computed<{ icon: string, label: string, value: ChartType }[]>(() => [{
+ const chartTypeOptions = computed<{ categoriesOnly?: boolean, icon: string, label: string, value: ChartType }[]>(() => [{
icon: 'lucide:chart-column',
label: t('chart.types.bar'),
value: 'bar',
@@ -17,6 +17,11 @@ export function useStatChart() {
icon: 'lucide:chart-line',
label: t('chart.types.line'),
value: 'line',
+ }, {
+ categoriesOnly: true,
+ icon: 'lucide:chart-pie',
+ label: t('chart.types.pie'),
+ value: 'pie',
}])
function createSeriesItem(typeItem: SeriesSlug, data: TotalReturns[], average?: number | false): ChartSeries {
diff --git a/app/app/components/stat/configPanels/Chart.vue b/app/app/components/stat/configPanels/Chart.vue
index d58a6bb2..4a407d93 100644
--- a/app/app/components/stat/configPanels/Chart.vue
+++ b/app/app/components/stat/configPanels/Chart.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useStatChart } from '~/components/stat/chart/useStatChart'
import { statConfigKey } from '~/components/stat/injectionKeys'
-import { chartViewOptions, resolveGrouped } from '~/components/stat/useStatConfig'
+import { chartViewOptions, resolveChartType, resolveGrouped } from '~/components/stat/useStatConfig'
const { t } = useI18n()
const statConfig = inject(statConfigKey)!
@@ -11,6 +11,14 @@ const isChartShow = computed(() => statConfig.config.value.isChartShow)
const grouping = computed(() => statConfig.config.value.grouping)
const isAutoGrouping = computed(() => grouping.value === 'auto')
const isChartGrouped = computed(() => resolveGrouped(statConfig.config.value.chart.isGrouped, grouping.value))
+const isCategoriesMode = computed(() => statConfig.config.value.chart.mode === 'categories')
+const activeChartType = computed(() => resolveChartType(statConfig.config.value.chartType, statConfig.config.value.chart.mode))
+const isPie = computed(() => activeChartType.value === 'pie')
+
+// Pie is only meaningful for the per-category breakdown.
+const visibleChartTypeOptions = computed(() =>
+ chartTypeOptions.value.filter(option => !option.categoriesOnly || isCategoriesMode.value),
+)
function toggleGroupedWithReset(currentEffective: boolean) {
if (!isAutoGrouping.value) {
@@ -32,6 +40,7 @@ function toggleGroupedWithReset(currentEffective: boolean) {
:title="t('stat.config.date.quick.label')"
/>
<StatConfigSwitch
+ v-if="!isPie"
configKey="chart"
field="isShowAverage"
:title="t('stat.config.chart.average.label')"
@@ -49,7 +58,10 @@ function toggleGroupedWithReset(currentEffective: boolean) {
/>
<div class="grid gap-4 pt-4">
- <div class="hidden gap-2 md:grid">
+ <div
+ v-if="!isPie"
+ class="hidden gap-2 md:grid"
+ >
<UiTitleSection size="sm" class="px-1">
{{ t('stat.config.chartView.label') }}
</UiTitleSection>
@@ -73,10 +85,10 @@ function toggleGroupedWithReset(currentEffective: boolean) {
</UiTitleSection>
<UiTabsBar>
<UiTabsItemPill
- v-for="item in chartTypeOptions"
+ v-for="item in visibleChartTypeOptions"
:key="item.value"
variant="outline"
- :isActive="statConfig.config.value.chartType === item.value"
+ :isActive="activeChartType === item.value"
class="flex grow gap-1"
@click="statConfig.updateConfig('chartType', item.value)"
>
diff --git a/app/app/components/stat/useStatConfig.ts b/app/app/components/stat/useStatConfig.ts
index b8c7b8c6..e104a46b 100644
--- a/app/app/components/stat/useStatConfig.ts
+++ b/app/app/components/stat/useStatConfig.ts
@@ -4,6 +4,8 @@ import { useStorage } from '@vueuse/core'
import defu from 'defu'
import { z } from 'zod/v4'
+import type { ChartType } from '~/components/stat/chart/types'
+
import { chartTypes } from '~/components/stat/chart/types'
import { applyConfigUpdate } from '~/components/stat/statConfig'
@@ -20,6 +22,17 @@ export function resolveGrouped(raw: boolean, grouping: GroupingMode): boolean {
return raw
}
+export type ChartMode = 'aggregated' | 'categories'
+
+/**
+ * Pie is only meaningful for the per-category breakdown. When the chart is in
+ * aggregated mode a stored `pie` selection falls back to `bar`, so switching
+ * modes never leaves the chart in an unrenderable state.
+ */
+export function resolveChartType(raw: ChartType, mode: ChartMode): ChartType {
+ return mode !== 'categories' && raw === 'pie' ? 'bar' : raw
+}
+
export const ConfigSchema = z.object({
catsList: z.object({
isGrouped: z.boolean(),
diff --git a/app/app/components/stat/useStatItem.ts b/app/app/components/stat/useStatItem.ts
index fb67a6f8..ec77e638 100644
--- a/app/app/components/stat/useStatItem.ts
+++ b/app/app/components/stat/useStatItem.ts
@@ -6,6 +6,7 @@ import { differenceInDays } from 'date-fns'
import type { TotalReturns } from '~/components/amount/getTotal'
import type { CategoryId } from '~/components/categories/types'
import type { StatDateProvider } from '~/components/date/types'
+import type { CategoryPieDatum } from '~/components/stat/chart/useCategorySeriesBuilder'
import type { FilterProvider } from '~/components/stat/filter/types'
import type { ChartSeries, IntervalData, SeriesSlug, SeriesSlugSelected, StatTabSlug } from '~/components/stat/types'
import type { StatConfigProvider } from '~/components/stat/useStatConfig'
@@ -13,13 +14,18 @@ import type { TrnId } from '~/components/trns/types'
import { useAmount } from '~/components/amount/useAmount'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
-import { buildCategoriesSeries } from '~/components/stat/chart/useCategorySeriesBuilder'
+import { buildCategoriesPieData, buildCategoriesSeries } from '~/components/stat/chart/useCategorySeriesBuilder'
import { useStatChart } from '~/components/stat/chart/useStatChart'
import { bucketTrnsByIntervals, computeAverageTotal, isPeriodOneDay as isPeriodOneDayFn } from '~/components/stat/intervals'
-import { resolveGrouped } from '~/components/stat/useStatConfig'
+import { resolveChartType, resolveGrouped } from '~/components/stat/useStatConfig'
import { getSelectedType, getSelectedTypeForSum, getTypesMapping, getTypesToShow } from '~/components/stat/utils'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
+export type ChartPieGroup = {
+ pieData: CategoryPieDatum[]
+ type: SeriesSlug
+}
+
type UseStatItemParams = {
filter: FilterProvider
statConfig: StatConfigProvider
@@ -39,6 +45,7 @@ export function useStatItem({
trnsIds,
type,
}: UseStatItemParams) {
+ const { t } = useI18n()
const trnsStore = useTrnsStore()
const categoriesStore = useCategoriesStore()
const { computeTotalForTrnsIds } = useAmount()
@@ -140,15 +147,39 @@ export function useStatItem({
return 'expense'
if (statTab.value === 'income')
return 'income'
+ // Split renders one item per type ('expense' / 'income'); follow that prop
+ // so each column's donut/bars show its own side.
+ if (statTab.value === 'split' && (type.value === 'expense' || type.value === 'income'))
+ return type.value
if (filteredType.value === 'expense' || filteredType.value === 'income')
return filteredType.value
return 'expense'
})
+ // Effective filter + grouping for the category breakdown (donut and bars).
+ // Selecting a category drills into it: match its leaf (transactible) ids and
+ // stop grouping, so a selected parent breaks down into its child slices.
+ const categoryBreakdownFilter = computed(() => {
+ const ids = filteredCategoriesIds.value
+ if (ids.length === 0) {
+ return {
+ filterCategoriesIds: undefined as CategoryId[] | undefined,
+ isGrouped: resolveGrouped(statConfig.config.value.chart.isGrouped, statConfig.config.value.grouping),
+ }
+ }
+ return {
+ filterCategoriesIds: categoriesStore.getTransactibleIds(ids),
+ isGrouped: false,
+ }
+ })
+
const chartSeries = computed<ChartSeries[]>(() => {
const intervals = intervalsDataWithFilteredCategories.value
const selectedInterval = intervals[statDate.params.value.intervalSelected]
- const chartType = statConfig.config.value?.chartType
+ // Bar/line series never use the `pie` type; the donut renders from
+ // `chartPieGroups`, so collapse pie -> bar for the axis-based series here.
+ const rawChartType = resolveChartType(statConfig.config.value.chartType, statConfig.config.value.chart.mode)
+ const chartType = rawChartType === 'pie' ? 'bar' : rawChartType
let baseSeries: ChartSeries[]
@@ -157,9 +188,9 @@ export function useStatItem({
categoriesItems: categoriesStore.items ?? {},
chartType,
computeTotalForTrnsIds,
- filterCategoriesIds: filteredCategoriesIds.value,
+ filterCategoriesIds: categoryBreakdownFilter.value.filterCategoriesIds,
intervals,
- isGrouped: resolveGrouped(statConfig.config.value.chart.isGrouped, statConfig.config.value.grouping),
+ isGrouped: categoryBreakdownFilter.value.isGrouped,
trnsItems: trnsStore.items ?? {},
type: categoriesBreakdownType.value,
})
@@ -181,6 +212,34 @@ export function useStatItem({
intervalsDataWithFilteredCategories.value.map(i => i.range.start),
)
+ function buildPieGroup(type: SeriesSlug): CategoryPieDatum[] {
+ return buildCategoriesPieData({
+ categoriesItems: categoriesStore.items ?? {},
+ computeTotalForTrnsIds,
+ filterCategoriesIds: categoryBreakdownFilter.value.filterCategoriesIds,
+ intervals: intervalsDataWithFilteredCategories.value,
+ isGrouped: categoryBreakdownFilter.value.isGrouped,
+ trnsItems: trnsStore.items ?? {},
+ type,
+ }, t('stat.config.chart.other'))
+ }
+
+ // Summary tab shows expense + income donuts side by side; every other tab
+ // shows a single donut for the active breakdown type.
+ const chartPieGroups = computed<ChartPieGroup[]>(() => {
+ if (statConfig.config.value.chart.mode !== 'categories')
+ return []
+
+ if (statTab.value === 'summary') {
+ return [
+ { pieData: buildPieGroup('expense'), type: 'expense' },
+ { pieData: buildPieGroup('income'), type: 'income' },
+ ]
+ }
+
+ return [{ pieData: buildPieGroup(categoriesBreakdownType.value), type: categoriesBreakdownType.value }]
+ })
+
function onSetCategoryFilter(categoryId: CategoryId) {
if (filteredCategoriesIds.value.includes(categoryId)) {
filteredCategoriesIds.value = []
@@ -195,6 +254,8 @@ export function useStatItem({
return {
averageTotal,
+ categoriesBreakdownType,
+ chartPieGroups,
chartSeries,
chartXAxisLabels,
filteredCategoriesIds,