-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile-interactor.js
263 lines (236 loc) · 8.61 KB
/
profile-interactor.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"use strict";
function profileInteractor(state, x, y) {
// x, y are d3.scale objects (linear, log, etc) from parent
// dispatch is the d3 event dispatcher: should have event "update" register
//var state = options;
var name = state.name;
var radius = ( state.radius == null ) ? 5 : state.radius;
var event_name = "profile." + state.name;
var dispatch = d3.dispatch("update","changed");
var x = x || d3.scale.linear();
var y = y || d3.scale.linear();
var interpolation = (state.interpolation == null) ? 'step-before' : state.interpolation;
var prevent_crossing = (state.prevent_crossing == null) ? false : state.prevent_crossing;
var show_points = (state.show_points == null) ? true : state.show_points;
var show_lines = (state.show_lines == null) ? true : state.show_lines;
var close_path = (state.close_path == null) ? false : state.close_path;
var fixed = (state.fixed == null) ? false : state.fixed;
var cursor = (fixed) ? "auto" : "move";
var draw_extensions = (state.draw_extensions == null) ? true : false;
var series = state.series || [];
var constraints = [];
var line = d3.svg.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); })
.interpolate(interpolation);
function data_to_pairs(data, column) {
var xi = 0;
return data.map(function(d, i) { return [xi+=d.thickness, d[column], column] });
}
function data_to_arrays(data) {
//var columns = ["sld_n", "sld_m", "theta_m"];
return series.map(function(s) { return data_to_pairs(data, s.id) });
}
function data_to_edges(data) {
return series.map(function(s) {
var xi = 0,
column = s.id,
yi = data[0][column],
edges = [];
data.forEach(function(d) {
edges.push([[xi, yi, column, 'v'], [xi, yi=d[column], column, 'v']])
edges.push([[xi, yi, column, 'h'], [xi+=d.thickness, yi, column, 'h']])
});
return edges;
})
}
function arrays_to_data(arrays) {
var columns = series.map(function(s) { return s.id });
var x = 0;
var output_array = [];
// assume all arrays are the same length:
arrays[0].forEach(function(a,i) {
var d = {};
columns.forEach(function(c, ci) {
d[c] = arrays[ci][i][1];
});
d.thickness = a[0] - x;
if (d.thickness < 0) {
d.thickness = 0;
} else {
x = a[0];
}
output_array[i] = d;
});
return output_array;
}
var colors = [
"#4bb2c5",
"#EAA228",
"#c5b47f",
"#579575",
"#839557",
"#958c12",
"#953579",
"#4b5de4",
"#d8b83f",
"#ff5800",
"#0085cc",
"#c747a3",
"#cddf54",
"#FBD178",
"#26B4E3",
"#bd70c7"
]
var drag_corner = d3.behavior.drag()
.on("drag", dragmove_corner)
.on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); });
var drag_edge = d3.behavior.drag()
.on("drag", dragmove_edge)
.on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); });
function interactor(selection) {
var group = selection.append("g")
.classed("interactors interactor-" + name, true)
.style("cursor", cursor)
interactor.update = function() {
var edge_groups_sel = group.selectAll("g.edges")
.data(data_to_edges(state.profile_data))
edge_groups_sel
.enter().append("g")
.attr("class", "edges")
.style("stroke", function(d,i) {return (series[i] || {}).color1 || colors[i] })
.style("stroke-linecap", "round")
.style("stroke-width", "4px")
.style("fill", "none")
edge_groups_sel
.exit().remove()
var corner_groups_sel = group.selectAll("g.corners")
.data(data_to_arrays(state.profile_data))
corner_groups_sel
.enter().append("g")
.classed("corners", true)
.attr("fill", function(d,i) {return (series[i] || {}).color1 || colors[i] });
corner_groups_sel
.exit().remove()
corner_groups_sel.each(function(d,i) {
var corners = d3.select(this).selectAll('.corner').data(d)
var new_corners = corners.enter().append("circle")
.classed("corner", true)
.attr("vertex", function(dd,ii) { return ii.toFixed()})
.attr("r", radius);
if (!fixed) new_corners.call(drag_corner);
corners
.attr("cx", function(dd) { return x(dd[0]); })
.attr("cy", function(dd) { return y(dd[1]); })
.on("click", function() {
corner_groups_sel.selectAll("circle.corner")
.attr("r", radius)
.classed("selected", false);
d3.select(this).attr("r", radius*1.2).classed("selected", true);
})
corners.exit().remove();
});
edge_groups_sel.each(function(d,i) {
var edges = d3.select(this).selectAll('.edge').data(d);
var new_edges = edges.enter().append("path")
.classed("edge", true)
.attr("side", function(dd,ii) { return ii.toFixed()})
.attr("direction", function(d) { return d[0][3] })
if (!fixed) new_edges.call(drag_edge);
d3.select(this).selectAll('.edge').on("dblclick", function(dd, ii) {
var direction = d3.select(this).attr("direction"),
old_row_index = Math.floor(ii/2),
old_row = state.profile_data[old_row_index],
new_row = jQuery.extend(true, {}, old_row);
if (direction == "h") {
var xi = x.invert(d3.mouse(this)[0]),
thickness_below = xi - d[ii][0][0],
thickness_above = d[ii][1][0] - xi;
new_row.thickness = thickness_below;
old_row.thickness = thickness_above;
}
else if (direction == "v") {
var yi = y.invert(d3.mouse(this)[1]),
col = state.series[i].id;
new_row.thickness = 0;
new_row[col] = yi;
}
state.profile_data.splice(old_row_index, 0, new_row);
d3.event.preventDefault();
d3.event.stopPropagation();
dispatch.changed(state.profile_data);
interactor.update();
})
edges.exit().remove();
if (draw_extensions) {
var left_ext = d3.select(this).selectAll('.left.extension')
.data([[[x.invert(-10) , d[0][0][1]], d[0][0]]])
.enter().append("path")
.classed("left extension", true)
var right_ext = d3.select(this).selectAll('.right.extension')
.data([[[x.invert(x.range()[1]+10) , d.slice(-1)[0][0][1]], d.slice(-1)[0][0]]])
.enter().append("path")
.classed("right extension", true)
}
d3.select(this).selectAll(".edge, .extension").attr("d", line);
});
// fire!
dispatch.update();
}
interactor.update();
}
function dragmove_corner(d,i) {
var new_x = x.invert(d3.event.x),
new_y = y.invert(d3.event.y);
var new_dx = x.invert(x(0) + d3.event.dx),
new_dy = y.invert(y(0) + d3.event.dy);
state.profile_data[i].thickness += new_dx; //= Math.max(0, state.profile_data[i].thickness + new_dx);
state.profile_data[i][d[2]] = new_y;
constraints.forEach(function(constraint) {
constraint(state.profile_data, d, i);
});
dispatch.changed(state.profile_data);
interactor.update();
}
function dragmove_edge(d,i) {
var new_x = x.invert(d3.event.x),
new_y = y.invert(d3.event.y);
var new_dx = x.invert(x(0) + d3.event.dx),
new_dy = y.invert(y(0) + d3.event.dy);
var direction = d3.select(this).attr("direction"),
old_row_index = Math.floor(i/2);
if (direction == "h") {
state.profile_data[old_row_index][d[0][2]] = new_y;
}
else if (direction == "v") {
var new_thickness = new_x - d[0][0];
old_row_index -= 1;
if (old_row_index >= 0) {
state.profile_data[old_row_index].thickness += new_dx;
}
}
constraints.forEach(function(constraint) {
constraint(state.profile_data, d, old_row_index);
});
dispatch.changed(state.profile_data);
interactor.update();
}
interactor.x = function(_) {
if (!arguments.length) return x;
x = _;
return interactor;
};
interactor.y = function(_) {
if (!arguments.length) return y;
y = _;
return interactor;
};
interactor.constraints = function(_) {
if (!arguments.length) return constraints;
constraints = _;
return interactor;
};
interactor.state = state;
interactor.dispatch = dispatch;
return interactor
}