-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.react.js
More file actions
80 lines (69 loc) · 1.72 KB
/
index.react.js
File metadata and controls
80 lines (69 loc) · 1.72 KB
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
import { Component } from 'React';
function fromProps(layout, props, avialable) {
for (let key in avialable) {
if (key in props && key in layout && layout[key]() !== props[key]) {
layout[key](props[key]);
}
}
return layout;
}
const PROPERTIES = ['values', 'offset', 'order', 'x', 'y', 'out'];
const StackedLayout = (ComposedComponet) => class extends Component {
constructor(props) {
super(props);
this.state = {stack: fromProps(d3.layout.stack(), props, PROPERTIES)};
}
componentWillReceiveNewProps(props) {
this.setState({stack: fromProps(this.state.stack, props, PROPERTIES)});
}
render() {
return (
<ComposedComponet
{...this.props}
data={this.state.stack(this.props.data)}
/>
);
}
};
const MarginProperties = (ComposedComponet) => class extends Component {
render() {
return <ComposedComponet {...this.props} />;
}
};
class Axis extends Component {
render() {
return (
<g className="axis x" ref="axis" />
);
}
}
class StackedBar extends Component {
render() {
const {xScale, yScale, colorScale} = this.props;
const layers = this.props.data.map((layer, i) => {
const rects = layer.map(({x, y, y0}, j) => (
<rect
key={j}
x={xScale(x)}
y={yScale(y + y0)}
height={yScale(y) - yScale(y + y0)}
width={xScale.rangeBand() - 1}
fill={colorScale(i)}
/>
));
return (
<g key={i} className="layer">
{rects}
</g>
);
});
return (
<svg>
{layers}
<Axis {...this.props} type="x" />
<Axis {...this.props} type="y" />
</svg>
);
}
}
MarginProperties(StackedLayout(StackedBar));