/
bar-label.jsx
103 lines (95 loc) · 3.24 KB
/
bar-label.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import defaults from "lodash/defaults";
import React, { PropTypes } from "react";
import { VictoryLabel, Helpers } from "victory-core";
export default class BarLabel extends React.Component {
static propTypes = {
events: PropTypes.object,
position: PropTypes.object,
horizontal: PropTypes.bool,
style: PropTypes.object,
datum: PropTypes.object,
index: PropTypes.number,
labelText: PropTypes.string,
labelComponent: PropTypes.any
};
getLabelAnchors(props) {
const sign = props.datum.y >= 0 ? 1 : -1;
if (!props.horizontal) {
return {
vertical: sign >= 0 ? "end" : "start",
text: "middle"
};
} else {
return {
vertical: "middle",
text: sign >= 0 ? "start" : "end"
};
}
}
getlabelPadding(props, style) {
return {
x: props.horizontal ? style.padding : 0,
y: props.horizontal ? 0 : style.padding
};
}
renderLabelComponent(props, position, anchors) {
const component = props.labelComponent;
const baseStyle = defaults({}, component.props.style, props.style, {padding: 0});
const style = Helpers.evaluateStyle(baseStyle, props.datum);
const padding = this.getlabelPadding(props, style);
const labelText = props.labelText || props.datum.label;
const index = [props.index.seriesIndex, props.index.barIndex];
const baseEvents = component && component.props.events ?
defaults({}, component.props.events, props.events) : props.events;
const events = Helpers.getPartialEvents(baseEvents, index, props);
const newProps = {
index: [props.index.seriesIndex, props.index.barIndex],
x: component.props.x || position.x + padding.x,
y: component.props.y || position.y - padding.y,
datum: props.datum, // Pass datum for custom label component to access
text: labelText,
textAnchor: component.props.textAnchor || anchors.text,
verticalAnchor: component.props.verticalAnchor || anchors.vertical,
style,
events
};
return React.cloneElement(component, newProps);
}
renderVictoryLabel(props, position, anchors) {
const baseStyle = defaults({}, props.style, {padding: 0});
const style = Helpers.evaluateStyle(baseStyle, props.datum);
const padding = this.getlabelPadding(props, style);
const index = [props.index.seriesIndex, props.index.barIndex];
const events = Helpers.getPartialEvents(props.events, index, props);
return (
<VictoryLabel
x={position.x + padding.x}
y={position.y - padding.y}
datum={props.datum}
index={[props.index.seriesIndex, props.index.barIndex]}
textAnchor={anchors.text}
verticalAnchor={anchors.vertical}
style={style}
text={props.labelText}
events={events}
/>
);
}
renderLabel(props) {
const anchors = this.getLabelAnchors(props);
const position = {
x: props.horizontal ? props.position.y1 : props.position.x,
y: props.horizontal ? props.position.x : props.position.y1
};
return props.labelComponent ?
this.renderLabelComponent(props, position, anchors) :
this.renderVictoryLabel(props, position, anchors);
}
render() {
return (
<g>
{this.renderLabel(this.props)}
</g>
);
}
}