Skip to content

Open source personal finance application

feat(stat): add grouping mode, chart-by-categories and improve line UX

Ilya Komichev4ee9a4d80157
diff --git a/app/components/stat/Menu.vue b/app/components/stat/Menu.vue
index f6533bc1..5a9d9279 100644
--- a/app/components/stat/Menu.vue
+++ b/app/components/stat/Menu.vue
@@ -55,7 +55,7 @@ watch(() => props.active, () => {
:key="item.id"
:class="[{
'!border-(--ui-primary) pb-2.5': item.id === props.active,
- 'rounded-sm pb-1.5 text-muted hover:bg-(--item-5)': item.id !== props.active,
+ 'text-muted rounded-sm pb-1.5 hover:bg-(--item-5)': item.id !== props.active,
}]"
class="border-b border-transparent px-3 pt-1.5 text-xs tracking-wide text-nowrap lg:text-sm"
@click="onClickStatMenu(item.id)"
diff --git a/app/components/stat/categories/DetailedSection.vue b/app/components/stat/categories/DetailedSection.vue
index c8388407..6f838047 100644
--- a/app/components/stat/categories/DetailedSection.vue
+++ b/app/components/stat/categories/DetailedSection.vue
@@ -7,6 +7,7 @@ import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { useCategoriesExpanded } from '~/components/stat/categories/useCategoriesExpanded'
import { useStatCategories } from '~/components/stat/categories/useStatCategories'
import { statConfigKey } from '~/components/stat/injectionKeys'
+import { resolveGrouped } from '~/components/stat/useStatConfig'
const props = defineProps<{
isOneCategory?: boolean
@@ -31,7 +32,7 @@ const catsList = computed(() => statConfig.config.value.catsList)
const isVerticalShow = computed(() => statConfig.config.value.vertical.isShow)
const isVerticalGrouped = computed(() => statConfig.config.value.vertical.isGrouped)
const isListShow = computed(() => catsList.value.isShow)
-const isListGrouped = computed(() => catsList.value.isGrouped)
+const isListGrouped = computed(() => resolveGrouped(catsList.value.isGrouped, statConfig.config.value.grouping))
const isLines = computed(() => catsList.value.isLines)
// Categories data (lazy — each variant computed only when accessed)
@@ -39,7 +40,8 @@ const groupedCategories = computed(() => computeCategoriesWithData(props.selecte
const ungroupedCategories = computed(() => computeCategoriesWithData(props.selectedTrnsIds ?? [], false))
const categoriesWithData = computed<CategoryWithData[]>(() => {
- const isGrouped = statConfig.config.value[statConfig.config.value.catsView === 'list' ? 'catsList' : 'catsRound'].isGrouped
+ const rawGrouped = statConfig.config.value[statConfig.config.value.catsView === 'list' ? 'catsList' : 'catsRound'].isGrouped
+ const isGrouped = resolveGrouped(rawGrouped, statConfig.config.value.grouping)
if (statConfig.config.value.isShowEmptyCategories && props.preCategoriesIds?.length)
return computeCategoriesWithData(props.selectedTrnsIds ?? [], isGrouped, props.preCategoriesIds)
@@ -74,6 +76,21 @@ const {
categoriesStore.items ?? {},
categoriesWithData,
)
+
+function onParentClick(item: CategoryWithData) {
+ if (item.categories?.length)
+ toggleCategory(item.id)
+ else
+ emit('clickCategory', item.id)
+}
+
+function onToggleListGrouping() {
+ if (statConfig.config.value.grouping !== 'auto') {
+ statConfig.updateConfig('grouping', 'auto')
+ return
+ }
+ statConfig.updateConfig('catsList', { isGrouped: !isListGrouped.value })
+}
</script>
<template>
@@ -152,7 +169,7 @@ const {
<StatCategoriesGroupingToggle
v-if="!props.isOneCategory"
:isGrouped="isListGrouped"
- @toggle="statConfig.config.value.catsList.isGrouped = !isListGrouped"
+ @toggle="onToggleListGrouping"
/>
</div>
</div>
@@ -174,11 +191,15 @@ const {
<div class="-mt-px flex items-stretch justify-between">
<StatCategoriesLine
:isShowParent="props.isOneCategory ? false : !isListGrouped"
+ :stacked="!props.isOneCategory && !isListGrouped"
:item="item"
+ :isExpanded="isExpanded(item.id)"
+ isShowChevron
:maxCategoryValues="linesMaxValues"
:lineWidth="isLines ? 0 : 1"
class="grow"
- @click="item.categories?.length ? toggleCategory(item.id) : emit('clickCategory', item.id)"
+ @click="onParentClick(item)"
+ @amountClick="emit('clickCategory', item.id)"
/>
</div>
</template>
@@ -198,6 +219,7 @@ const {
:lineWidth="isLines ? 0 : 1"
class="grow"
@click="emit('clickCategory', itemInside.id)"
+ @amountClick="emit('clickCategory', itemInside.id)"
/>
</div>
</div>
diff --git a/app/components/stat/categories/Line.vue b/app/components/stat/categories/Line.vue
index b00a0f6b..a2c85206 100644
--- a/app/components/stat/categories/Line.vue
+++ b/app/components/stat/categories/Line.vue
@@ -13,6 +13,8 @@ const props = defineProps<{
insideClass?: string
insideStyle?: string
isActive?: boolean
+ isExpanded?: boolean
+ isShowChevron?: boolean
isShowParent?: boolean
item: CategoryWithData
lineWidth?: number
@@ -20,12 +22,16 @@ const props = defineProps<{
expense: number
income: number
}
+ stacked?: boolean
}>()
const emit = defineEmits<{
+ amountClick: [categoryId: CategoryId]
click: [categoryId: CategoryId]
}>()
+const hasChildren = computed(() => !!props.item.categories?.length)
+
const statConfig = inject(statConfigKey)!
const categoriesStore = useCategoriesStore()
const currenciesStore = useCurrenciesStore()
@@ -47,6 +53,11 @@ const { longPressRef } = useCategoryLongPress(
() => props.item.id,
() => emit('click', props.item.id),
)
+
+function onAmountClick(e: MouseEvent) {
+ e.stopPropagation()
+ emit('amountClick', props.item.id)
+}
</script>
<template>
@@ -95,20 +106,29 @@ const { longPressRef } = useCategoryLongPress(
/>
</template>
- <CategoriesName
- :category
- :childrenCount="props.item.categories?.length"
- :class="{ '!pb-2': isLines }"
- :isShowParent="props.isShowParent"
- :parentCategory
- />
+ <div class="flex grow items-center gap-1">
+ <CategoriesName
+ :category
+ :childrenCount="isShowChevron ? undefined : props.item.categories?.length"
+ :class="{ '!pb-2': isLines }"
+ :isShowParent="props.isShowParent"
+ :parentCategory
+ :stacked="props.stacked"
+ />
+
+ <Icon
+ v-if="isShowChevron && hasChildren"
+ :name="props.isExpanded ? 'lucide:chevron-down' : 'lucide:chevron-right'"
+ size="18"
+ class="text-muted self-center"
+ />
+ </div>
<div
v-if="props.item.value !== 0"
- :class="{
- '!pb-2': isLines,
- }"
- class="grow pr-1"
+ :class="{ '!pb-2': isLines }"
+ class="-my-1.5 flex min-w-24 shrink-0 cursor-pointer items-center justify-end self-stretch rounded-sm px-2"
+ @click="onAmountClick"
>
<Amount
:amount="props.item.value"
diff --git a/app/components/stat/categories/RoundSection.vue b/app/components/stat/categories/RoundSection.vue
index 8968209b..d809c8b0 100644
--- a/app/components/stat/categories/RoundSection.vue
+++ b/app/components/stat/categories/RoundSection.vue
@@ -5,6 +5,7 @@ import type { TrnId } from '~/components/trns/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { useStatCategories } from '~/components/stat/categories/useStatCategories'
import { statConfigKey } from '~/components/stat/injectionKeys'
+import { resolveGrouped } from '~/components/stat/useStatConfig'
const props = defineProps<{
filteredCategoriesIds: CategoryId[]
@@ -25,7 +26,7 @@ const categoriesStore = useCategoriesStore()
const statConfig = inject(statConfigKey)!
const isExpanded = computed(() => statConfig.config.value.catsRound.isExpanded)
-const isGrouped = computed(() => statConfig.config.value.catsRound.isGrouped)
+const isGrouped = computed(() => resolveGrouped(statConfig.config.value.catsRound.isGrouped, statConfig.config.value.grouping))
const isShowFavorites = computed(() => statConfig.config.value.catsRound.isShowFavorites)
const isShowRecent = computed(() => statConfig.config.value.catsRound.isShowRecent)
diff --git a/app/components/stat/categories/RoundSectionMenu.vue b/app/components/stat/categories/RoundSectionMenu.vue
index 54fb2670..fd646988 100644
--- a/app/components/stat/categories/RoundSectionMenu.vue
+++ b/app/components/stat/categories/RoundSectionMenu.vue
@@ -1,12 +1,13 @@
<script setup lang="ts">
import type { CategoryId } from '~/components/categories/types'
+import type { GroupingMode } from '~/components/stat/useStatConfig'
import type { TrnId } from '~/components/trns/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { useStatCategories } from '~/components/stat/categories/useStatCategories'
import { useStatChart } from '~/components/stat/chart/useStatChart'
import { statConfigKey } from '~/components/stat/injectionKeys'
-import { chartViewOptions } from '~/components/stat/useStatConfig'
+import { chartViewOptions, groupingModes, resolveGrouped } from '~/components/stat/useStatConfig'
const props = defineProps<{
filteredCategoriesIds: CategoryId[]
@@ -28,15 +29,17 @@ const isExpandedListCard = ref(true)
const { chartTypeOptions } = useStatChart()
const isChartShow = computed(() => statConfig.config.value.isChartShow)
+const grouping = computed(() => statConfig.config.value.grouping)
+const isAutoGrouping = computed(() => grouping.value === 'auto')
+
const isExpanded = computed(() => statConfig.config.value.catsRound.isExpanded)
-const isGrouped = computed(() => statConfig.config.value.catsRound.isGrouped)
+const isChartGrouped = computed(() => resolveGrouped(statConfig.config.value.chart.isGrouped, grouping.value))
+const isCatsRoundGrouped = computed(() => resolveGrouped(statConfig.config.value.catsRound.isGrouped, grouping.value))
const isShowFavorites = computed(() => statConfig.config.value.catsRound.isShowFavorites)
const isShowRecent = computed(() => statConfig.config.value.catsRound.isShowRecent)
-const isVerticalShow = computed(() => statConfig.config.value.vertical.isShow)
const isCatsRoundShow = computed(() => statConfig.config.value.catsRound.isShow)
const isCatsListShow = computed(() => statConfig.config.value.catsList.isShow)
-const isTrnsShow = computed(() => statConfig.config.value.trns.isShow)
const mergedPreCategoriesIds = computed(() => {
const ids: CategoryId[] = []
@@ -70,7 +73,7 @@ const mergedPreCategoriesIds = computed(() => {
return ids
})
-const roundCategories = computed(() => computeCategoriesWithData(props.selectedTrnsIds ?? [], isGrouped.value, mergedPreCategoriesIds.value))
+const roundCategories = computed(() => computeCategoriesWithData(props.selectedTrnsIds ?? [], isCatsRoundGrouped.value, mergedPreCategoriesIds.value))
function toggleMaster(key: 'catsList' | 'catsRound' | 'trns' | 'vertical', current: boolean) {
statConfig.updateConfig(key, { isShow: !current } as never)
@@ -79,6 +82,18 @@ function toggleMaster(key: 'catsList' | 'catsRound' | 'trns' | 'vertical', curre
function toggleChartShow() {
statConfig.updateConfig('isChartShow', !isChartShow.value)
}
+
+function toggleGroupedWithReset(key: 'catsList' | 'catsRound' | 'chart', currentEffective: boolean) {
+ if (!isAutoGrouping.value) {
+ statConfig.updateConfig('grouping', 'auto')
+ return
+ }
+ statConfig.updateConfig(key, { isGrouped: !currentEffective } as never)
+}
+
+function setGrouping(mode: GroupingMode) {
+ statConfig.updateConfig('grouping', mode)
+}
</script>
<template>
@@ -99,177 +114,206 @@ function toggleChartShow() {
<template #content>
<div class="grid w-80 gap-2 overflow-y-auto p-2" style="max-height: var(--reka-popper-available-height, 80dvh)">
- <div class="grid gap-3">
- <!-- Chart -->
- <div class="overflow-hidden rounded-sm border border-(--ui-border)">
- <div class="flex items-stretch bg-(--item-4)" :class="{ 'pb-px': isExpandedChartCard }">
- <div
- class="flex grow cursor-pointer items-center gap-2 rounded-tr-sm rounded-br-sm px-3 py-2 hover:bg-(--item-3)"
- @click="isExpandedChartCard = !isExpandedChartCard"
+ <!-- Global grouping -->
+ <div class="grid gap-1 px-1 pb-1">
+ <UiTitleSection size="sm">
+ {{ t('stat.config.grouping.label') }}
+ </UiTitleSection>
+ <UiTabsBar>
+ <UiTabsItemPill
+ v-for="mode in groupingModes"
+ :key="mode"
+ variant="outline"
+ :isActive="grouping === mode"
+ class="grow"
+ @click="setGrouping(mode)"
>
- <Icon
- :name="isExpandedChartCard ? 'lucide:chevron-down' : 'lucide:chevron-right'"
- size="18"
- />
- <div class="grow text-sm font-semibold">
- {{ t('stat.config.chartShow.title') }}
+ {{ t(`stat.config.grouping.${mode}`) }}
+ </UiTabsItemPill>
+ </UiTabsBar>
+ </div>
+
+ <div class="grid gap-3">
+ <!-- Chart -->
+ <div class="overflow-hidden rounded-sm border border-(--ui-border)">
+ <div class="flex items-stretch bg-(--item-4)" :class="{ 'pb-px': isExpandedChartCard }">
+ <div
+ class="flex grow cursor-pointer items-center gap-2 rounded-tr-sm rounded-br-sm px-3 py-2 hover:bg-(--item-3)"
+ @click="isExpandedChartCard = !isExpandedChartCard"
+ >
+ <Icon
+ :name="isExpandedChartCard ? 'lucide:chevron-down' : 'lucide:chevron-right'"
+ size="18"
+ />
+ <div class="grow text-sm font-semibold">
+ {{ t('stat.config.chartShow.title') }}
+ </div>
+ </div>
+ <div
+ class="flex shrink-0 cursor-pointer items-center rounded-tl-sm rounded-bl-sm px-3 hover:bg-(--item-3)"
+ @click.stop="toggleChartShow"
+ >
+ <FormSwitch :value="isChartShow" />
</div>
</div>
+
<div
- class="flex shrink-0 cursor-pointer items-center rounded-tl-sm rounded-bl-sm px-3 hover:bg-(--item-3)"
- @click.stop="toggleChartShow"
+ v-if="isExpandedChartCard"
+ class="grid gap-2 p-2 transition-opacity"
+ :class="{ 'pointer-events-none opacity-50': !isChartShow }"
>
- <FormSwitch :value="isChartShow" />
- </div>
- </div>
+ <StatConfigSwitch
+ configKey="date"
+ field="isShowQuick"
+ :title="t('stat.config.date.quick.label')"
+ />
+ <StatConfigSwitch
+ configKey="chart"
+ field="isShowAverage"
+ :title="t('stat.config.chart.average.label')"
+ />
+ <UiSwitchItem
+ :checkboxValue="statConfig.config.value.chart.mode === 'categories'"
+ :title="t('stat.config.chart.byCategories')"
+ @click="statConfig.updateConfig('chart', { mode: statConfig.config.value.chart.mode === 'categories' ? 'aggregated' : 'categories' })"
+ />
+ <UiSwitchItem
+ v-if="statConfig.config.value.chart.mode === 'categories'"
+ :checkboxValue="isChartGrouped"
+ :title="t('stat.config.chart.groupByParent')"
+ @click="toggleGroupedWithReset('chart', isChartGrouped)"
+ />
- <div
- v-if="isExpandedChartCard"
- class="grid gap-2 p-2 transition-opacity"
- :class="{ 'pointer-events-none opacity-50': !isChartShow }"
- >
- <StatConfigSwitch
- configKey="date"
- field="isShowQuick"
- :title="t('stat.config.date.quick.label')"
- />
- <StatConfigSwitch
- configKey="chart"
- field="isShowAverage"
- :title="t('stat.config.chart.average.label')"
- />
-
- <!-- Chart: view (md+ only) -->
- <div class="hidden gap-2 md:grid">
- <UiTitleSection size="sm" class="px-1">
- {{ t('stat.config.chartView.label') }}
- </UiTitleSection>
- <UiTabsBar>
- <UiTabsItemPill
- v-for="view in chartViewOptions"
- :key="view"
- variant="outline"
- :isActive="statConfig.config.value.chartView === view"
- class="grow"
- @click="statConfig.updateConfig('chartView', view)"
- >
- {{ t(`stat.config.chartView.${view}`) }}
- </UiTabsItemPill>
- </UiTabsBar>
- </div>
+ <!-- Chart: view (md+ only) -->
+ <div class="hidden gap-2 md:grid">
+ <UiTitleSection size="sm" class="px-1">
+ {{ t('stat.config.chartView.label') }}
+ </UiTitleSection>
+ <UiTabsBar>
+ <UiTabsItemPill
+ v-for="view in chartViewOptions"
+ :key="view"
+ variant="outline"
+ :isActive="statConfig.config.value.chartView === view"
+ class="grow"
+ @click="statConfig.updateConfig('chartView', view)"
+ >
+ {{ t(`stat.config.chartView.${view}`) }}
+ </UiTabsItemPill>
+ </UiTabsBar>
+ </div>
- <!-- Chart: type -->
- <div class="grid gap-2">
- <UiTitleSection size="sm" class="px-1">
- {{ t('stat.config.chart.type.label') }}
- </UiTitleSection>
- <UiTabsBar>
- <UiTabsItemPill
- v-for="item in chartTypeOptions"
- :key="item.value"
- variant="outline"
- :isActive="statConfig.config.value.chartType === item.value"
- class="flex grow gap-1"
- @click="statConfig.updateConfig('chartType', item.value)"
- >
- <Icon :name="item.icon" :size="16" />
- {{ item.label }}
- </UiTabsItemPill>
- </UiTabsBar>
+ <!-- Chart: type -->
+ <div class="grid gap-2">
+ <UiTitleSection size="sm" class="px-1">
+ {{ t('stat.config.chart.type.label') }}
+ </UiTitleSection>
+ <UiTabsBar>
+ <UiTabsItemPill
+ v-for="item in chartTypeOptions"
+ :key="item.value"
+ variant="outline"
+ :isActive="statConfig.config.value.chartType === item.value"
+ class="flex grow gap-1"
+ @click="statConfig.updateConfig('chartType', item.value)"
+ >
+ <Icon :name="item.icon" :size="16" />
+ {{ item.label }}
+ </UiTabsItemPill>
+ </UiTabsBar>
+ </div>
</div>
</div>
- </div>
- <!-- Round categories -->
- <div class="overflow-hidden rounded-sm border border-(--ui-border)">
- <div class="flex items-stretch bg-(--item-4)" :class="{ 'pb-px': isExpandedRoundCard }">
- <div
- class="flex grow cursor-pointer items-center gap-2 rounded-tr-sm rounded-br-sm px-3 py-2 hover:bg-(--item-3)"
- @click="isExpandedRoundCard = !isExpandedRoundCard"
- >
- <Icon
- :name="isExpandedRoundCard ? 'lucide:chevron-down' : 'lucide:chevron-right'"
- size="18"
- />
- <div class="grow text-sm font-semibold">
- {{ t('stat.config.categories.rounds.title') }}
+ <!-- Round categories -->
+ <div class="overflow-hidden rounded-sm border border-(--ui-border)">
+ <div class="flex items-stretch bg-(--item-4)" :class="{ 'pb-px': isExpandedRoundCard }">
+ <div
+ class="flex grow cursor-pointer items-center gap-2 rounded-tr-sm rounded-br-sm px-3 py-2 hover:bg-(--item-3)"
+ @click="isExpandedRoundCard = !isExpandedRoundCard"
+ >
+ <Icon
+ :name="isExpandedRoundCard ? 'lucide:chevron-down' : 'lucide:chevron-right'"
+ size="18"
+ />
+ <div class="grow text-sm font-semibold">
+ {{ t('stat.config.categories.rounds.title') }}
+ </div>
+ </div>
+ <div
+ class="flex shrink-0 cursor-pointer items-center rounded-tl-sm rounded-bl-sm px-3 hover:bg-(--item-3)"
+ @click.stop="toggleMaster('catsRound', isCatsRoundShow)"
+ >
+ <FormSwitch :value="isCatsRoundShow" />
</div>
</div>
+
<div
- class="flex shrink-0 cursor-pointer items-center rounded-tl-sm rounded-bl-sm px-3 hover:bg-(--item-3)"
- @click.stop="toggleMaster('catsRound', isCatsRoundShow)"
+ v-if="isExpandedRoundCard"
+ class="grid gap-0.5 p-1 transition-opacity"
+ :class="{ 'pointer-events-none opacity-50': !isCatsRoundShow }"
>
- <FormSwitch :value="isCatsRoundShow" />
+ <UiSwitchItem
+ :checkboxValue="isCatsRoundGrouped"
+ :title="t('stat.config.categories.rounds.groupByParent')"
+ @click="toggleGroupedWithReset('catsRound', isCatsRoundGrouped)"
+ />
+ <UiSwitchItem
+ :checkboxValue="isShowFavorites"
+ :title="t('stat.config.categories.rounds.showFavorites')"
+ @click="statConfig.updateConfig('catsRound', { isShowFavorites: !isShowFavorites })"
+ />
+ <UiSwitchItem
+ :checkboxValue="isShowRecent"
+ :title="t('stat.config.categories.rounds.showRecent')"
+ @click="statConfig.updateConfig('catsRound', { isShowRecent: !isShowRecent })"
+ />
+ <StatConfigSwitch
+ configKey="catsRound"
+ field="isIconBg"
+ :title="t('stat.catButtons.isRoundIcon')"
+ />
+ <UiSwitchItem
+ v-if="roundCategories.length > visibleCategoriesLimit"
+ :checkboxValue="isExpanded"
+ :title="t('base.toggleExpand')"
+ @click="statConfig.updateConfig('catsRound', { isExpanded: !isExpanded })"
+ />
</div>
</div>
- <div
- v-if="isExpandedRoundCard"
- class="grid gap-0.5 p-1 transition-opacity"
- :class="{ 'pointer-events-none opacity-50': !isCatsRoundShow }"
- >
- <UiSwitchItem
- :checkboxValue="isGrouped"
- :title="t('stat.config.categories.rounds.groupByParent')"
- @click="statConfig.updateConfig('catsRound', { isGrouped: !isGrouped })"
- />
- <UiSwitchItem
- :checkboxValue="isShowFavorites"
- :title="t('stat.config.categories.rounds.showFavorites')"
- @click="statConfig.updateConfig('catsRound', { isShowFavorites: !isShowFavorites })"
- />
- <UiSwitchItem
- :checkboxValue="isShowRecent"
- :title="t('stat.config.categories.rounds.showRecent')"
- @click="statConfig.updateConfig('catsRound', { isShowRecent: !isShowRecent })"
- />
- <StatConfigSwitch
- configKey="catsRound"
- field="isIconBg"
- :title="t('stat.catButtons.isRoundIcon')"
- />
- <UiSwitchItem
- v-if="roundCategories.length > visibleCategoriesLimit"
- :checkboxValue="isExpanded"
- :title="t('base.toggleExpand')"
- @click="statConfig.updateConfig('catsRound', { isExpanded: !isExpanded })"
- />
- </div>
- </div>
-
- <!-- List -->
- <div class="overflow-hidden rounded-sm border border-(--ui-border)">
- <div class="flex items-stretch bg-(--item-4)" :class="{ 'pb-px': isExpandedListCard }">
- <div
- class="flex grow cursor-pointer items-center gap-2 rounded-tr-sm rounded-br-sm px-3 py-2 hover:bg-(--item-3)"
- @click="isExpandedListCard = !isExpandedListCard"
- >
- <Icon
- :name="isExpandedListCard ? 'lucide:chevron-down' : 'lucide:chevron-right'"
- size="18"
- />
- <div class="grow text-sm font-semibold">
- {{ t('stat.config.categories.list.title') }}
+ <!-- List -->
+ <div class="overflow-hidden rounded-sm border border-(--ui-border)">
+ <div class="flex items-stretch bg-(--item-4)" :class="{ 'pb-px': isExpandedListCard }">
+ <div
+ class="flex grow cursor-pointer items-center gap-2 rounded-tr-sm rounded-br-sm px-3 py-2 hover:bg-(--item-3)"
+ @click="isExpandedListCard = !isExpandedListCard"
+ >
+ <Icon
+ :name="isExpandedListCard ? 'lucide:chevron-down' : 'lucide:chevron-right'"
+ size="18"
+ />
+ <div class="grow text-sm font-semibold">
+ {{ t('stat.config.categories.list.title') }}
+ </div>
+ </div>
+ <div
+ class="flex shrink-0 cursor-pointer items-center rounded-tl-sm rounded-bl-sm px-3 hover:bg-(--item-3)"
+ @click.stop="toggleMaster('catsList', isCatsListShow)"
+ >
+ <FormSwitch :value="isCatsListShow" />
</div>
</div>
+
<div
- class="flex shrink-0 cursor-pointer items-center rounded-tl-sm rounded-bl-sm px-3 hover:bg-(--item-3)"
- @click.stop="toggleMaster('catsList', isCatsListShow)"
+ v-if="isExpandedListCard"
+ class="grid gap-0.5 p-1 transition-opacity"
+ :class="{ 'pointer-events-none opacity-50': !isCatsListShow }"
>
- <FormSwitch :value="isCatsListShow" />
+ <StatConfigSwitch configKey="catsList" field="isLines" :title="t('stat.catButtons.isLines')" />
+ <StatConfigSwitch configKey="catsList" field="isRoundIcon" :title="t('stat.catButtons.isRoundIcon')" />
</div>
</div>
-
- <div
- v-if="isExpandedListCard"
- class="grid gap-0.5 p-1 transition-opacity"
- :class="{ 'pointer-events-none opacity-50': !isCatsListShow }"
- >
- <StatConfigSwitch configKey="catsList" field="isLines" :title="t('stat.catButtons.isLines')" />
- <StatConfigSwitch configKey="catsList" field="isRoundIcon" :title="t('stat.catButtons.isRoundIcon')" />
- </div>
- </div>
-
</div>
<div class="grid gap-0.5">
diff --git a/app/components/stat/chart/useCategorySeriesBuilder.ts b/app/components/stat/chart/useCategorySeriesBuilder.ts
new file mode 100644
index 00000000..19b3fb9c
--- /dev/null
+++ b/app/components/stat/chart/useCategorySeriesBuilder.ts
@@ -0,0 +1,89 @@
+import type { Categories, CategoryId } from '~/components/categories/types'
+import type { ChartType } from '~/components/stat/chart/types'
+import type { ChartSeries, IntervalData, SeriesSlug } from '~/components/stat/types'
+import type { TrnId, TrnItem } from '~/components/trns/types'
+
+import { getParentCategoryIdOrUndefined } from '~/components/categories/utils'
+
+type BuildParams = {
+ categoriesItems: Categories
+ chartType: ChartType | undefined
+ computeTotalForTrnsIds: (ids: TrnId[]) => { expense: number, income: number, sum: number }
+ filterCategoriesIds?: CategoryId[]
+ intervals: IntervalData[]
+ isGrouped: boolean
+ trnsItems: Record<TrnId, Pick<TrnItem, 'categoryId'>>
+ type: SeriesSlug
+}
+
+function resolveCategoryId(
+ trnCategoryId: CategoryId | undefined,
+ categoriesItems: Categories,
+ isGrouped: boolean,
+): CategoryId | undefined {
+ if (!trnCategoryId)
+ return undefined
+ if (!isGrouped)
+ return trnCategoryId
+ return getParentCategoryIdOrUndefined(categoriesItems, trnCategoryId) ?? trnCategoryId
+}
+
+export function buildCategoriesSeries({
+ categoriesItems,
+ chartType,
+ computeTotalForTrnsIds,
+ filterCategoriesIds,
+ intervals,
+ isGrouped,
+ trnsItems,
+ type,
+}: BuildParams): ChartSeries[] {
+ const filterSet = filterCategoriesIds?.length ? new Set(filterCategoriesIds) : undefined
+
+ // For each interval, build a map of displayCategoryId -> trnIds[]
+ const perIntervalByCategory: Record<CategoryId, TrnId[]>[] = intervals.map((interval) => {
+ const bucket: Record<CategoryId, TrnId[]> = {}
+ for (const trnId of interval.trnsIds) {
+ const rawCategoryId = trnsItems[trnId]?.categoryId
+ if (!rawCategoryId || rawCategoryId === 'transfer')
+ continue
+ if (filterSet && !filterSet.has(rawCategoryId))
+ continue
+
+ const displayId = resolveCategoryId(rawCategoryId, categoriesItems, isGrouped)
+ if (!displayId)
+ continue
+
+ bucket[displayId] ??= []
+ bucket[displayId].push(trnId)
+ }
+ return bucket
+ })
+
+ // Union of category ids across all intervals + their totals over the full range
+ const categoryTotals: Record<CategoryId, number> = {}
+ for (const bucket of perIntervalByCategory) {
+ for (const [catId, trnIds] of Object.entries(bucket)) {
+ const value = computeTotalForTrnsIds(trnIds)[type]
+ categoryTotals[catId] = (categoryTotals[catId] ?? 0) + value
+ }
+ }
+
+ // Sort categories by total descending
+ const orderedCategoryIds = Object.keys(categoryTotals)
+ .filter(id => categoryTotals[id]! > 0)
+ .sort((a, b) => categoryTotals[b]! - categoryTotals[a]!)
+
+ return orderedCategoryIds.map((catId): ChartSeries => {
+ const category = categoriesItems[catId]
+ const data = perIntervalByCategory.map(bucket =>
+ bucket[catId] ? computeTotalForTrnsIds(bucket[catId])[type] : 0,
+ )
+ return {
+ color: category?.color,
+ data,
+ name: category?.name ?? catId,
+ type: chartType ?? 'bar',
+ }
+ })
+}
diff --git a/app/components/stat/statConfig.test.ts b/app/components/stat/statConfig.test.ts
index 7551ccd4..76f9cb44 100644
--- a/app/components/stat/statConfig.test.ts
+++ b/app/components/stat/statConfig.test.ts
@@ -23,13 +23,16 @@ const defaultConfig: MiniItemConfig = {
},
catsView: 'list',
chart: {
+ isGrouped: true,
isShowAverage: false,
+ mode: 'aggregated',
},
chartType: 'bar',
chartView: 'full',
date: {
isShowQuick: false,
},
+ grouping: 'auto',
isChartShow: true,
isShowEmptyCategories: false,
statAverage: {
diff --git a/app/components/stat/useStatConfig.ts b/app/components/stat/useStatConfig.ts
index f20f7422..60789957 100644
--- a/app/components/stat/useStatConfig.ts
+++ b/app/components/stat/useStatConfig.ts
@@ -9,6 +9,17 @@ import { applyConfigUpdate } from '~/components/stat/statConfig'
export const chartViewOptions = ['half', 'full'] as const
+export const groupingModes = ['auto', 'parents', 'children'] as const
+export type GroupingMode = typeof groupingModes[number]
+
+export function resolveGrouped(raw: boolean, grouping: GroupingMode): boolean {
+ if (grouping === 'parents')
+ return true
+ if (grouping === 'children')
+ return false
+ return raw
+}
+
export const ConfigSchema = z.object({
catsList: z.object({
isGrouped: z.boolean(),
@@ -26,13 +37,16 @@ export const ConfigSchema = z.object({
}),
catsView: z.enum(['list', 'round']),
chart: z.object({
+ isGrouped: z.boolean(),
isShowAverage: z.boolean(),
+ mode: z.enum(['aggregated', 'categories']),
}),
chartType: z.enum(chartTypes),
chartView: z.enum(chartViewOptions),
date: z.object({
isShowQuick: z.boolean(),
}),
+ grouping: z.enum(groupingModes),
isChartShow: z.boolean(),
isShowEmptyCategories: z.boolean(),
statAverage: z.object({
@@ -83,7 +97,9 @@ export function useStatConfig({ props, storageKey }: StatConfigParams) {
},
catsView: 'list',
chart: {
+ isGrouped: true,
isShowAverage: false,
+ mode: 'aggregated',
},
chartType: 'bar',
chartView: 'full',
@@ -91,6 +107,7 @@ export function useStatConfig({ props, storageKey }: StatConfigParams) {
isShowQuick: false,
},
+ grouping: 'auto',
isChartShow: true,
isShowEmptyCategories: false,
diff --git a/app/components/stat/useStatItem.test.ts b/app/components/stat/useStatItem.test.ts
index dd95ee3f..d5774b92 100644
--- a/app/components/stat/useStatItem.test.ts
+++ b/app/components/stat/useStatItem.test.ts
@@ -31,6 +31,16 @@ vi.mock('~/components/trns/useTrnsStore', () => ({
}),
}))
+vi.mock('~/components/categories/useCategoriesStore', () => ({
+ useCategoriesStore: () => ({
+ items: {},
+ }),
+}))
+
+vi.mock('~/components/stat/chart/useCategorySeriesBuilder', () => ({
+ buildCategoriesSeries: () => [],
+}))
+
const computeTotalMock = vi.fn((): TotalReturns => ({
adjustment: 0,
expense: 0,
diff --git a/app/components/stat/useStatItem.ts b/app/components/stat/useStatItem.ts
index 03fa5faa..fb67a6f8 100644
--- a/app/components/stat/useStatItem.ts
+++ b/app/components/stat/useStatItem.ts
@@ -12,8 +12,11 @@ import type { StatConfigProvider } from '~/components/stat/useStatConfig'
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 { useStatChart } from '~/components/stat/chart/useStatChart'
import { bucketTrnsByIntervals, computeAverageTotal, isPeriodOneDay as isPeriodOneDayFn } from '~/components/stat/intervals'
+import { resolveGrouped } from '~/components/stat/useStatConfig'
import { getSelectedType, getSelectedTypeForSum, getTypesMapping, getTypesToShow } from '~/components/stat/utils'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
@@ -37,6 +40,7 @@ export function useStatItem({
type,
}: UseStatItemParams) {
const trnsStore = useTrnsStore()
+ const categoriesStore = useCategoriesStore()
const { computeTotalForTrnsIds } = useAmount()
const { createSeriesItem, withMarkArea } = useStatChart()
@@ -131,19 +135,46 @@ export function useStatItem({
return intervals.reduce((acc, i) => acc + i.total[typeSlug], 0) / intervals.length
}
+ const categoriesBreakdownType = computed<SeriesSlug>(() => {
+ if (statTab.value === 'expense')
+ return 'expense'
+ if (statTab.value === 'income')
+ return 'income'
+ if (filteredType.value === 'expense' || filteredType.value === 'income')
+ return filteredType.value
+ return 'expense'
+ })
+
const chartSeries = computed<ChartSeries[]>(() => {
const intervals = intervalsDataWithFilteredCategories.value
- const intervalTotals = intervals.map(g => g.total)
-
- const baseSeries = typesToShow.value.map(t =>
- createSeriesItem(t, intervalTotals, computeSeriesAverage(t, intervals)),
- )
-
const selectedInterval = intervals[statDate.params.value.intervalSelected]
+ const chartType = statConfig.config.value?.chartType
+
+ let baseSeries: ChartSeries[]
+
+ if (statConfig.config.value.chart.mode === 'categories') {
+ baseSeries = buildCategoriesSeries({
+ categoriesItems: categoriesStore.items ?? {},
+ chartType,
+ computeTotalForTrnsIds,
+ filterCategoriesIds: filteredCategoriesIds.value,
+ intervals,
+ isGrouped: resolveGrouped(statConfig.config.value.chart.isGrouped, statConfig.config.value.grouping),
+ trnsItems: trnsStore.items ?? {},
+ type: categoriesBreakdownType.value,
+ })
+ }
+ else {
+ const intervalTotals = intervals.map(g => g.total)
+ baseSeries = typesToShow.value.map(t =>
+ createSeriesItem(t, intervalTotals, computeSeriesAverage(t, intervals)),
+ )
+ }
+
if (!selectedInterval?.range.start || statDate.params.value.intervalSelected < 0)
return baseSeries
- return withMarkArea(baseSeries, selectedInterval.range.start, statConfig.config.value?.chartType)
+ return withMarkArea(baseSeries, selectedInterval.range.start, chartType)
})
const chartXAxisLabels = computed(() =>
diff --git a/i18n/locales/en-US.js b/i18n/locales/en-US.js
index 9d62c361..10e520ee 100644
--- a/i18n/locales/en-US.js
+++ b/i18n/locales/en-US.js
@@ -302,6 +302,8 @@ export default {
average: {
label: 'Show average line',
},
+ byCategories: 'By categories',
+ groupByParent: 'Group by parent',
type: {
label: 'Chart type',
},
@@ -320,6 +322,12 @@ export default {
label: 'Date selector slider',
},
},
+ grouping: {
+ auto: 'Auto',
+ children: 'With children',
+ label: 'Show categories',
+ parents: 'Parents only',
+ },
menu: {
label: 'Page Settings',
},
diff --git a/i18n/locales/ru-RU.js b/i18n/locales/ru-RU.js
index 35c97ff5..b00c3abe 100644
--- a/i18n/locales/ru-RU.js
+++ b/i18n/locales/ru-RU.js
@@ -302,6 +302,8 @@ export default {
average: {
label: 'Показывать средние значения',
},
+ byCategories: 'По категориям',
+ groupByParent: 'Группировать по родителю',
type: {
label: 'Тип графика',
},
@@ -320,6 +322,12 @@ export default {
label: 'Слайдер быстрого выбора даты',
},
},
+ grouping: {
+ auto: 'Авто',
+ children: 'С вложенными',
+ label: 'Показывать категории',
+ parents: 'Только родители',
+ },
menu: {
label: 'Настройки страницы',
},