-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchart.js
144 lines (121 loc) · 3.68 KB
/
chart.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @jsx React.DOM
*/
var React = require('react');
var Allocation = require('./allocation');
var Bucket = require('./bucket');
var _ = require('lodash');
module.exports = React.createClass({
getInitialState: function() {
return {
dragging: false,
data: this.props.data,
dragElement: null
};
},
render: function() {
return (
<svg
width={this.props.width}
height={this.props.height}
onMouseUp={this.dragEnd}
onMouseMove={this.onDrag}>
<g>
{this.props.data.map(function(d){
console.log(this.activate)
switch (d.type) {
case 'allocation':
return <Allocation
ref={d.ref}
dragStart={this.dragStart}
key={d.id}
datum={d} />
break;
case 'bucket':
return <Bucket
ready={this.state.dragging}
ref={d.ref}
key={d.id}
datum={d}
scale={this.props.scale} />
break;
};
}.bind(this))}
</g>
<g>
{this.state.dragging ? this.props.data.map(function(d) {
var mouseOver = this.state.dragging ? this.activate : null;
var mouseLeave = this.state.dragging ? this.deActivate : null;
if (d.type === 'bucket') {
return <circle
className='collisionDetection'
key={d.id + '$-over'}
onMouseOver={mouseOver}
onMouseLeave={mouseLeave}
cx={d.x}
cy={d.y}
r={d.r}
style={{opacity:0}} >
</circle>
} else {
return;
}
}.bind(this)) : null }
</g>
</svg>
);
},
activate: function(e) {
//extract data from elements and send to allocation arcform function
var key = e.dispatchMarker.split("$")[1],
dropDatum = _.find(this.state.data, {id: key}),
dragDatum = _.find(this.state.data, {id: this.state.dragElement}),
startAngle = (dropDatum.allocated / dropDatum.target)*Math.PI*2,
endAngle = (dragDatum.value/dropDatum.target)*Math.PI*2 + startAngle,
midAngle = endAngle - startAngle,
radius = this.props.scale(dropDatum.target),
//offset center based on arc's computed centroid
center = [
dragDatum.x - (radius/2)*Math.cos(midAngle),
dragDatum.y - (radius/2)*Math.sin(midAngle)
];
this.refs.dragElement.arcForm({
startAngle: startAngle,
endAngle: endAngle,
radius: radius,
center: center,
value: dragDatum.value,
proportion: (dragDatum.value / dropDatum.target)
});
},
deActivate: function(e) {
var key = e.dispatchMarker.split("$")[1];
var coordinates = this.refs.dragElement.state.coordinates
this.refs.dragElement.circleForm({coordinates: coordinates});
},
onDrag: function(e) {
if (!this.state.dragging) {
return;
} else {
this.refs.dragElement.onDrag()
}
},
dragUpdate: function(key, bool) {
var dragElement = bool ? key : null
var updateData = this.state.data.map(function(d) {
if (d.id === key) {
d.ref = bool ? 'dragElement' : d.type;
}
return d;
});
this.setState({data: updateData, dragging: bool, dragElement: dragElement})
},
dragStart: function(e) {
var key = e.dispatchMarker.split("$")[1];
this.dragUpdate(key, true)
},
dragEnd: function(e) {
var key = e.dispatchMarker.split("$")[1];
this.dragUpdate(key, false)
}
});