Skip to content

Open source personal finance application

refactor: streamline date utilities and chart configuration

Ilya Komicheva6ed72d9ec7b
diff --git a/app/components/date/utils.ts b/app/components/date/utils.ts
index 66e0e1d2..c257cbb1 100644
--- a/app/components/date/utils.ts
+++ b/app/components/date/utils.ts
@@ -3,7 +3,6 @@ import { ru } from 'date-fns/locale'
import type { LocaleSlug } from '~/components/app/locale/types'
import type { IntervalsInRangeProps, Range } from '~/components/date/types'
-import type { PeriodNameWithAll } from '~/components/filter/types'
export function formatByLocale(date: Date, formatter: string, locale?: LocaleSlug) {
const formatOptions = locale === 'ru' ? { locale: ru } : {}
@@ -94,18 +93,3 @@ export function getEndOf(date: Date, intervalType: string): Date {
return endOfDay(date)
}
}
-
-export function getFormatForChart(periodName: PeriodNameWithAll) {
- switch (periodName) {
- case 'day':
- return 'd MMM'
- case 'week':
- return 'd MMM'
- case 'month':
- return 'MMM'
- case 'year':
- return 'yyyy'
- default:
- return 'MM'
- }
-}
diff --git a/app/components/stat/chart/View.vue b/app/components/stat/chart/View.vue
index 684440b5..a4b6178a 100644
--- a/app/components/stat/chart/View.vue
+++ b/app/components/stat/chart/View.vue
@@ -7,11 +7,13 @@ import { SVGRenderer } from 'echarts/renderers'
import VChart from 'vue-echarts'
import type { Period } from '~/components/date/types'
+import type { PeriodNameWithAll } from '~/components/filter/types'
import type { ChartType } from '~/components/stat/chart/types'
-import { formatByLocale, getFormatForChart } from '~/components/date/utils'
+import { formatByLocale } from '~/components/date/utils'
import { config, lineConfig } from '~/components/stat/chart/config'
-import { getLocalAmount, setChartXAxis } from '~/components/stat/chart/utils'
+import { getLocalAmount } from '~/components/stat/chart/utils'
+import { useGetDateRange } from '~/components/stat/date/useGetDateRange'
type ChartParams = {
color?: string
@@ -56,9 +58,25 @@ use([
TooltipComponent,
])
-const { locale } = useI18n()
+const { locale, t } = useI18n()
+const { getStringDateRange } = useGetDateRange(t, locale.value)
const chartRef = ref()
+function getFormatForChart(periodName: PeriodNameWithAll) {
+ switch (periodName) {
+ case 'day':
+ return 'd MMM'
+ case 'week':
+ return 'd MMM'
+ case 'month':
+ return 'MMM'
+ case 'year':
+ return 'yyyy'
+ default:
+ return 'MM'
+ }
+}
+
const option = computed(() => {
const data = defu(config, {
series: setChartSeries(props.series),
@@ -91,14 +109,24 @@ const option = computed(() => {
return `${content}</div></div>`
},
},
- xAxis: setChartXAxis(props.xAxisLabels),
+ xAxis: {
+ data: props.xAxisLabels,
+ type: 'category',
+ },
})
data.xAxis.axisLabel.formatter = (date: string) => {
- return formatByLocale(new Date(+date), getFormatForChart(props.period), locale.value)
+ if (props.period === 'day') {
+ return getStringDateRange({
+ end: new Date(+date).getTime(),
+ start: new Date(+date).getTime(),
+ }, 'day', 1)
+ }
+
+ return formatByLocale(new Date(+date), getFormatForChart(props.period, new Date(+date)), locale.value)
}
data.xAxis.axisPointer.label.formatter = ({ value }: { value: string }) => {
- return formatByLocale(new Date(+value), getFormatForChart(props.period), locale.value)
+ return formatByLocale(new Date(+value), getFormatForChart(props.period, new Date(+value)), locale.value)
}
return data
diff --git a/app/components/stat/chart/utils.ts b/app/components/stat/chart/utils.ts
index a459f4c2..75316f13 100644
--- a/app/components/stat/chart/utils.ts
+++ b/app/components/stat/chart/utils.ts
@@ -5,13 +5,6 @@ export function markArea(value: number) {
}
}
-export function setChartXAxis(categories: number[]) {
- return {
- data: categories,
- type: 'category',
- }
-}
-
export function getCompactAmount(amount: number) {
return Intl.NumberFormat('en', { notation: 'compact' }).format(amount)
}