diff --git a/src/components/Charts/chartUtils.ts b/src/components/Charts/chartUtils.ts index 4b6096d5..be7d0e67 100644 --- a/src/components/Charts/chartUtils.ts +++ b/src/components/Charts/chartUtils.ts @@ -28,3 +28,23 @@ export function euclideanDistance( const dsq = Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2); return Math.sqrt(dsq); } + +export function getYMax(points: Array>): number { + const maxVal = Math.max(...points.flat()); + const sign = maxVal >= 0 ? 1 : -1; + const texp = 10 ** Math.floor(Math.log10(Math.abs(maxVal))); + if (sign === 1) { + return Math.ceil(maxVal / texp) * texp; + } + return Math.floor(maxVal / texp) * texp; +} + +export function getYMin(points: Array>): number { + const minVal = Math.min(...points.flat()); + const sign = minVal >= 0 ? 1 : -1; + const texp = 10 ** Math.floor(Math.log10(Math.abs(minVal))); + if (sign === 1) { + return Math.ceil(minVal / texp) * texp; + } + return Math.floor(minVal / texp) * texp; +} diff --git a/src/pages/Dashboard/Cashflow.vue b/src/pages/Dashboard/Cashflow.vue index 9fd37104..b8392a68 100644 --- a/src/pages/Dashboard/Cashflow.vue +++ b/src/pages/Dashboard/Cashflow.vue @@ -101,6 +101,7 @@ import PeriodSelector from './PeriodSelector'; import Cashflow from '../../../reports/Cashflow/Cashflow'; import { getDatesAndPeriodicity } from './getDatesAndPeriodicity'; import LineChart from '@/components/Charts/LineChart.vue'; +import { getYMax } from '@/components/Charts/chartUtils'; export default { name: 'Cashflow', @@ -126,17 +127,13 @@ export default { return !(totalInflow === 0 && totalOutflow === 0); }, chartData() { - const xLabels = this.periodList.map((l) => l.split(' ')[0]); + const xLabels = this.periodList; const points = ['inflow', 'outflow'].map((k) => this.data.map((d) => d[k]) ); const colors = ['#2490EF', '#B7BFC6']; const format = (value) => frappe.format(value ?? 0, 'Currency'); - - const maxVal = Math.max(...points.flat()); - const texp = 10 ** Math.floor(Math.log10(maxVal)); - const yMax = Math.ceil(maxVal / texp) * texp; - + const yMax = getYMax(points); return { points, xLabels, colors, format, yMax }; }, },