Add labels to allocation piecharts (#337)
This commit is contained in:
parent
b0b0942162
commit
9c9ca4ab1e
@ -16,6 +16,8 @@ import { LinearScale } from 'chart.js';
|
|||||||
import { ArcElement } from 'chart.js';
|
import { ArcElement } from 'chart.js';
|
||||||
import { DoughnutController } from 'chart.js';
|
import { DoughnutController } from 'chart.js';
|
||||||
import { Chart } from 'chart.js';
|
import { Chart } from 'chart.js';
|
||||||
|
import ChartDataLabels from 'chartjs-plugin-datalabels';
|
||||||
|
import { sum } from 'lodash';
|
||||||
import * as Color from 'color';
|
import * as Color from 'color';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -32,6 +34,7 @@ export class PortfolioProportionChartComponent
|
|||||||
@Input() keys: string[];
|
@Input() keys: string[];
|
||||||
@Input() locale: string;
|
@Input() locale: string;
|
||||||
@Input() maxItems?: number;
|
@Input() maxItems?: number;
|
||||||
|
@Input() showLabels: boolean;
|
||||||
@Input() positions: {
|
@Input() positions: {
|
||||||
[symbol: string]: Pick<PortfolioPosition, 'type'> & { value: number };
|
[symbol: string]: Pick<PortfolioPosition, 'type'> & { value: number };
|
||||||
};
|
};
|
||||||
@ -48,7 +51,7 @@ export class PortfolioProportionChartComponent
|
|||||||
};
|
};
|
||||||
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
Chart.register(ArcElement, DoughnutController, LinearScale, Tooltip);
|
Chart.register(ArcElement, DoughnutController, LinearScale, Tooltip, ChartDataLabels);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ngOnInit() {}
|
public ngOnInit() {}
|
||||||
@ -226,6 +229,8 @@ export class PortfolioProportionChartComponent
|
|||||||
labels
|
labels
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const showLabels = this.showLabels || false;
|
||||||
|
|
||||||
if (this.chartCanvas) {
|
if (this.chartCanvas) {
|
||||||
if (this.chart) {
|
if (this.chart) {
|
||||||
this.chart.data = data;
|
this.chart.data = data;
|
||||||
@ -234,6 +239,9 @@ export class PortfolioProportionChartComponent
|
|||||||
this.chart = new Chart(this.chartCanvas.nativeElement, {
|
this.chart = new Chart(this.chartCanvas.nativeElement, {
|
||||||
data,
|
data,
|
||||||
options: {
|
options: {
|
||||||
|
layout: {
|
||||||
|
padding: 60,
|
||||||
|
},
|
||||||
cutout: '70%',
|
cutout: '70%',
|
||||||
plugins: {
|
plugins: {
|
||||||
legend: { display: false },
|
legend: { display: false },
|
||||||
@ -257,6 +265,27 @@ export class PortfolioProportionChartComponent
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
datalabels: {
|
||||||
|
display: function(ctx) {
|
||||||
|
const value = ctx.dataset.data[ctx.dataIndex];
|
||||||
|
return showLabels === true && ctx.datasetIndex === 0 && value > sum(ctx.dataset.data) / 10;
|
||||||
|
},
|
||||||
|
font: {
|
||||||
|
weight: 'bold',
|
||||||
|
},
|
||||||
|
color: this.getColorPalette()[0],
|
||||||
|
labels: {
|
||||||
|
index: {
|
||||||
|
align: 'end',
|
||||||
|
anchor: 'end',
|
||||||
|
font: {size: 18},
|
||||||
|
formatter: function(value, ctx) {
|
||||||
|
return ctx.chart.data.labels[ctx.dataIndex];
|
||||||
|
},
|
||||||
|
offset: 8,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -68,6 +68,7 @@
|
|||||||
<gf-portfolio-proportion-chart
|
<gf-portfolio-proportion-chart
|
||||||
[baseCurrency]="user?.settings?.baseCurrency"
|
[baseCurrency]="user?.settings?.baseCurrency"
|
||||||
[isInPercent]="true"
|
[isInPercent]="true"
|
||||||
|
[showLabels]="true"
|
||||||
[keys]="['currency']"
|
[keys]="['currency']"
|
||||||
[locale]="user?.settings?.locale"
|
[locale]="user?.settings?.locale"
|
||||||
[positions]="positions"
|
[positions]="positions"
|
||||||
@ -91,6 +92,7 @@
|
|||||||
class="mx-auto"
|
class="mx-auto"
|
||||||
[baseCurrency]="user?.settings?.baseCurrency"
|
[baseCurrency]="user?.settings?.baseCurrency"
|
||||||
[isInPercent]="false"
|
[isInPercent]="false"
|
||||||
|
[showLabels]="true"
|
||||||
[keys]="['name']"
|
[keys]="['name']"
|
||||||
[locale]="user?.settings?.locale"
|
[locale]="user?.settings?.locale"
|
||||||
[positions]="symbols"
|
[positions]="symbols"
|
||||||
|
@ -81,6 +81,7 @@
|
|||||||
"cache-manager-redis-store": "2.0.0",
|
"cache-manager-redis-store": "2.0.0",
|
||||||
"chart.js": "3.5.0",
|
"chart.js": "3.5.0",
|
||||||
"chartjs-adapter-date-fns": "2.0.0",
|
"chartjs-adapter-date-fns": "2.0.0",
|
||||||
|
"chartjs-plugin-datalabels": "^2.0.0",
|
||||||
"cheerio": "1.0.0-rc.6",
|
"cheerio": "1.0.0-rc.6",
|
||||||
"class-transformer": "0.3.2",
|
"class-transformer": "0.3.2",
|
||||||
"class-validator": "0.13.1",
|
"class-validator": "0.13.1",
|
||||||
|
@ -6112,6 +6112,11 @@ chartjs-adapter-date-fns@2.0.0:
|
|||||||
resolved "https://registry.yarnpkg.com/chartjs-adapter-date-fns/-/chartjs-adapter-date-fns-2.0.0.tgz#5e53b2f660b993698f936f509c86dddf9ed44c6b"
|
resolved "https://registry.yarnpkg.com/chartjs-adapter-date-fns/-/chartjs-adapter-date-fns-2.0.0.tgz#5e53b2f660b993698f936f509c86dddf9ed44c6b"
|
||||||
integrity sha512-rmZINGLe+9IiiEB0kb57vH3UugAtYw33anRiw5kS2Tu87agpetDDoouquycWc9pRsKtQo5j+vLsYHyr8etAvFw==
|
integrity sha512-rmZINGLe+9IiiEB0kb57vH3UugAtYw33anRiw5kS2Tu87agpetDDoouquycWc9pRsKtQo5j+vLsYHyr8etAvFw==
|
||||||
|
|
||||||
|
chartjs-plugin-datalabels@^2.0.0:
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/chartjs-plugin-datalabels/-/chartjs-plugin-datalabels-2.0.0.tgz#caacefb26803d968785071eab012dde8746c5939"
|
||||||
|
integrity sha512-WBsWihphzM0Y8fmQVm89+iy99mmgejmj5/jcsYqwxSioLRL/zqJ4Scv/eXq5ZqvG3TpojlGzZLeaOaSvDm7fwA==
|
||||||
|
|
||||||
check-more-types@^2.24.0:
|
check-more-types@^2.24.0:
|
||||||
version "2.24.0"
|
version "2.24.0"
|
||||||
resolved "https://registry.yarnpkg.com/check-more-types/-/check-more-types-2.24.0.tgz#1420ffb10fd444dcfc79b43891bbfffd32a84600"
|
resolved "https://registry.yarnpkg.com/check-more-types/-/check-more-types-2.24.0.tgz#1420ffb10fd444dcfc79b43891bbfffd32a84600"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user