|
1 | 1 | define([ |
2 | 2 | 'react', |
| 3 | + 'animate', |
| 4 | + 'palette/colors', |
3 | 5 | 'paths/stack' |
4 | | -], function(React, Stack) { |
| 6 | +], function(React, Animate, Colors, Stack) { |
5 | 7 | return React.createClass({ |
6 | 8 |
|
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], |
17 | 10 |
|
18 | 11 | cyclic: function(array, i) { |
19 | 12 | return array[i % array.length]; |
20 | 13 | }, |
21 | 14 |
|
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}); |
35 | 33 | }, |
36 | 34 |
|
37 | | - handleMouseLeave: function(event) { |
38 | | - this.setState({text: []}); |
| 35 | + getInitialState: function(){ |
| 36 | + return {data: this.props.data}; |
39 | 37 | }, |
40 | 38 |
|
41 | 39 | getDefaultProps: function() { |
42 | 40 | return { |
43 | 41 | width: 420, |
44 | 42 | 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 | + }) |
47 | 52 | }; |
48 | 53 | }, |
49 | 54 |
|
50 | | - getInitialState: function() { |
51 | | - return {text: []}; |
52 | | - }, |
53 | | - |
54 | 55 | render: function() { |
55 | 56 |
|
56 | 57 | var self = this; |
57 | 58 |
|
58 | 59 | var stack = Stack({ |
59 | | - data: this.props.data, |
| 60 | + data: this.state.data, |
60 | 61 | accessor: this.props.accessor, |
61 | 62 | width: this.props.width, |
62 | 63 | height:this.props.height, |
63 | 64 | gutter: this.props.gutter, |
64 | 65 | compute: { |
65 | 66 | color: function(index, item, group){ |
66 | | - return self.cyclic(self.props.palette, group); |
| 67 | + return Colors.string(self.cyclic(self.props.palette, group)); |
67 | 68 | } |
68 | 69 | } |
69 | 70 | }); |
70 | 71 |
|
71 | 72 | var curves = stack.curves.map(function(curve){ |
72 | 73 | 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)}} |
76 | 75 | onMouseLeave={self.handleMouseLeave}/>; |
77 | 76 | }); |
78 | 77 |
|
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 | | - |
88 | 78 | return ( |
89 | 79 | <div id="stack"> |
90 | 80 | <svg width="500" height="380"> |
91 | 81 | <g transform="translate(40, 10)"> |
92 | 82 | {curves} |
93 | | - {text} |
94 | 83 | </g> |
95 | 84 | </svg> |
96 | 85 | </div> |
|
0 commit comments