Browse Source

feat(lib): make chartCanvas a view child signal

pull/6491/head
KenTandrian 4 weeks ago
parent
commit
2ad9109024
  1. 132
      libs/ui/src/lib/portfolio-proportion-chart/portfolio-proportion-chart.component.ts

132
libs/ui/src/lib/portfolio-proportion-chart/portfolio-proportion-chart.component.ts

@ -18,7 +18,7 @@ import {
OnChanges, OnChanges,
OnDestroy, OnDestroy,
Output, Output,
ViewChild viewChild
} from '@angular/core'; } from '@angular/core';
import { DataSource } from '@prisma/client'; import { DataSource } from '@prisma/client';
import { Big } from 'big.js'; import { Big } from 'big.js';
@ -83,11 +83,11 @@ export class GfPortfolioProportionChartComponent
@Output() proportionChartClicked = new EventEmitter<AssetProfileIdentifier>(); @Output() proportionChartClicked = new EventEmitter<AssetProfileIdentifier>();
@ViewChild('chartCanvas') chartCanvas: ElementRef<HTMLCanvasElement>;
public chart: Chart<'doughnut'>; public chart: Chart<'doughnut'>;
public isLoading = true; public isLoading = true;
private readonly chartCanvas =
viewChild.required<ElementRef<HTMLCanvasElement>>('chartCanvas');
private readonly OTHER_KEY = 'OTHER'; private readonly OTHER_KEY = 'OTHER';
private colorMap: { private colorMap: {
@ -336,76 +336,74 @@ export class GfPortfolioProportionChartComponent
labels labels
}; };
if (this.chartCanvas) { if (this.chart) {
if (this.chart) { this.chart.data = data;
this.chart.data = data; this.chart.options.plugins ??= {};
this.chart.options.plugins ??= {}; this.chart.options.plugins.tooltip =
this.chart.options.plugins.tooltip = this.getTooltipPluginConfiguration(data);
this.getTooltipPluginConfiguration(data);
this.chart.update(); this.chart.update();
} else { } else {
this.chart = new Chart<'doughnut'>(this.chartCanvas.nativeElement, { this.chart = new Chart<'doughnut'>(this.chartCanvas().nativeElement, {
data, data,
options: { options: {
animation: false, animation: false,
cutout: '70%', cutout: '70%',
layout: { layout: {
padding: this.showLabels === true ? 100 : 0 padding: this.showLabels === true ? 100 : 0
}, },
onClick: (_, activeElements, chart) => { onClick: (_, activeElements, chart) => {
try { try {
const dataIndex = activeElements[0].index; const dataIndex = activeElements[0].index;
const symbol = chart.data.labels?.[dataIndex] as string; const symbol = chart.data.labels?.[dataIndex] as string;
const dataSource = this.data[symbol].dataSource; const dataSource = this.data[symbol].dataSource;
if (dataSource) { if (dataSource) {
this.proportionChartClicked.emit({ dataSource, symbol }); this.proportionChartClicked.emit({ dataSource, symbol });
}
} catch {}
},
onHover: (event, chartElement) => {
if (this.cursor) {
(event.native?.target as HTMLElement).style.cursor =
chartElement[0] ? this.cursor : 'default';
} }
}, } catch {}
plugins: { },
datalabels: { onHover: (event, chartElement) => {
color: (context) => { if (this.cursor) {
return this.getColorPalette()[ (event.native?.target as HTMLElement).style.cursor =
context.dataIndex % this.getColorPalette().length chartElement[0] ? this.cursor : 'default';
];
},
display: this.showLabels === true ? 'auto' : false,
labels: {
index: {
align: 'end',
anchor: 'end',
formatter: (value, context) => {
const symbol = context.chart.data.labels?.[
context.dataIndex
] as string;
return value > 0
? isUUID(symbol)
? (translate(this.data[symbol]?.name) ?? symbol)
: symbol
: '';
},
offset: 8
}
}
},
legend: { display: false },
tooltip: this.getTooltipPluginConfiguration(data)
} }
}, },
plugins: [ChartDataLabels], plugins: {
type: 'doughnut' datalabels: {
}); color: (context) => {
} return this.getColorPalette()[
context.dataIndex % this.getColorPalette().length
];
},
display: this.showLabels === true ? 'auto' : false,
labels: {
index: {
align: 'end',
anchor: 'end',
formatter: (value, context) => {
const symbol = context.chart.data.labels?.[
context.dataIndex
] as string;
return value > 0
? isUUID(symbol)
? (translate(this.data[symbol]?.name) ?? symbol)
: symbol
: '';
},
offset: 8
}
}
},
legend: { display: false },
tooltip: this.getTooltipPluginConfiguration(data)
}
},
plugins: [ChartDataLabels],
type: 'doughnut'
});
} }
this.isLoading = false; this.isLoading = false;

Loading…
Cancel
Save