2021-04-13 21:53:58 +02:00
|
|
|
import 'chartjs-adapter-date-fns';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
ViewChild
|
|
|
|
} from '@angular/core';
|
2021-05-16 22:11:14 +02:00
|
|
|
import { primaryColorRgb } from '@ghostfolio/common/config';
|
2021-08-01 09:41:44 +02:00
|
|
|
import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.interface';
|
2021-04-13 21:53:58 +02:00
|
|
|
import {
|
2021-07-31 21:33:45 +02:00
|
|
|
Chart,
|
2021-04-13 21:53:58 +02:00
|
|
|
LineController,
|
|
|
|
LineElement,
|
2021-08-01 09:41:44 +02:00
|
|
|
LinearScale,
|
2021-04-13 21:53:58 +02:00
|
|
|
PointElement,
|
|
|
|
TimeScale
|
|
|
|
} from 'chart.js';
|
2021-07-03 11:32:03 +02:00
|
|
|
import { addMonths, isAfter, parseISO, subMonths } from 'date-fns';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'gf-investment-chart',
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
templateUrl: './investment-chart.component.html',
|
|
|
|
styleUrls: ['./investment-chart.component.scss']
|
|
|
|
})
|
|
|
|
export class InvestmentChartComponent implements OnChanges, OnDestroy, OnInit {
|
2021-07-31 21:33:45 +02:00
|
|
|
@Input() investments: InvestmentItem[];
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@ViewChild('chartCanvas') chartCanvas;
|
|
|
|
|
|
|
|
public chart: Chart;
|
|
|
|
public isLoading = true;
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
Chart.register(
|
2021-05-30 20:39:37 +02:00
|
|
|
LinearScale,
|
2021-04-13 21:53:58 +02:00
|
|
|
LineController,
|
|
|
|
LineElement,
|
|
|
|
PointElement,
|
|
|
|
TimeScale
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {}
|
|
|
|
|
|
|
|
public ngOnChanges() {
|
2021-07-31 21:33:45 +02:00
|
|
|
if (this.investments) {
|
2021-04-13 21:53:58 +02:00
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 11:32:03 +02:00
|
|
|
public ngOnDestroy() {
|
|
|
|
this.chart?.destroy();
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
private initialize() {
|
|
|
|
this.isLoading = true;
|
|
|
|
|
2021-07-31 21:33:45 +02:00
|
|
|
if (this.investments?.length > 0) {
|
2021-07-03 11:32:03 +02:00
|
|
|
// Extend chart by three months (before)
|
2021-07-31 21:33:45 +02:00
|
|
|
const firstItem = this.investments[0];
|
|
|
|
this.investments.unshift({
|
2021-07-03 11:32:03 +02:00
|
|
|
...firstItem,
|
|
|
|
date: subMonths(parseISO(firstItem.date), 3).toISOString(),
|
|
|
|
investment: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
// Extend chart by three months (after)
|
2021-07-31 21:33:45 +02:00
|
|
|
const lastItem = this.investments[this.investments.length - 1];
|
|
|
|
this.investments.push({
|
2021-07-03 11:32:03 +02:00
|
|
|
...lastItem,
|
2021-08-08 19:25:38 +02:00
|
|
|
date: addMonths(new Date(), 3).toISOString()
|
2021-07-03 11:32:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
const data = {
|
2021-07-31 21:33:45 +02:00
|
|
|
labels: this.investments.map((position) => {
|
2021-04-13 21:53:58 +02:00
|
|
|
return position.date;
|
|
|
|
}),
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
borderColor: `rgb(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b})`,
|
|
|
|
borderWidth: 2,
|
2021-07-31 21:33:45 +02:00
|
|
|
data: this.investments.map((position) => {
|
2021-04-13 21:53:58 +02:00
|
|
|
return position.investment;
|
2021-07-03 11:32:03 +02:00
|
|
|
}),
|
|
|
|
segment: {
|
|
|
|
borderColor: (context: unknown) =>
|
|
|
|
this.isInFuture(
|
|
|
|
context,
|
|
|
|
`rgba(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b}, 0.67)`
|
|
|
|
),
|
|
|
|
borderDash: (context: unknown) => this.isInFuture(context, [2, 2])
|
|
|
|
},
|
|
|
|
stepped: true
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.chartCanvas) {
|
|
|
|
if (this.chart) {
|
|
|
|
this.chart.data = data;
|
|
|
|
this.chart.update();
|
|
|
|
} else {
|
|
|
|
this.chart = new Chart(this.chartCanvas.nativeElement, {
|
|
|
|
data,
|
|
|
|
options: {
|
|
|
|
elements: {
|
|
|
|
line: {
|
|
|
|
tension: 0
|
|
|
|
},
|
|
|
|
point: {
|
|
|
|
radius: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
maintainAspectRatio: true,
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
responsive: true,
|
|
|
|
scales: {
|
|
|
|
x: {
|
2021-05-30 20:39:37 +02:00
|
|
|
display: true,
|
2021-04-13 21:53:58 +02:00
|
|
|
grid: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
type: 'time',
|
|
|
|
time: {
|
|
|
|
unit: 'year'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
display: false,
|
|
|
|
grid: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
type: 'line'
|
|
|
|
});
|
|
|
|
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
private isInFuture<T>(aContext: any, aValue: T) {
|
|
|
|
return isAfter(new Date(aContext?.p1?.parsed?.x), new Date())
|
2021-07-03 11:32:03 +02:00
|
|
|
? aValue
|
|
|
|
: undefined;
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
}
|