-
Notifications
You must be signed in to change notification settings - Fork 46
/
px-vis-behavior-interaction.html
238 lines (191 loc) · 6.71 KB
/
px-vis-behavior-interaction.html
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
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script>
var PxVisBehaviorInteraction = window.PxVisBehaviorInteraction = (window.PxVisBehaviorInteraction || {});
/*
Name:
PxVisBehaviorInteraction.lasso
Description:
Polymer behavior that provides the methods and properties necessary
to handle lasso selection
@polymerBehavior PxVisBehaviorInteraction.lasso
*/
PxVisBehaviorInteraction.lasso = [{
properties: {
_isLassoing: {
type: Boolean,
value: false
},
_lassoDrawing: {
type: Object
},
_lassoGroup: {
type: Object
},
_lassoCoords: {
type: Object,
value: function() {
return [];
}
},
_lassoCrosshairData: {
type: Object,
value: function() {
return {
'rawData': [],
'timeStamps': []
}
}
},
_lassoGenerator: {
type: Object
},
/**
* When switched clears all the previous selection made
* (in _lassoCrosshairData)
*/
resetLassoData: {
type: Boolean,
value: false,
observer: '_resetLasso'
},
lassoType: String
},
_resetLasso: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
this._lassoCrosshairData = {
rawData: [],
timeStamps: []
}
},
_startLasso: function() {
//only allow left clicks
if(Px.d3.event.button === 0) {
this._calcCrosshair = true;
this._isLassoing = true;
//in case the user clicks inside the chart, and mouses out, we are waiting for a mouseup, and closing our action box with the coordinates available on the mouseup.
Px.d3.select(document).on('mouseup.action', this._stopLasso.bind(this));
Px.d3.select(document).on('mousemove.action', this._updateLasso.bind(this));
var mousePos = Px.d3.mouse(this._rect.node());
this._lassoGenerator = Px.d3.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; });
this._lassoGroup = this.svg.append('g')
.attr('class', 'lasso-group');
// TODO Change to box type and lasso type; toolbar selection
// make available to all
if(this._isTimeType(this.xAxisType) && this.lassoType !== 'freeform') {
//timeseries lasso will be a box
if(this.lassoType === 'yRect') {
const width = Math.max(this.width - this.margin.left - this.margin.right, 0);
this._lassoCoords.push([0, mousePos[1]], [width, mousePos[1]], [0, mousePos[1]], [width, mousePos[1]], [0, mousePos[1]]);
} else {
const _height = Math.max(this.height - this.margin.bottom - this.margin.top,0);
this._lassoCoords.push([mousePos[0], 0], [mousePos[0], _height], [mousePos[0], 0], [mousePos[0], _height], [mousePos[0], 0]);
}
} else {
//re add start at the end so that our polygon closes
this._lassoCoords.push([mousePos[0], mousePos[1]], [mousePos[0], mousePos[1]]);
}
this._lassoGroup.append('path')
.attr('class', 'lasso-line')
.attr('fill', this._checkThemeVariable('--px-vis-zoom-brush-fill-color', 'rgb(0,0,0)'))
.attr('fill-opacity', this._checkThemeVariable('--px-vis-zoom-brush-fill-opacity', 0.5))
.attr('stroke', this._checkThemeVariable('--px-vis-zoom-brush-outline-color', 'rgb(0,0,0)'));
this._calcData();
}
},
_updateLasso: function(first) {
if(this._isLassoing) {
var mousePos = Px.d3.mouse(this._rect.node());
if(this._isTimeType(this.xAxisType) && this.lassoType !== 'freeform') {
this._updateCoordsTS(mousePos);
} else if(!this.radial) {
this._updateCoordsCartesian(mousePos);
} else {
this._updateCoordsRadial(mousePos);
}
//update drawing
this._lassoGroup.selectAll('path.lasso-line')
.attr('d', this._lassoGenerator(this._lassoCoords));
this._calcData();
}
},
_updateCoordsCartesian: function(mousePos) {
var result = mousePos,
_height = Math.max(this.height - this.margin.bottom - this.margin.top,0),
last = this._lassoCoords.pop();
result[0] = this._constrainOnWidth(mousePos[0]);
result[1] = mousePos[1] < 0 ? 0 : mousePos[1];
result[1] = result[1] > _height ? _height : result[1];
this._lassoCoords.push(result);
this._lassoCoords.push(last);
},
_updateCoordsRadial: function(mousePos) {
//width = height = radius
var result = mousePos,
squareDist = mousePos[0]*mousePos[0] + mousePos[1]*mousePos[1],
ratio,
last = this._lassoCoords.pop();
//scale back values if we are outside of the circle
if(squareDist > this.width*this.width) {
ratio = this.width/Math.sqrt(squareDist);
result[0] = mousePos[0] * ratio;
result[1] = mousePos[1] * ratio;
}
this._lassoCoords.push(result);
this._lassoCoords.push(last);
},
_updateCoordsTS: function(mousePos) {
const last = this._lassoCoords.pop();
let first, second;
if(this.lassoType === 'yRect') {
const width = Math.max(this.width - this.margin.left - this.margin.right, 0);
const newY = this._constrainOnHeight(mousePos[1]);
first = [width, newY];
second = [0, newY];
} else {
const height = Math.max(this.height - this.margin.bottom - this.margin.top,0);
const newX = this._constrainOnWidth(mousePos[0]);
first = [newX, height];
second = [newX, 0];
}
this._lassoCoords.pop();
this._lassoCoords.pop();
this._lassoCoords.push(first);
this._lassoCoords.push(second);
this._lassoCoords.push(last);
},
_constrainOnWidth: function(xVal) {
var _width = Math.max(this.width - this.margin.left - this.margin.right,0),
result;
result = xVal < 0 ? 0 : xVal;
result = result > _width ? _width : result;
return result;
},
_constrainOnHeight: function(yVal) {
const height = Math.max(this.height - this.margin.top - this.margin.bottom, 0);
let result = yVal < 0 ? 0 : yVal;
result = result > height ? height : result;
return result;
},
_stopLasso: function() {
this._lassoGroup.remove();
this._lassoCoords = [];
this._isLassoing = false;
this._lassoCrosshairData = this.crosshairData;
}
}, PxVisBehavior.observerCheck]
</script>