create investment endpoint for analysis timeline

Co-authored-by: Thomas <dotsilver@gmail.com>
This commit is contained in:
Valentin Zickner
2021-07-31 21:33:45 +02:00
committed by Thomas
parent 4a0695613e
commit de83dc7b84
8 changed files with 78 additions and 57 deletions

View File

@@ -10,16 +10,16 @@ import {
ViewChild
} from '@angular/core';
import { primaryColorRgb } from '@ghostfolio/common/config';
import { PortfolioItem } from '@ghostfolio/common/interfaces';
import {
Chart,
LinearScale,
LineController,
LineElement,
LinearScale,
PointElement,
TimeScale
} from 'chart.js';
import { Chart } from 'chart.js';
import { addMonths, isAfter, parseISO, subMonths } from 'date-fns';
import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.interface';
@Component({
selector: 'gf-investment-chart',
@@ -28,7 +28,7 @@ import { addMonths, isAfter, parseISO, subMonths } from 'date-fns';
styleUrls: ['./investment-chart.component.scss']
})
export class InvestmentChartComponent implements OnChanges, OnDestroy, OnInit {
@Input() portfolioItems: PortfolioItem[];
@Input() investments: InvestmentItem[];
@ViewChild('chartCanvas') chartCanvas;
@@ -48,7 +48,7 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy, OnInit {
public ngOnInit() {}
public ngOnChanges() {
if (this.portfolioItems) {
if (this.investments) {
this.initialize();
}
}
@@ -60,32 +60,32 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy, OnInit {
private initialize() {
this.isLoading = true;
if (this.portfolioItems?.length > 0) {
if (this.investments?.length > 0) {
// Extend chart by three months (before)
const firstItem = this.portfolioItems[0];
this.portfolioItems.unshift({
const firstItem = this.investments[0];
this.investments.unshift({
...firstItem,
date: subMonths(parseISO(firstItem.date), 3).toISOString(),
investment: 0
});
// Extend chart by three months (after)
const lastItem = this.portfolioItems[this.portfolioItems.length - 1];
this.portfolioItems.push({
const lastItem = this.investments[this.investments.length - 1];
this.investments.push({
...lastItem,
date: addMonths(parseISO(lastItem.date), 3).toISOString()
});
}
const data = {
labels: this.portfolioItems.map((position) => {
labels: this.investments.map((position) => {
return position.date;
}),
datasets: [
{
borderColor: `rgb(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b})`,
borderWidth: 2,
data: this.portfolioItems.map((position) => {
data: this.investments.map((position) => {
return position.investment;
}),
segment: {

View File

@@ -12,6 +12,7 @@ import {
import { DeviceDetectorService } from 'ngx-device-detector';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.interface';
@Component({
selector: 'gf-analysis-page',
@@ -35,7 +36,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
{ label: 'Initial', value: 'original' },
{ label: 'Current', value: 'current' }
];
public portfolioItems: PortfolioItem[];
public investments: InvestmentItem[];
public portfolioPositions: { [symbol: string]: PortfolioPosition };
public positions: { [symbol: string]: any };
public positionsArray: PortfolioPosition[];
@@ -71,10 +72,10 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
});
this.dataService
.fetchPortfolio()
.fetchInvestments()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((response) => {
this.portfolioItems = response;
this.investments = response;
this.changeDetectorRef.markForCheck();
});

View File

@@ -202,7 +202,7 @@
</mat-card-header>
<mat-card-content>
<gf-investment-chart
[portfolioItems]="portfolioItems"
[investments]='investments'
></gf-investment-chart>
</mat-card-content>
</mat-card>

View File

@@ -36,6 +36,7 @@ import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { SettingsStorageService } from './settings-storage.service';
import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.interface';
@Injectable({
providedIn: 'root'
@@ -143,8 +144,8 @@ export class DataService {
);
}
public fetchPortfolio() {
return this.http.get<PortfolioItem[]>('/api/portfolio');
public fetchInvestments() {
return this.http.get<InvestmentItem[]>('/api/portfolio/investments');
}
public fetchPortfolioOverview() {