Open source personal finance application
add new statistics
ilkome3d5f49298247
diff --git a/components/analytics/Analytics.vue b/components/analytics/Analytics.vuenew file mode 100644index 00000000..f5012e8a--- /dev/null+++ b/components/analytics/Analytics.vue@@ -0,0 +1,166 @@+<script>+export default {+ name: 'Analytics',++ props: {+ showCategoriesCharts: {+ type: Boolean,+ default: true+ },+ showInside: {+ type: Boolean,+ default: false+ }+ },++ computed: {+ statCurrentPeriod () {+ return this.$store.getters['stat/statCurrentPeriod']+ }+ }+}+</script>++<template lang="pug">+.wrap++ .block+ PeriodCatsChart(+ v-if="showCategoriesCharts"+ type="expenses"+ )+ .columns+ .item(+ v-for="categoryId in statCurrentPeriod.expenses.categoriesIds"+ :key="categoryId"+ )+ .categoryWrap+ .categoryIcon+ Icon(+ :icon="$store.state.categories.items[categoryId].icon"+ :background="$store.state.categories.items[categoryId].color || $store.state.ui.defaultBgColor"+ :round="true"+ :big="true"+ )+ .modalBottom__header__title {{ $store.state.categories.items[categoryId].name }}++ .item__chart+ AnalyticsChart(:categoryId="categoryId")++ .item__inside+ StatItem(+ :biggest="statCurrentPeriod.expenses.biggest"+ :category="$store.state.categories.items[categoryId]"+ :categoryId="categoryId"+ :currency="$store.state.currencies.base"+ :key="categoryId"+ :type="0"+ :total="statCurrentPeriod.categories[categoryId].expenses"+ )++ .block+ PeriodCatsChart(+ v-if="showCategoriesCharts"+ type="incomes"+ )+ .columns+ .item(+ v-for="categoryId in statCurrentPeriod.incomes.categoriesIds"+ :key="categoryId"+ )+ .categoryWrap+ .categoryIcon+ Icon(+ :icon="$store.state.categories.items[categoryId].icon"+ :background="$store.state.categories.items[categoryId].color || $store.state.ui.defaultBgColor"+ :round="true"+ :big="true"+ )+ .modalBottom__header__title {{ $store.state.categories.items[categoryId].name }}++ .item__chart+ AnalyticsChart(:categoryId="categoryId")++ .item__inside+ StatItem(+ :biggest="statCurrentPeriod.expenses.biggest"+ :category="$store.state.categories.items[categoryId]"+ :categoryId="categoryId"+ :currency="$store.state.currencies.base"+ :key="categoryId"+ :type="1"+ :total="statCurrentPeriod.categories[categoryId].expenses"+ )+</template>++<style lang="stylus">+@import "~assets/stylus/variables"++.item+ .item__inside+ .statItem+ .statItem._active+ margin 0 (- $m6)+ margin-bottom (- $m6)+</style>++<style lang="stylus" scoped>+@import "~assets/stylus/variables"++.block+ padding-bottom $m10++.wrap+ position relative++ +media-tablet()+ max-width 1100px+ padding 60px 60px++.columns+ display grid+ grid-template-columns repeat(1, 1fr)+ grid-column-gap $m9+ grid-row-gap 60px+ padding-top 60px+ padding-bottom $m9++ +media-tablet()+ grid-template-columns repeat(2, 1fr)++.item+ padding $m6+ color var(--c-font-1)+ background var(--c-bg-3)+ border-radius $m6++ &__name+ min-width 100px+ padding $m7+ padding-bottom $m8+ color var(--c-font-2)+ font-size 18px+ fontFamilyNunito()++ &__chart+ flex-grow 1++ &__inside+ padding-top $m6++.categoryWrap+ flex-grow 1+ display flex+ align-items center+ justify-content center+ flex-flow column+ margin-top -38px+ padding-bottom $m5++.categoryIcon+ flex-grow 1+ display flex+ align-items center+ justify-content center+ padding-bottom 12px+</style>diff --git a/components/analytics/AnalyticsChart.vue b/components/analytics/AnalyticsChart.vuenew file mode 100644index 00000000..b6afdf2d--- /dev/null+++ b/components/analytics/AnalyticsChart.vue@@ -0,0 +1,229 @@+<script>+import dayjs from 'dayjs'+import { Chart } from 'highcharts-vue'+import Highcharts from 'highcharts'+import borderRadius from 'highcharts-border-radius'+import chartOptions from '~/components/stat/chartOptions'++borderRadius(Highcharts)++export default {+ name: 'AnalyticsChart',++ components: {+ Chart+ },++ props: {+ categoryId: {+ type: String,+ required: true+ }+ },++ data () {+ return {+ average: 0,+ date: dayjs().valueOf(),+ periodName: 'month',+ visiblePeriodMenu: false,+ dateStart: null,+ dateEnd: null,+ chartOptions: {+ ...chartOptions,+ legend: false+ }+ }+ },++ computed: {+ activeTab () {+ return this.$store.state.dashboard.activeTab === 'balance'+ },++ showedPeriods () {+ return this.$store.state.stat.showedPeriods+ },++ statCurrentPeriod () {+ return this.$store.getters['stat/statCurrentPeriod']+ },++ filterDate () {+ return this.$store.state.filter.date+ }+ },++ watch: {+ showedPeriods (newValue) {+ this.init()+ },++ filterDate (newValue) {+ this.init()+ }+ },++ mounted () {+ this.init()+ },++ methods: {+ init () {+ const data = this.generateData(this.periodName)+ const handlePerioSelect = date => this.$store.dispatch('filter/setDate', parseInt(date))+ this.chartOptions.series = data.series+ this.chartOptions.plotOptions.series = {+ cursor: 'pointer',+ point: {+ events: {+ click () {+ handlePerioSelect(this.date)+ }+ }+ }+ }++ this.chartOptions = {+ ...this.chartOptions,+ xAxis: {+ categories: data.categories+ },++ yAxis: {+ plotLines: [{+ color: 'var(--c-font-5)',+ value: this.average, // Insert your average here+ width: '2',+ zIndex: 1+ }]+ },++ chart: {+ backgroundColor: 'transparent',+ with: '100%',+ height: 160,+ spacing: [5, 0, 0, 0],+ type: 'column',+ zoomType: 'x',+ panning: true,+ panKey: 'shift'+ }+ }+ },++ formatAmount (amount) {+ const fixed = this.$store.state.currencies.base === 'RUB' ? 0 : 2+ return Number(`${amount.toFixed(fixed)}`).toLocaleString('ru-RU')+ },++ generateData (periodName, oldSeries) {+ const trns = this.$store.state.trns.items++ // diff periods from oldest trn and today+ const oldestTrnDate = dayjs(trns[this.$store.getters['trns/firstCreatedTrnId']].date).endOf(periodName)+ let periodsToShow = dayjs().endOf(periodName).diff(oldestTrnDate, periodName) + 1+ periodsToShow = periodsToShow > this.showedPeriods ? this.showedPeriods : periodsToShow++ const categories = []+ const incomesData = []++ for (let index = 0; index < periodsToShow; index++) {+ // count total period+ const periodDate = dayjs().endOf(periodName).subtract(index, periodName).valueOf()+ const name = dayjs(periodDate).format('MMM')+ const trnsIds = this.$store.getters['trns/getTrns']({+ categoryId: this.categoryId,+ date: periodDate,+ periodName+ })+ const periodTotal = this.$store.getters['trns/getTotalOfTrnsIds'](trnsIds)++ // return+ incomesData.unshift({+ date: periodDate,+ y: Math.abs(Number(`${periodTotal.incomes.toFixed() - periodTotal.expenses.toFixed()}`)),+ color: 'var(--c-font-1)',+ marker: {+ radius: 6,+ enabled: dayjs(periodDate).isSame(this.$store.state.filter.date, periodName)+ }+ })+ categories.unshift(name)+ }++ let periods = 0+ let periodsTotal = 0+ for (const iterator of incomesData) {+ if (iterator.y !== 0) {+ periods = periods + 1+ periodsTotal = periodsTotal + iterator.y+ }+ }++ this.average = Math.abs(Number(periodsTotal / periods).toFixed())++ return {+ series: [{+ visible: true,+ type: 'spline',+ name: 'Data',+ color: '#c1c1c1',+ data: incomesData+ }],+ categories+ }+ }+ }+}+</script>++<template lang="pug">+.chart(ref="chart")+ .columns+ .item+ .item__name Выбранный период+ .item__price+ Amount(+ :currency="$store.state.currencies.base"+ :type="3"+ :value="statCurrentPeriod.categories[categoryId].total"+ size="xl"+ vertical="center"+ )++ .item+ .item__name Среднее+ .item__price+ Amount(+ :currency="$store.state.currencies.base"+ :value="average || 0"+ size="xl"+ vertical="center"+ )++ Chart.swiper-no-swiping(:options="chartOptions")+</template>++<style lang="stylus" scoped>+@import "~assets/stylus/variables"++.chart+ z-index 3+ position relative+ padding 20px++.item+ color var(--c-font-4)+ text-align center++ &__name+ padding-bottom $m5++.columns+ display grid+ grid-template-columns repeat(2, 1fr)+ grid-column-gap $m7+ grid-row-gap $m7+ padding-bottom $m7+</style>diff --git a/components/dashboard/Dashboard.vue b/components/dashboard/Dashboard.vueindex fd559291..76a8a5f1 100644--- a/components/dashboard/Dashboard.vue+++ b/components/dashboard/Dashboard.vue@@ -91,6 +91,10 @@ export default {.dashboard__tab(v-if="$store.state.dashboard.activeTab === 'balance'")StatChartDonut(v-if="$store.getters['trns/hasTrns']")StatPc++ transition(name="animation-tab")+ .dashboard__tab(v-if="$store.state.dashboard.activeTab === 'analytics'")+ Analytics</template><style lang="stylus" scoped>diff --git a/components/dashboard/DashboardStatControl.vue b/components/dashboard/DashboardStatControl.vueindex be128a96..baf7794d 100644--- a/components/dashboard/DashboardStatControl.vue+++ b/components/dashboard/DashboardStatControl.vue@@ -22,6 +22,11 @@ export default {:class="{ _active: $store.state.dashboard.activeTab === 'balance' }") {{ $t('stat.balanceTitle') }}+ .tabItem(+ @click="$store.commit('dashboard/setDashboardActiveTab', 'analytics')"+ :class="{ _active: $store.state.dashboard.activeTab === 'analytics' }"+ ) +.tabs.tabItem(:class="{ _disable: $store.state.filter.period === 'all' || $store.getters['stat/isLastPeriodSelected'] }"diff --git a/locales/en-US.js b/locales/en-US.jsindex 4bc9438d..fe206caa 100644--- a/locales/en-US.js+++ b/locales/en-US.js@@ -1,4 +1,8 @@export default {+ analytics: {+ title: 'Analytics'+ },+appName: 'Finapp',for: 'for',diff --git a/locales/ru-RU.js b/locales/ru-RU.jsindex 77fa2292..dd8f8a6b 100644--- a/locales/ru-RU.js+++ b/locales/ru-RU.js@@ -1,5 +1,9 @@export default {+ analytics: {+ title: 'Аналитика'+ },+appName: 'Финапка',for: 'за',