-
Notifications
You must be signed in to change notification settings - Fork 1
/
x-slice-interactor.js
155 lines (132 loc) · 4.14 KB
/
x-slice-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
"use strict";
function xSliceInteractor(state, x, y) {
// dispatch is the d3 event dispatcher: should have event "update" register
var name = state.name;
var dispatch = d3.dispatch("update");
var x = x || d3.scale.linear();
var y = y || d3.scale.linear();
var show_lines = (state.show_lines == null) ? true : state.show_lines;
var show_range = (state.show_range == null) ? true : state.show_range;
var fixed = (state.fixed == null) ? false : state.fixed;
var cursor = (fixed) ? "auto" : "move";
var xval_to_path = function(xval) {
var xd = x.range(),
yd = y.range();
var pathstring = "";
// start in the center and draw to the edge
pathstring += "M" + x(xval).toFixed();
pathstring += "," + yd[0].toFixed();
pathstring += "L" + x(xval).toFixed();
pathstring += "," + yd[1].toFixed();
return pathstring
}
var state_to_paths = function(state) {
// convert from cx, cy, angle_offset and angle_range to paths for
// boundaries and center lines
// calculate angles of graph corners
if (show_lines) {
var x1_line = xval_to_path(state.x1),
x2_line = xval_to_path(state.x2);
return [
{
"path": x1_line,
"classname": "x1 x-slice"
},
{
"path": x2_line,
"classname": "x2 x-slice"
}
]
}
else {
return [];
}
}
var state_to_rect = function(state) {
if (show_range) {
var yd = y.range();
var x0 = Math.min(x(state.x1), x(state.x2));
var x1 = Math.max(x(state.x1), x(state.x2));
var y0 = Math.min(yd[0], yd[1]);
var y1 = Math.max(yd[0], yd[1]);
var rect = {
"x": x0,
"y": y0,
"width": x1 - x0,
"height": y1 - y0
}
return rect
}
else {
return null
}
}
var drag_lines = d3.behavior.drag()
.on("drag", dragmove_lines)
.on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); });
function interactor(selection) {
var group = selection.append("g")
.classed("interactors interactor-" + name, true)
var fill = group.append("rect")
.attr("class", "range-fill")
.style("stroke", "none")
.datum(state_to_rect(state))
fill
.style("fill", state.color1)
.attr("opacity", 0.1)
.attr("x", function(d) {return d.x})
.attr("y", function(d) {return d.y})
.attr("width", function(d) {return d.width})
.attr("height", function(d) {return d.height})
var lines_group = group.append("g")
.attr("class", "lines-group")
.style("fill", "none")
.style("cursor", cursor)
.style("stroke", state.color1)
.style("stroke-width", "4px");
var lines = lines_group.selectAll(".lines")
.data(state_to_paths(state))
.enter().append("path")
.attr("class", function(d) {return d['classname']})
.classed("lines", true)
.attr("d", function(d) {return d['path']})
if (!fixed) lines.call(drag_lines);
interactor.update = function(preventPropagation) {
group.selectAll('rect').datum(state_to_rect(state))
.attr("x", function(d) {return d.x})
.attr("y", function(d) {return d.y})
.attr("width", function(d) {return d.width})
.attr("height", function(d) {return d.height})
group.selectAll('.lines')
.data(state_to_paths(state))
.attr("d", function(d) {return d['path']})
// fire!
if (!preventPropagation) {
dispatch.update();
}
}
}
function dragmove_lines() {
var new_x = x.invert(d3.event.x);
if (d3.select(this).classed("x1")) {
state.x1 = new_x;
}
else {
state.x2 = new_x;
}
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.state = state;
interactor.dispatch = dispatch;
return interactor
}