Skip to content

Commit 9444df6

Browse files
Minor
1 parent d53a0d3 commit 9444df6

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

js/components/bar.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
define([
22
'react',
33
'animate',
4-
'paths/bar'],
5-
function(React, Animate, Bar){
4+
'palette/colors',
5+
'paths/bar'
6+
], function(React, Animate, Colors, Bar){
67
return React.createClass({
78

89
mixins: [Animate.Mixin],
@@ -36,8 +37,15 @@ define([
3637
width: 420,
3738
height: 350,
3839
gutter: 10,
39-
palette: ["LightCoral", "NavajoWhite", "LemonChiffon",
40-
"PaleGreen", "CornflowerBlue", "Thistle", "Lavender"]
40+
palette: Colors.mix({
41+
r: 130,
42+
g: 140,
43+
b: 210
44+
}, {
45+
r: 180,
46+
g: 205,
47+
b: 150
48+
})
4149
};
4250
},
4351

@@ -51,16 +59,16 @@ define([
5159
height: this.props.height,
5260
accessor: this.props.accessor,
5361
compute: {
54-
color: function(i){
55-
return self.props.palette[i % self.props.palette.length];
62+
color: function(i) {
63+
return Colors.string(self.props.palette[i % self.props.palette.length]);
5664
}
5765
},
5866
gutter: this.props.gutter,
5967
});
6068

6169
var curves = bar.curves.map(function(curve){
6270
return <path d={curve.line.path.print()} fill={curve.color}
63-
onMouseOver={function(event){self.handleMouseOver(curve.index);}}
71+
onMouseOver={function(event){self.handleMouseOver(curve.index)}}
6472
onMouseLeave={self.handleMouseLeave}/>;
6573
});
6674

js/components/stack.jsx

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,85 @@
11
define([
22
'react',
3+
'animate',
4+
'palette/colors',
35
'paths/stack'
4-
], function(React, Stack) {
6+
], function(React, Animate, Colors, Stack) {
57
return React.createClass({
68

7-
cardinal: function(num) {
8-
if(num == 1)
9-
return "st";
10-
else if(num == 2)
11-
return "nd";
12-
else if(num == 3)
13-
return "rd";
14-
else
15-
return "th";
16-
},
9+
mixins: [Animate.Mixin],
1710

1811
cyclic: function(array, i) {
1912
return array[i % array.length];
2013
},
2114

22-
handleMouseOver: function(barX, curves) {
23-
var ifCentroidX = function(curve){
24-
return curve.line.centroid[0] === barX;
25-
};
26-
var toTextInfo = function(curve){
27-
return {
28-
x: barX,
29-
y: curve.line.centroid[1],
30-
value: curve.item,
31-
index: curve.index
32-
}
33-
};
34-
this.setState({text: curves.filter(ifCentroidX).map(toTextInfo)});
15+
scaledByIndex: function(index, scale, matrix) {
16+
if (index===undefined)
17+
return matrix;
18+
var scale = scale || 0;
19+
var matrix = matrix || this.props.data;
20+
return matrix.map(function(array, i){
21+
return array.map(function(value){
22+
return (i==index) ? value : value*scale;
23+
});
24+
});
25+
},
26+
27+
handleMouseOver: function(index){
28+
this.animateState({data: this.scaledByIndex(index, 0.1)});
29+
},
30+
31+
handleMouseLeave: function(){
32+
this.animateState({data: this.props.data});
3533
},
3634

37-
handleMouseLeave: function(event) {
38-
this.setState({text: []});
35+
getInitialState: function(){
36+
return {data: this.props.data};
3937
},
4038

4139
getDefaultProps: function() {
4240
return {
4341
width: 420,
4442
height: 350,
45-
palette: ["LightCoral", "NavajoWhite", "LemonChiffon",
46-
"PaleGreen", "CornflowerBlue", "Thistle", "Lavender"]
43+
palette: Colors.mix({
44+
r: 130,
45+
g: 140,
46+
b: 210
47+
}, {
48+
r: 180,
49+
g: 205,
50+
b: 150
51+
})
4752
};
4853
},
4954

50-
getInitialState: function() {
51-
return {text: []};
52-
},
53-
5455
render: function() {
5556

5657
var self = this;
5758

5859
var stack = Stack({
59-
data: this.props.data,
60+
data: this.state.data,
6061
accessor: this.props.accessor,
6162
width: this.props.width,
6263
height:this.props.height,
6364
gutter: this.props.gutter,
6465
compute: {
6566
color: function(index, item, group){
66-
return self.cyclic(self.props.palette, group);
67+
return Colors.string(self.cyclic(self.props.palette, group));
6768
}
6869
}
6970
});
7071

7172
var curves = stack.curves.map(function(curve){
7273
return <path d={curve.line.path.print()} fill={curve.color}
73-
onMouseOver={function(event){
74-
self.handleMouseOver(curve.line.centroid[0], stack.curves)}
75-
}
74+
onMouseOver={function(event){self.handleMouseOver(curve.group)}}
7675
onMouseLeave={self.handleMouseLeave}/>;
7776
});
7877

79-
var text = this.state.text.map(function(textInfo){
80-
//sembra che React non supporti l'attributo alignmentBaseline
81-
return <text x={textInfo.x} y={textInfo.y} alignmentBaseline="middle"
82-
fontFamily="Serif" textAnchor="middle"
83-
fontSize={self.props.fontSize || self.props.height / 20}>
84-
{ (textInfo.index+1) + self.cardinal(textInfo.index+1) +
85-
" serie: " + textInfo.value }</text>;
86-
});
87-
8878
return (
8979
<div id="stack">
9080
<svg width="500" height="380">
9181
<g transform="translate(40, 10)">
9282
{curves}
93-
{text}
9483
</g>
9584
</svg>
9685
</div>

0 commit comments

Comments
 (0)