From a70d3c7d270057a1a484d6a4f25d1b0b01ee384b Mon Sep 17 00:00:00 2001 From: Glib Briia Date: Tue, 3 Nov 2020 07:58:26 +0000 Subject: [PATCH 1/2] Add colors to pie chart --- src/ProgressChart.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ProgressChart.tsx b/src/ProgressChart.tsx index ed512830..ee58400f 100644 --- a/src/ProgressChart.tsx +++ b/src/ProgressChart.tsx @@ -10,7 +10,7 @@ import AbstractChart, { export type ProgressChartData = | Array - | { labels?: Array; data: Array }; + | { labels?: Array; colors?:Array; data: Array; }; export interface ProgressChartProps extends AbstractChartProps { data: ProgressChartData; @@ -93,6 +93,9 @@ class ProgressChart extends AbstractChart< const withLabel = (i: number) => (data as any).labels && (data as any).labels[i]; + + const withColor = (i: number) => + (data as any).colors && (data as any).colors[i]; const legend = !hideLegend && ( <> @@ -192,10 +195,7 @@ class ProgressChart extends AbstractChart< strokeLinejoin="round" d={pie.curves[0].sector.path.print()} strokeWidth={strokeWidth} - stroke={this.props.chartConfig.color( - (i / pies.length) * 0.5 + 0.5, - i - )} + stroke={withColor(i)} /> ); })} From 3a8b64b21017840115a10975f8789a7f87022f1f Mon Sep 17 00:00:00 2001 From: Glib Briia Date: Tue, 3 Nov 2020 08:45:40 +0000 Subject: [PATCH 2/2] Conditionally use colors from data --- src/ProgressChart.tsx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ProgressChart.tsx b/src/ProgressChart.tsx index ee58400f..d1b94e57 100644 --- a/src/ProgressChart.tsx +++ b/src/ProgressChart.tsx @@ -10,7 +10,7 @@ import AbstractChart, { export type ProgressChartData = | Array - | { labels?: Array; colors?:Array; data: Array; }; + | { labels?: Array; colors?: Array; data: Array }; export interface ProgressChartProps extends AbstractChartProps { data: ProgressChartData; @@ -27,6 +27,7 @@ export interface ProgressChartProps extends AbstractChartProps { hideLegend?: boolean; strokeWidth?: number; radius?: number; + withCustomBarColorFromData?: boolean; } type ProgressChartState = {}; @@ -93,7 +94,7 @@ class ProgressChart extends AbstractChart< const withLabel = (i: number) => (data as any).labels && (data as any).labels[i]; - + const withColor = (i: number) => (data as any).colors && (data as any).colors[i]; @@ -173,7 +174,10 @@ class ProgressChart extends AbstractChart< ry={borderRadius} fill="url(#backgroundGradient)" /> - + {pieBackgrounds.map((pie, i) => { return ( @@ -195,7 +199,14 @@ class ProgressChart extends AbstractChart< strokeLinejoin="round" d={pie.curves[0].sector.path.print()} strokeWidth={strokeWidth} - stroke={withColor(i)} + stroke={ + this.props.withCustomBarColorFromData + ? withColor(i) + : this.props.chartConfig.color( + (i / pies.length) * 0.5 + 0.5, + i + ) + } /> ); })}