Improve proportion charts (#83)
* Improve proportion charts * Clean up code
This commit is contained in:
parent
04cfa7366f
commit
45516311f5
@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Harmonized the style of various tables
|
- Harmonized the style of various tables
|
||||||
|
- Keep the color per type when switching between _Initial_ and _Current_ in pie charts
|
||||||
|
- Upgraded `chart.js` from version `3.0.2` to `3.2.1`
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Fixed the tooltips when switching between _Initial_ and _Current_ in pie charts
|
||||||
|
|
||||||
## 1.1.0 - 11.05.2021
|
## 1.1.0 - 11.05.2021
|
||||||
|
|
||||||
|
@ -36,6 +36,10 @@ export class PortfolioProportionChartComponent
|
|||||||
public chart: Chart;
|
public chart: Chart;
|
||||||
public isLoading = true;
|
public isLoading = true;
|
||||||
|
|
||||||
|
private colorMap: {
|
||||||
|
[symbol: string]: string;
|
||||||
|
} = {};
|
||||||
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
Chart.register(ArcElement, DoughnutController, LinearScale, Tooltip);
|
Chart.register(ArcElement, DoughnutController, LinearScale, Tooltip);
|
||||||
}
|
}
|
||||||
@ -48,37 +52,67 @@ export class PortfolioProportionChartComponent
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ngOnDestroy() {
|
||||||
|
this.chart?.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
private initialize() {
|
private initialize() {
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
const chartData: { [symbol: string]: number } = {};
|
const chartData: {
|
||||||
|
[symbol: string]: { color?: string; value: number };
|
||||||
|
} = {};
|
||||||
|
|
||||||
Object.keys(this.positions).forEach((symbol) => {
|
Object.keys(this.positions).forEach((symbol) => {
|
||||||
if (this.positions[symbol][this.key]) {
|
if (this.positions[symbol][this.key]) {
|
||||||
if (chartData[this.positions[symbol][this.key]]) {
|
if (chartData[this.positions[symbol][this.key]]) {
|
||||||
chartData[this.positions[symbol][this.key]] += this.positions[
|
chartData[this.positions[symbol][this.key]].value += this.positions[
|
||||||
symbol
|
symbol
|
||||||
].value;
|
].value;
|
||||||
} else {
|
} else {
|
||||||
chartData[this.positions[symbol][this.key]] = this.positions[
|
chartData[this.positions[symbol][this.key]] = {
|
||||||
symbol
|
value: this.positions[symbol].value
|
||||||
].value;
|
};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (chartData['Other']) {
|
||||||
|
chartData['Other'].value += this.positions[symbol].value;
|
||||||
|
} else {
|
||||||
|
chartData['Other'] = {
|
||||||
|
value: this.positions[symbol].value
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const chartDataSorted = Object.entries(chartData)
|
const chartDataSorted = Object.entries(chartData)
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
return a[1] - b[1];
|
return a[1].value - b[1].value;
|
||||||
})
|
})
|
||||||
.reverse();
|
.reverse();
|
||||||
|
|
||||||
|
chartDataSorted.forEach(([symbol, item], index) => {
|
||||||
|
if (this.colorMap[symbol]) {
|
||||||
|
// Reuse color
|
||||||
|
item.color = this.colorMap[symbol];
|
||||||
|
} else {
|
||||||
|
const color = this.getColorPalette()[index];
|
||||||
|
|
||||||
|
// Store color for reuse
|
||||||
|
this.colorMap[symbol] = color;
|
||||||
|
|
||||||
|
item.color = color;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
backgroundColor: this.getColorPalette(),
|
backgroundColor: chartDataSorted.map(([, item]) => {
|
||||||
|
return item.color;
|
||||||
|
}),
|
||||||
borderWidth: 0,
|
borderWidth: 0,
|
||||||
data: chartDataSorted.map(([, value]) => {
|
data: chartDataSorted.map(([, item]) => {
|
||||||
return value;
|
return item.value;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -100,21 +134,13 @@ export class PortfolioProportionChartComponent
|
|||||||
tooltip: {
|
tooltip: {
|
||||||
callbacks: {
|
callbacks: {
|
||||||
label: (context) => {
|
label: (context) => {
|
||||||
const label = data.labels[context.dataIndex];
|
const label = context.label;
|
||||||
|
|
||||||
if (this.isInPercent) {
|
if (this.isInPercent) {
|
||||||
const value =
|
const value = 100 * <number>context.raw;
|
||||||
100 *
|
|
||||||
data.datasets[context.datasetIndex].data[
|
|
||||||
context.dataIndex
|
|
||||||
];
|
|
||||||
return `${label} (${value.toFixed(2)}%)`;
|
return `${label} (${value.toFixed(2)}%)`;
|
||||||
} else {
|
} else {
|
||||||
const value =
|
const value = <number>context.raw;
|
||||||
data.datasets[context.datasetIndex].data[
|
|
||||||
context.dataIndex
|
|
||||||
];
|
|
||||||
|
|
||||||
return `${label} (${value.toLocaleString(this.locale, {
|
return `${label} (${value.toLocaleString(this.locale, {
|
||||||
maximumFractionDigits: 2,
|
maximumFractionDigits: 2,
|
||||||
minimumFractionDigits: 2
|
minimumFractionDigits: 2
|
||||||
@ -153,8 +179,4 @@ export class PortfolioProportionChartComponent
|
|||||||
'#cc5de8' // grape 5
|
'#cc5de8' // grape 5
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ngOnDestroy() {
|
|
||||||
this.chart?.destroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@
|
|||||||
"bootstrap": "4.6.0",
|
"bootstrap": "4.6.0",
|
||||||
"cache-manager": "3.4.3",
|
"cache-manager": "3.4.3",
|
||||||
"cache-manager-redis-store": "2.0.0",
|
"cache-manager-redis-store": "2.0.0",
|
||||||
"chart.js": "3.0.2",
|
"chart.js": "3.2.1",
|
||||||
"chartjs-adapter-date-fns": "1.1.0-beta.1",
|
"chartjs-adapter-date-fns": "1.1.0-beta.1",
|
||||||
"chartjs-chart-timeline": "0.4.0",
|
"chartjs-chart-timeline": "0.4.0",
|
||||||
"cheerio": "1.0.0-rc.6",
|
"cheerio": "1.0.0-rc.6",
|
||||||
|
@ -4066,10 +4066,10 @@ chardet@^0.7.0:
|
|||||||
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
|
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
|
||||||
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
|
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
|
||||||
|
|
||||||
chart.js@3.0.2:
|
chart.js@3.2.1:
|
||||||
version "3.0.2"
|
version "3.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-3.0.2.tgz#5893b0548714445b5190cbda9ac307357a56a0c7"
|
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-3.2.1.tgz#1a17d6a88cef324ef711949e227eb51d6c4c26d3"
|
||||||
integrity sha512-DR0GmFSlxcFJp/w//ZmbxSduAkH/AqwxoiZxK97KHnWZf6gvsKWS3160WvNMMHYvzW9OXqGWjPjVh1Qu+xDabg==
|
integrity sha512-XsNDf3854RGZkLCt+5vWAXGAtUdKP2nhfikLGZqud6G4CvRE2ts64TIxTTfspOin2kEZvPgomE29E6oU02dYjQ==
|
||||||
|
|
||||||
chartjs-adapter-date-fns@1.1.0-beta.1:
|
chartjs-adapter-date-fns@1.1.0-beta.1:
|
||||||
version "1.1.0-beta.1"
|
version "1.1.0-beta.1"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user