Skip to content

Commit 6d8ec0e

Browse files
feat: add option to toggle value labels on bar charts (#182)
1 parent 8133ab1 commit 6d8ec0e

File tree

16 files changed

+1071
-33
lines changed

16 files changed

+1071
-33
lines changed

src/components/react_canvas/bar_geometries.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface BarGeometriesDataState {
2020
export class BarGeometries extends React.PureComponent<
2121
BarGeometriesDataProps,
2222
BarGeometriesDataState
23-
> {
23+
> {
2424
static defaultProps: Partial<BarGeometriesDataProps> = {
2525
animated: false,
2626
};
@@ -101,7 +101,11 @@ export class BarGeometries extends React.PureComponent<
101101
borderEnabled: border.visible,
102102
geometryStyle,
103103
});
104-
return <Rect {...barProps} />;
104+
return (
105+
<React.Fragment key={index}>
106+
<Rect {...barProps} />
107+
</React.Fragment>
108+
);
105109
}
106110
});
107111
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from 'react';
2+
import { Group, Rect, Text } from 'react-konva';
3+
import { BarGeometry } from '../../lib/series/rendering';
4+
import { Rotation } from '../../lib/series/specs';
5+
import { DisplayValueStyle } from '../../lib/themes/theme';
6+
import { Dimensions } from '../../lib/utils/dimensions';
7+
import { buildBarValueProps } from './utils/rendering_props_utils';
8+
9+
interface BarValuesProps {
10+
chartDimensions: Dimensions;
11+
chartRotation: Rotation;
12+
debug: boolean;
13+
bars: BarGeometry[];
14+
displayValueStyle: DisplayValueStyle;
15+
}
16+
17+
export class BarValues extends React.PureComponent<BarValuesProps> {
18+
render() {
19+
const { chartDimensions } = this.props;
20+
21+
return (
22+
<Group x={chartDimensions.left} y={chartDimensions.top}>
23+
{this.renderBarValues()}
24+
</Group>
25+
);
26+
}
27+
28+
private renderBarValues = () => {
29+
const { bars, displayValueStyle, debug, chartRotation, chartDimensions } = this.props;
30+
return bars.map((bar, index) => {
31+
const { displayValue, x, y, height, width } = bar;
32+
if (!displayValue) {
33+
return;
34+
}
35+
36+
const key = `bar-value-${index}`;
37+
const displayValueProps = buildBarValueProps({
38+
x,
39+
y,
40+
barHeight: height,
41+
barWidth: width,
42+
displayValueStyle,
43+
displayValue,
44+
chartRotation,
45+
chartDimensions,
46+
});
47+
48+
const debugProps = {
49+
...displayValueProps,
50+
stroke: 'violet',
51+
strokeWidth: 1,
52+
fill: 'transparent',
53+
};
54+
55+
return (
56+
<Group key={key}>
57+
{debug && <Rect {...debugProps} />}
58+
{displayValue && <Text {...displayValueProps} />}
59+
</Group>
60+
);
61+
});
62+
}
63+
}

src/components/react_canvas/reactive_chart.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Annotation } from './annotation';
1010
import { AreaGeometries } from './area_geometries';
1111
import { Axis } from './axis';
1212
import { BarGeometries } from './bar_geometries';
13+
import { BarValues } from './bar_values';
1314
import { Grid } from './grid';
1415
import { LineGeometries } from './line_geometries';
1516

@@ -198,6 +199,22 @@ class Chart extends React.Component<ReactiveChartProps, ReactiveChartState> {
198199
return annotationComponents;
199200
}
200201

202+
renderBarValues = () => {
203+
const { debug, chartDimensions, geometries, chartTheme, chartRotation } = this.props.chartStore!;
204+
if (!geometries) {
205+
return;
206+
}
207+
const props = {
208+
debug,
209+
chartDimensions,
210+
chartRotation,
211+
bars: geometries.bars,
212+
// displayValue is guaranteed on style as part of the merged theme
213+
displayValueStyle: chartTheme.barSeriesStyle.displayValue!,
214+
};
215+
return <BarValues {...props} />;
216+
}
217+
201218
renderBrushTool = () => {
202219
const { brushing, brushStart, brushEnd } = this.state;
203220
const { chartDimensions, chartRotation, chartTransform } = this.props.chartStore!;
@@ -360,19 +377,23 @@ class Chart extends React.Component<ReactiveChartProps, ReactiveChartState> {
360377
<Layer hitGraphEnabled={false} listening={false}>
361378
{this.renderAnnotations()}
362379
</Layer>
380+
381+
<Layer hitGraphEnabled={false} listening={false} {...layerClippings}>
382+
{this.renderBarValues()}
383+
</Layer>
363384
</Stage>
364385
</div>
365386
);
366387
}
367388

368389
private renderDebugChartBorders = () => {
369-
const { chartDimensions, chartRotation, chartTransform } = this.props.chartStore!;
390+
const { chartDimensions } = this.props.chartStore!;
370391
return (
371392
<Rect
372-
x={chartDimensions.left + chartTransform.x}
373-
y={chartDimensions.top + chartTransform.y}
374-
width={[90, -90].includes(chartRotation) ? chartDimensions.height : chartDimensions.width}
375-
height={[90, -90].includes(chartRotation) ? chartDimensions.width : chartDimensions.height}
393+
x={chartDimensions.left}
394+
y={chartDimensions.top}
395+
width={chartDimensions.width}
396+
height={chartDimensions.height}
376397
stroke="red"
377398
strokeWidth={4}
378399
listening={false}

0 commit comments

Comments
 (0)