Skip to content

Open source personal finance application

refactor(chart): update series options and improve config types for better type safety

Ilya Komichevc480a93dc97c
diff --git a/app/components/stat/chart/View.vue b/app/components/stat/chart/View.vue
index af2c2608..997f3fd9 100644
--- a/app/components/stat/chart/View.vue
+++ b/app/components/stat/chart/View.vue
@@ -15,13 +15,6 @@ import { config, lineConfig } from '~/components/stat/chart/config'
import { getLocalAmount } from '~/components/stat/chart/utils'
import { useGetDateRange } from '~/components/stat/date/useGetDateRange'
-type ChartParams = {
- color?: string
- name: string
- seriesName: string
- value: number
-}
-
const props = withDefaults(
defineProps<{
chartType?: ChartType
diff --git a/app/components/stat/chart/config.ts b/app/components/stat/chart/config.ts
index 862c04b9..7d0f310c 100644
--- a/app/components/stat/chart/config.ts
+++ b/app/components/stat/chart/config.ts
@@ -1,31 +1,39 @@
-import type { ChartType } from '~/components/stat/chart/types'
+import type { GridComponentOption } from 'echarts'
+import type { BarSeriesOption, LineSeriesOption, PieSeriesOption } from 'echarts/charts'
+import type { TooltipComponentOption } from 'echarts/components'
+import type { ComposeOption } from 'echarts/core'
+
import type { SeriesSlug } from '~/components/stat/types'
import { getCompactAmount, getLocalAmount } from '~/components/stat/chart/utils'
-type SeriesOption = {
- color: string
- colorLine?: string
+type EChartsOption = ComposeOption<
+ | TooltipComponentOption
+ | PieSeriesOption
+ | GridComponentOption
+>
+
+type SeriesOption = (BarSeriesOption | LineSeriesOption) & {
localeKey: string
- type: ChartType
+ markLineColor?: string
}
export const seriesOptions: Record<SeriesSlug, SeriesOption> = {
expense: {
color: 'var(--expense-1)',
- colorLine: 'var(--expense-2)',
localeKey: 'money.expense',
+ markLineColor: 'var(--expense-2)',
type: 'bar',
},
income: {
color: 'var(--income-1)',
- colorLine: 'var(--income-2)',
localeKey: 'money.expense',
+ markLineColor: 'var(--income-2)',
type: 'bar',
},
}
-export const config = {
+export const config: EChartsOption['baseOption'] = {
// Grid
grid: {
bottom: '0',
@@ -60,7 +68,9 @@ export const config = {
},
axisPointer: {
label: {
+ backgroundColor: 'var(--chart-line)',
color: 'var(--chart-axisLabel)',
+ margin: 10,
},
},
axisTick: {
@@ -76,7 +86,6 @@ export const config = {
formatter: (n: number) => getCompactAmount(n),
show: false, // TODO: config
},
-
axisLine: {
lineStyle: {
color: 'var(--chart-splitLine)',
@@ -84,8 +93,9 @@ export const config = {
},
axisPointer: {
label: {
+ backgroundColor: 'var(--chart-line)',
color: 'var(--chart-axisLabel)',
- formatter: (props: { value: number } | undefined) => props ? getLocalAmount(props.value) : '',
+ formatter: props => getLocalAmount(+props.value) ?? '',
},
snap: true,
},
@@ -99,7 +109,6 @@ export const config = {
show: false,
},
type: 'value',
-
},
}
diff --git a/app/components/stat/chart/useStatChart.ts b/app/components/stat/chart/useStatChart.ts
index a20ec4bd..73182811 100644
--- a/app/components/stat/chart/useStatChart.ts
+++ b/app/components/stat/chart/useStatChart.ts
@@ -32,7 +32,7 @@ export function useStatChart() {
show: false,
},
lineStyle: {
- color: seriesOptions[typeItem]?.colorLine ?? seriesOptions[typeItem]?.color,
+ color: seriesOptions[typeItem]?.markLineColor ?? seriesOptions[typeItem]?.color,
type: 'solid',
},
silent: false,