Open source personal finance application
experimental(Total): init page with chart
ilkome9784934092d8
diff --git a/components/chart/Base.vue b/components/chart/Base.vuenew file mode 100644index 00000000..fd784a53--- /dev/null+++ b/components/chart/Base.vue@@ -0,0 +1,58 @@+<script setup lang="ts">+import { use } from 'echarts/core'+import { SVGRenderer } from 'echarts/renderers'+import { BarChart, LineChart } from 'echarts/charts'+import VChart from 'vue-echarts'++import {+ DataZoomComponent,+ DataZoomInsideComponent,+ DataZoomSliderComponent,+ GridComponent,+ LegendComponent,+ MarkAreaComponent,+ MarkLineComponent,+ TooltipComponent,+} from 'echarts/components'++defineProps<{+ option: object+ loading?: boolean+}>()++const emit = defineEmits(['finished', 'onClickChart'])++use([+ BarChart,+ DataZoomComponent,+ DataZoomInsideComponent,+ DataZoomSliderComponent,+ GridComponent,+ LegendComponent,+ LineChart,+ MarkAreaComponent,+ MarkLineComponent,+ SVGRenderer,+ TooltipComponent,+])++const chart = ref()+function finished() {+ emit('finished', chart.value)+}++function onClickChart(params: any) {+ emit('onClickChart', params)+}+</script>++<template lang="pug">+VChart.w-full(+ :loading="loading"+ :option="option"+ @finished="finished"+ @zr:click="onClickChart"+ autoresize+ ref="chart"+)+</template>diff --git a/components/chart/chartOprions.ts b/components/chart/chartOprions.tsnew file mode 100644index 00000000..b6788f2a--- /dev/null+++ b/components/chart/chartOprions.ts@@ -0,0 +1,132 @@+export const marklineActiveConfig = {+ symbol: [false, false],+ symbolSize: 10,+ itemStyle: {+ borderCap: 'round',+ color: 'var(--color-item-main-active)',+ },+ lineStyle: {+ width: 5,+ type: 'solid',+ opacity: 1,+ },+ label: {+ show: false,+ },+}++export const baseSeriesConfig = {+ z: 10,+ smooth: true,+ barMaxWidth: 16,+ symbol: 'circle',+ symbolSize: 6,+ lineStyle: {+ width: 2,+ },+ label: {+ show: true,+ backgroundColor: 'var(--c-item-bg-main)',+ borderRadius: 2,+ color: 'var(--color-item-base-down)',+ fontFamily: 'var(--font-secondary)',+ fontSize: '10',+ padding: [2, 0],+ position: 'top',+ },+}++export const baseSeriesItemStyle = {+ borderRadius: [4, 4, 0, 0],+}++export const averageLine = {+ z: 1,+ silent: true,+ symbol: [false, false],+ data: [{+ type: 'average',+ name: 'Avg',+ label: {+ show: false,+ },+ }],+ lineStyle: {+ type: 'solid',+ opacity: 0.7,+ },+}++// Options+export const options = {+ // Grid+ grid: {+ top: '30',+ left: '10',+ right: '10',+ bottom: '30',+ containLabel: true,+ },++ // Categories+ xAxis: {+ type: 'category',+ axisTick: {+ alignWithLabel: true,+ },+ axisLabel: {+ color: 'var(--color-item-base-down)',+ fontFamily: 'var(--font-secondary)',+ fontSize: '10',+ },+ },++ yAxis: {+ type: 'value',+ axisPointer: {+ snap: true,+ },+ position: 'right',+ axisTick: {+ lineStyle: {+ color: 'blue',+ },+ },+ splitLine: {+ lineStyle: {+ color: 'var(--c-bg-5)',+ },+ },+ axisLabel: {+ color: 'var(--color-item-base-down)',+ fontFamily: 'var(--font-secondary)',+ fontSize: '10',+ },+ },++ // Tooltip+ tooltip: {+ trigger: 'axis',+ axisPointer: {+ type: 'cross',+ // type: 'shadow',+ label: {+ backgroundColor: 'var(--color-item-main-active)',+ },+ },+ // valueFormatter: value => `${formatAmount(value)} ${getCurrencySymbol(baseCurrency.value)}`,++ // position(pos, params, dom, rect, size) {+ // // tooltip will be fixed on the right if mouse hovering on the left,+ // // and on the left if hovering on the right.+ // const obj = { top: -60 }+ // obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 0+ // return obj+ // },+ backgroundColor: 'var(--c-item-bg-main)',+ borderColor: 'var(--c-item-bg-active)',+ textStyle: {+ color: 'var(--color-item-base)',+ },+ },+}diff --git a/components/wallets/page/List.vue b/components/wallets/page/List.vueindex 1ecab4a1..4461fb8a 100644--- a/components/wallets/page/List.vue+++ b/components/wallets/page/List.vue@@ -33,6 +33,16 @@ UiPage.pb-12.px-3WalletsTotal+ //- Total+ .pb-4.px-3(v-if="$store.getters['user/isDevUser']")+ .flex+ .cursor-pointer.p-1.px-3.flex.items-center.gap-3.rounded-md.bg-skin-item-main-bg.hocus_bg-skin-item-main-hover(+ @click="$router.push('/wallets/total')"+ )+ .mdi.mdi-poll.text-xl+ .text-xs.leading-none.text-skin-item-base Total details+ .mdi.mdi-chevron-right.text-lg.leading-none.text-skin-item-base-down+//- List//---------------------------------WalletsList(#default="{ walletsItemsSorted }")diff --git a/pages/wallets/total.vue b/pages/wallets/total.vuenew file mode 100644index 00000000..c11b9f46--- /dev/null+++ b/pages/wallets/total.vue@@ -0,0 +1,243 @@+<script setup lang="ts">+import _merge from 'lodash.merge'+import _minby from 'lodash.minby'+import _sortby from 'lodash.sortby'+import dayjs from 'dayjs'+import type { ComputedRef, Ref } from '@vue/composition-api'+import type { PeriodsNamesExeptAll } from '~/components/date/types'+import type { TrnID, TrnItem } from '~/components/trns/types'+import useAmount from '~/components/amount/useAmount'+import { averageLine, baseSeriesItemStyle, options } from '~/components/chart/chartOprions'+import { getTransferCatgoriesIds } from '~/components/categories/getCategories'+import { getTotal } from '~/components/trns/getTotal'+import { getTrnsIds } from '~/components/trns/getTrns'++type DateValueOf = number++const { baseCurrency, formatAmount, getCurrencySymbol } = useAmount()+const { $store } = useNuxtApp()++/**+ * Get oldest TrnItem+ */+function getOldestTrnDate(trnsItems: Record<TrnID, TrnItem>): DateValueOf {+ const startOfToday = dayjs().startOf('day').valueOf()+ return _minby(Object.values(trnsItems), 'date')?.date || startOfToday+}++/**+ * Get max periods to show+ */+function getMaxPeriodsToShow({ periodName, fromDate }: { periodName: PeriodsNamesExeptAll; fromDate: DateValueOf }) {+ return dayjs().endOf(periodName).diff(fromDate, periodName) + 1+}++/**+ * Transactions+ */+const trnsItems: ComputedRef<Record<TrnID, TrnItem>> = computed(() => $store.state.trns.items)+const trnsIds: ComputedRef<TrnID[]> = computed(() => Object.keys(trnsItems.value))+const oldestTrnDate = getOldestTrnDate(trnsItems.value)++/**+ * Config+ */+const activePeriod: Ref<PeriodsNamesExeptAll> = ref('week')++/**+ * Periods+ */+const periods: PeriodsNamesExeptAll[] = ['day', 'week', 'month', 'year']+const maxPeriodsNumber = computed(() => getMaxPeriodsToShow({ periodName: activePeriod.value, fromDate: oldestTrnDate }))++/**+ * Get Period date from until+ */+function getPeriodDates({ date, period }: { date: DateValueOf; period: PeriodsNamesExeptAll }) {+ return {+ from: dayjs(date).startOf(period).valueOf(),+ until: dayjs(date).endOf(period).valueOf(),+ }+}++function getLastDateOrToday({ date }: { date: DateValueOf }) {+ const today = dayjs().endOf('day').valueOf()+ if (date > today)+ return today++ return date+}++function getPeriodsDates({ fromDate, periodName, periodsCount }: {+ fromDate: DateValueOf+ periodName: PeriodsNamesExeptAll+ periodsCount: number+}) {+ const periods = []++ for (let index = 0; index < periodsCount; index++) {+ periods.push({+ date: dayjs(fromDate).startOf(periodName).add(index, periodName).startOf(periodName).format(),+ })+ }++ return periods+}++const periodshey = computed(() => getPeriodsDates({+ fromDate: oldestTrnDate,+ periodName: activePeriod.value,+ periodsCount: maxPeriodsNumber.value,+}))++const series = computed(() => periodshey.value.map((period) => {+ const periodDates = getPeriodDates({ date: period.date, period: activePeriod.value })+ const trnsIds = getTrnsIds({+ fromDate: periodDates.from,+ untilDate: periodDates.until,+ trnsItems: trnsItems.value,+ })++ const categoriesItems = $store.state.categories.items+ const walletsItems = $store.state.wallets.items+ const baseRate = $store.state.currencies.base+ const rates = $store.state.currencies.rates+ const transferCategoriesIds = getTransferCatgoriesIds(categoriesItems)+ const total = getTotal({+ baseRate,+ rates,+ transferCategoriesIds,+ trnsIds,+ trnsItems: trnsItems.value,+ walletsItems,+ })++ return {+ date: period.date,+ trnsIds,+ total,+ }+}))++const seriesTotal = computed(() => {+ const t = []+ for (const [idx, element] of series.value.entries()) {+ const sum = element.total.sumTransfers + element.total.sumTransactions+ idx > 0+ ? t.push(t[idx - 1] + sum)+ : t.push(sum)+ }++ return t+})++/**+ * Chart oprions+ */+const chartOptions = computed(() => ({+ xAxis: {+ type: 'category',+ data: series.value.map(period => dayjs(period.date).format('DD MMMM')),+ },++ series: [{+ name: 'Income',+ type: 'bar',+ data: series.value.map(period => period.total.incomeTransactions),+ markLine: averageLine,+ itemStyle: {+ ...baseSeriesItemStyle,+ color: 'green',+ },+ }, {+ name: 'Expense',+ type: 'bar',+ data: series.value.map(period => period.total.expenseTransactions),+ markLine: averageLine,+ itemStyle: {+ ...baseSeriesItemStyle,+ color: 'red',+ },+ }, {+ name: 'Total',+ type: 'line',+ data: seriesTotal.value,+ markLine: averageLine,+ itemStyle: {+ ...baseSeriesItemStyle,+ color: '#c1c1c1',+ },+ }],++ tooltip: {+ valueFormatter: value => `${formatAmount(value)} ${getCurrencySymbol(baseCurrency.value)}`,+ },++ dataZoom: [{+ type: 'inside',+ start: 0,+ end: 100,+ }, {+ start: 0,+ end: 0,+ }],++ legend: {},+}))++/**+ * Chart data+ */+const chartData = computed(() => _merge(JSON.parse(JSON.stringify(options)), chartOptions.value))+</script>++<template lang="pug">+.h-full.overflow-hidden.overflow-y-auto+ h1 periods+ .flex+ div.px-2(+ v-for="date in periods"+ :key="date"+ @click="activePeriod = date"+ ) {{ date }}++ h1 series+ //- pre {{ series }}++ h1(@click="loading = !loading") loading {{ loading }}+ //- pre {{ series }}++ h1 seriesTotal+ //- pre {{ seriesTotal }}++ h1 storeFilter+ //- pre {{ storeFilter }}++ h1 date+ pre {{ dayjs(date).format('YYYY-MM-DD HH:mm') }}++ h1 activePeriod+ pre {{ activePeriod }}++ h1 oldestTrnDate+ pre {{ dayjs(oldestTrnDate).format('YYYY-MM-DD HH:mm') }}++ h1 maxPeriodsNumber+ pre {{ maxPeriodsNumber }}++ .my-6.mx-2.py-2.rounded-lg.bg-skin-item-main-bg.border.dark_border-neutral-800+ ChartBase.h-80(+ @finished="finished"+ :option="chartData"+ )++ .my-6.mx-2+ h1 {{ trnsIds.length }}++ h1 chartOptions.xAxis+ //- pre {{ chartOptions }}++ TrnsHistory(+ :trnsIds="trnsIds"+ )+</template>