Skip to content

Commit 2d3f091

Browse files
fix(RadialChart): Handle width and height prop correctly (#52)
1 parent d705484 commit 2d3f091

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

packages/charts/src/components/DonutChart/index.tsx

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, { PureComponent } from 'react';
22
import { ChartInternalProps } from '../../interfaces/ChartInternalProps';
33
import { ChartBaseProps } from '../../interfaces/ChartBaseProps';
4-
import { mergeConfig } from '../../util/utils';
4+
import { populateData } from '../../util/populateData';
5+
import { formatTooltipLabelForPieCharts, mergeConfig } from '../../util/utils';
56
import { ChartBaseDefaultProps } from '../../util/ChartBaseDefaultProps';
6-
import { PieChart } from '../PieChart';
7+
import { Pie } from 'react-chartjs-2';
78
import { withChartContainer } from '../ChartContainer/withChartContainer';
89
import { PieChartPlaceholder } from '../PieChart/Placeholder';
910

@@ -20,15 +21,57 @@ export class DonutChart extends PureComponent<DonutChartPropTypes> {
2021
static LoadingPlaceholder = PieChartPlaceholder;
2122

2223
render() {
23-
const { options, ...otherProps } = this.props as DonutChartPropTypes & ChartInternalProps;
24+
const {
25+
labels,
26+
datasets,
27+
colors,
28+
theme,
29+
categoryAxisFormatter,
30+
getDatasetAtEvent,
31+
getElementAtEvent,
32+
valueAxisFormatter,
33+
options
34+
} = this.props as DonutChartPropTypes & ChartInternalProps;
35+
36+
const doughnut = populateData(labels, datasets, colors, theme.theme, true);
2437

2538
const mergedOptions = mergeConfig(
2639
{
27-
cutoutPercentage: 70
40+
cutoutPercentage: 70,
41+
tooltips: {
42+
callbacks: {
43+
label: formatTooltipLabelForPieCharts(categoryAxisFormatter, valueAxisFormatter)
44+
}
45+
},
46+
plugins: {
47+
datalabels: {
48+
anchor: 'center',
49+
align: 'center',
50+
offset: 0,
51+
color: (context) => {
52+
return parseInt(context.dataset.backgroundColor[context.datasetIndex], 16) > 0xffffff / 2
53+
? '#666'
54+
: '#fff';
55+
},
56+
formatter: valueAxisFormatter
57+
}
58+
}
2859
},
2960
options
3061
);
3162

32-
return <PieChart innerChartRef={this.props.innerChartRef} {...otherProps} options={mergedOptions} />;
63+
return (
64+
<Pie
65+
ref={this.props.innerChartRef}
66+
data={doughnut}
67+
height={this.props.height}
68+
width={this.props.width}
69+
options={mergedOptions}
70+
// @ts-ignore
71+
getDatasetAtEvent={getDatasetAtEvent}
72+
// @ts-ignore
73+
getElementAtEvent={getElementAtEvent}
74+
/>
75+
);
3376
}
3477
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import React from 'react';
21
import { storiesOf } from '@storybook/react';
2+
import { number, text } from '@storybook/addon-knobs';
3+
import React from 'react';
34
import { RadialChart } from './index';
45

56
function renderStory() {
6-
return <RadialChart value={40} displayValue="40%" />;
7+
return <RadialChart value={number('value', 40)} displayValue={text('displayValue', '40%')} />;
78
}
89

9-
storiesOf('Charts | RadialChart', module).add('Default', renderStory);
10+
const renderInContainer = () => (
11+
<div style={{ width: '100px', height: '100px', margin: '0 auto', position: 'relative' }}>
12+
<RadialChart value={number('value', 90)} displayValue={text('displayValue', '90%')} width={100} height={100} />
13+
</div>
14+
);
15+
16+
storiesOf('Charts | RadialChart', module)
17+
.add('Default', renderStory)
18+
.add('In Container', renderInContainer);

packages/charts/src/components/RadialChart/index.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface RadialChartPropTypes extends CommonProps {
1818
innerChartRef?: Ref<any>;
1919
}
2020

21-
const styles = () => ({
21+
const styles = {
2222
radialChart: {
2323
position: 'relative'
2424
},
@@ -34,7 +34,7 @@ const styles = () => ({
3434
pointerEvents: 'none',
3535
fontSize: '2rem'
3636
}
37-
});
37+
};
3838

3939
@withStyles(styles)
4040
export class RadialChart extends PureComponent<RadialChartPropTypes> {
@@ -46,7 +46,7 @@ export class RadialChart extends PureComponent<RadialChartPropTypes> {
4646
};
4747

4848
render() {
49-
const { maxValue, value, displayValue, classes, style, className, colors, options } = this
49+
const { maxValue, value, displayValue, classes, style, className, colors, options, width, height } = this
5050
.props as RadialChartPropTypes & ChartInternalProps;
5151

5252
const data = [value, maxValue - value];
@@ -66,23 +66,29 @@ export class RadialChart extends PureComponent<RadialChartPropTypes> {
6666
options
6767
);
6868

69+
const radialChartContainerStyles = {
70+
width: `${width}px`,
71+
height: `${height}px`,
72+
...style
73+
};
74+
6975
const outerClasses = StyleClassHelper.of(classes.radialChart);
7076
if (className) {
7177
outerClasses.put(className);
7278
}
7379

7480
return (
75-
<div className={outerClasses.toString()} style={style}>
81+
<div className={outerClasses.toString()} style={radialChartContainerStyles}>
7682
<DonutChart
7783
innerChartRef={this.props.innerChartRef}
7884
datasets={[{ data }]}
7985
colors={colors}
8086
options={mergedOptions}
87+
width={width}
88+
height={height}
89+
style={{ paddingTop: 0 }}
8190
/>
82-
<div
83-
className={classes.content}
84-
style={{ maxWidth: `${this.props.width}px`, maxHeight: `${this.props.height}px` }}
85-
>
91+
<div className={classes.content}>
8692
<h3
8793
style={{
8894
color: '#333333',

0 commit comments

Comments
 (0)