Browse Source

Improve X-axis scale

pull/3010/head
Thomas Kaul 2 years ago
parent
commit
da1dce57df
  1. 36
      apps/client/src/app/components/investment-chart/investment-chart.component.ts

36
apps/client/src/app/components/investment-chart/investment-chart.component.ts

@ -38,8 +38,16 @@ import {
} from 'chart.js'; } from 'chart.js';
import 'chartjs-adapter-date-fns'; import 'chartjs-adapter-date-fns';
import annotationPlugin from 'chartjs-plugin-annotation'; import annotationPlugin from 'chartjs-plugin-annotation';
import { addDays, format, isAfter, parseISO, subDays } from 'date-fns'; import {
import { last } from 'lodash'; addDays,
format,
isAfter,
isValid,
min,
parseISO,
subDays
} from 'date-fns';
import { first, last } from 'lodash';
@Component({ @Component({
selector: 'gf-investment-chart', selector: 'gf-investment-chart',
@ -143,7 +151,7 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
}); });
} }
const chartData: ChartData<'line'> = { const chartData: ChartData<'bar' | 'line'> = {
labels: this.historicalDataItems.map(({ date }) => { labels: this.historicalDataItems.map(({ date }) => {
return parseDate(date); return parseDate(date);
}), }),
@ -194,17 +202,23 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
}; };
if (this.chartCanvas) { if (this.chartCanvas) {
let scaleXMin: string;
if (this.daysInMarket) {
const minDate = min([
parseDate(first(this.investments)?.date),
subDays(new Date().setHours(0, 0, 0, 0), this.daysInMarket)
]);
scaleXMin = isValid(minDate) ? minDate.toISOString() : undefined;
}
if (this.chart) { if (this.chart) {
this.chart.data = chartData; this.chart.data = chartData;
this.chart.options.plugins.tooltip = <unknown>( this.chart.options.plugins.tooltip = <unknown>(
this.getTooltipPluginConfiguration() this.getTooltipPluginConfiguration()
); );
this.chart.options.scales.x.min = this.daysInMarket this.chart.options.scales.x.min = scaleXMin;
? subDays(
new Date().setHours(0, 0, 0, 0),
this.daysInMarket
).toISOString()
: undefined;
if ( if (
this.savingsRate && this.savingsRate &&
@ -287,9 +301,7 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
grid: { grid: {
display: false display: false
}, },
min: this.daysInMarket min: scaleXMin,
? subDays(new Date(), this.daysInMarket).toISOString()
: undefined,
suggestedMax: new Date().toISOString(), suggestedMax: new Date().toISOString(),
type: 'time', type: 'time',
time: { time: {

Loading…
Cancel
Save