Debian Codegraph @

bqplot/js/src/FastIntervalSelector.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 import * as _ from 'underscore';
17 import * as d3 from 'd3';
18 // var d3 =Object.assign({}, require("d3-selection"), require("d3-selection-multi"));
19 import { BaseXSelector } from './Selector';
20 import * as sel_utils from './selector_utils';
21 import { applyAttrs } from './utils';
22
23 export class FastIntervalSelector extends BaseXSelector {
24 render() {
25 super.render();
26 this.freeze_but_move = true;
27 this.freeze_dont_move = false;
28 this.active = false;
29 this.dirty = false;
30 this.size = this.model.get('size');
31
32 this.width =
33 this.parent.width - this.parent.margin.left - this.parent.margin.right;
34 this.height =
35 this.parent.height - this.parent.margin.top - this.parent.margin.bottom;
36
37 const that = this;
38 const scale_creation_promise = this.create_scales();
39 Promise.all([this.mark_views_promise, scale_creation_promise]).then(() => {
40 //container for mouse events
41 that.background = that.d3el
42 .append('rect')
43 .attr('x', 0)
44 .attr('y', 0)
45 .attr('width', that.width)
46 .attr('height', that.height)
47 .attr('class', 'selector selectormouse')
48 .attr('pointer-events', 'all')
49 .attr('visibility', 'hidden');
50
51 that.background
52 .on('mousemove', _.bind(that.mousemove, that))
53 .on('click', _.bind(that.click, that))
54 .on('dblclick', _.bind(that.dblclick, that));
55
56 that.rect = that.d3el
57 .append('rect')
58 .attr('class', 'selector intsel')
59 .attr('x', 0)
60 .attr('y', 0)
61 .attr('width', that.size)
62 .attr('height', that.height)
63 .attr('pointer-events', 'none')
64 .attr('display', 'none');
65
66 that.color_change();
67 that.selected_changed();
68 that.create_listeners();
69 });
70 }
71
72 create_listeners() {
73 super.create_listeners();
74 this.listenTo(this.model, 'change:color', this.color_change);
75 }
76
77 color_change() {
78 if (this.model.get('color') !== null) {
79 this.rect.style('fill', this.model.get('color'));
80 }
81 }
82
83 click() {
84 this.active = true;
85 this.rect.style('display', 'inline');
86 this.freeze_but_move = this.model.get('size')
87 ? true
88 : !this.freeze_but_move;
89 }
90
91 dblclick() {
92 this.freeze_dont_move = !this.freeze_dont_move;
93 }
94
95 mousemove() {
96 if (this.freeze_dont_move || !this.active) {
97 return;
98 }
99 this.dirty = true;
100 const mouse_pos = d3.mouse(this.background.node());
101 const int_len =
102 this.size > 0 ? this.size : parseInt(this.rect.attr('width'));
103 const vert_factor = (this.height - mouse_pos[1]) / this.height;
104 const interval_size = this.freeze_but_move
105 ? int_len
106 : Math.round(vert_factor * this.width);
107
108 let start;
109 if (mouse_pos[0] - interval_size / 2 < 0) {
110 start = 0;
111 } else if (mouse_pos[0] + interval_size / 2 > this.width) {
112 start = this.width - interval_size;
113 } else {
114 start = mouse_pos[0] - interval_size / 2;
115 }
116
117 //update the interval location and size
118 this.rect.attr('x', start);
119 this.rect.attr('width', interval_size);
120 const pixel_extent = [start, start + interval_size];
121 this.set_selected('selected', this.scale.invert_range(pixel_extent));
122 this.update_mark_selected(pixel_extent, undefined);
123 this.touch();
124 this.dirty = false;
125 }
126
127 update_mark_selected(extent_x, extent_y) {
128 if (extent_x === undefined || extent_x.length === 0) {
129 // Reset all the selected in marks
130 _.each(this.mark_views, (mark_view: any) => {
131 return mark_view.selector_changed();
132 });
133 return;
134 }
135 let x, y;
136 if (extent_y === undefined) {
137 // 1d brush
138 const orient = this.model.get('orientation');
139 (x = orient == 'vertical' ? [] : extent_x),
140 (y = orient == 'vertical' ? extent_x : []);
141 } else {
142 // 2d brush
143 (x = extent_x), (y = extent_y);
144 }
145 const point_selector = function (p) {
146 return sel_utils.point_in_rectangle(p, x, y);
147 };
148 const rect_selector = function (xy) {
149 return sel_utils.rect_inter_rect(xy[0], xy[1], x, y);
150 };
151
152 _.each(
153 this.mark_views,
154 (mark_view: any) => {
155 mark_view.selector_changed(point_selector, rect_selector);
156 },
157 this
158 );
159 }
160
161 relayout() {
162 super.relayout();
163
164 this.adjust_rectangle();
165 this.background.attr('width', this.width).attr('height', this.height);
166
167 this.set_range([this.scale]);
168 }
169
170 reset() {
171 this.rect.attr('x', 0).attr('width', 0);
172 this.model.set('selected', null);
173 this.update_mark_selected(undefined, undefined);
174 this.touch();
175 }
176
177 update_scale_domain(ignore_gui_update) {
178 // Call the base class function to update the scale.
179 super.update_scale_domain();
180 if (ignore_gui_update !== true) {
181 this.selected_changed();
182 }
183 }
184
185 selected_changed() {
186 //TODO: should the size get overridden if it was set previously and
187 //then selected was changed from the python side?
188 if (this.dirty) {
189 //this change was most probably triggered from the js side and
190 //should be ignored.
191 return;
192 }
193 //reposition the interval selector and set the selected attribute.
194 const selected = this.model.get('selected') || [];
195 if (selected.length === 0) {
196 this.reset();
197 } else if (selected.length != 2) {
198 // invalid value for selected. Ignoring the value
199 return;
200 } else {
201 let pixels = selected.map(this.scale.scale);
202 pixels = pixels.sort((a, b) => {
203 return a - b;
204 });
205
206 applyAttrs(this.rect, {
207 x: pixels[0],
208 width: pixels[1] - pixels[0],
209 }).style('display', 'inline');
210 this.active = true;
211 this.update_mark_selected(pixels, undefined);
212 }
213 }
214
215 adjust_rectangle() {
216 if (this.model.get('orientation') == 'vertical') {
217 this.d3el.selectAll('rect').attr('x', 0).attr('width', this.width);
218 } else {
219 this.d3el.selectAll('rect').attr('y', 0).attr('height', this.height);
220 }
221 }
222
223 freeze_but_move: boolean;
224 freeze_dont_move: boolean;
225 active: boolean;
226 dirty: boolean;
227 size: number;
228 background: d3.Selection<SVGRectElement, any, any, any>;
229 rect: d3.Selection<SVGRectElement, any, any, any>;
230 }