Debian Codegraph @

bqplot/js/src/IndexSelector.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 d3 from 'd3';
17 // var d3 =Object.assign({}, require("d3-selection"));
18 import * as _ from 'underscore';
19 import { BaseXSelector } from './Selector';
20
21 export class IndexSelector extends BaseXSelector {
22 render() {
23 super.render();
24 this.active = false;
25 this.dirty = false;
26 const that = this;
27 const scale_creation_promise = this.create_scales();
28 Promise.all([this.mark_views_promise, scale_creation_promise]).then(() => {
29 that.line = that.d3el
30 .append('line')
31 .attr('class', 'selector indsel')
32 .attr('x1', 0)
33 .attr('y1', 0)
34 .attr('x2', 0)
35 .attr('y2', that.height)
36 .attr('stroke-width', that.model.get('line_width'))
37 .attr('pointer-events', 'none')
38 .attr('visibility', 'hidden');
39 that.color_change();
40
41 //container for mouse events
42 that.background = that.d3el
43 .append('rect')
44 .attr('x', 0)
45 .attr('y', 0)
46 .attr('width', that.width)
47 .attr('height', that.height)
48 .attr('class', 'selector selectormouse')
49 .attr('pointer-events', 'all')
50 .attr('visibility', 'hidden');
51
52 that.background
53 .on('mousemove', _.bind(that.mousemove, that))
54 .on('click', _.bind(that.initial_click, that));
55
56 that.create_listeners();
57 that.selected_changed();
58 });
59 }
60
61 create_listeners() {
62 super.create_listeners();
63 this.listenTo(this.model, 'change:color', this.color_change);
64 }
65
66 color_change() {
67 if (this.model.get('color') !== null) {
68 this.line.style('stroke', this.model.get('color'));
69 }
70 }
71
72 initial_click() {
73 this.line.attr('visibility', 'visible');
74 this.click();
75 this.background.on('click', _.bind(this.click, this));
76 }
77
78 click() {
79 this.active = !this.active;
80 }
81
82 mousemove() {
83 if (!this.active || this.dirty) {
84 return;
85 }
86 this.dirty = true;
87 const mouse_pos = d3.mouse(this.background.node());
88 const xpixel = mouse_pos[0];
89 //update the index vertical line
90 this.line.attr('x1', xpixel).attr('x2', xpixel);
91
92 this.set_selected('selected', [this.invert_pixel(xpixel)]);
93 _.each(this.mark_views, (mark_view: any) => {
94 mark_view.invert_point(xpixel);
95 });
96 this.touch();
97 this.dirty = false;
98 }
99
100 invert_pixel(pixel) {
101 return this.scale.invert(pixel);
102 }
103
104 reset() {
105 this.active = false;
106 if (this.line !== undefined && this.line !== null) {
107 this.line.attr('x1', 0).attr('x2', 0).attr('visibility', 'hidden');
108 }
109
110 if (this.background !== undefined && this.background !== null) {
111 this.background.on('click', _.bind(this.initial_click, this));
112 }
113 this.model.set('selected', null);
114
115 _.each(this.mark_views, (mark_view: any) => {
116 mark_view.invert_point();
117 });
118 this.touch();
119 }
120
121 update_scale_domain(ignore_gui_update) {
122 // Call the base class function to update the scale.
123 super.update_scale_domain();
124 if (ignore_gui_update !== true) {
125 this.selected_changed();
126 }
127 }
128
129 selected_changed() {
130 if (this.dirty) {
131 //this change was most probably triggered from the js side and
132 //should be ignored.
133 return;
134 }
135 //reposition the interval selector and set the selected attribute.
136 const selected = this.model.get('selected') || [];
137 if (selected.length === 0) {
138 this.reset();
139 } else if (selected.length != 1) {
140 // invalid value for selected. Ignoring the value
141 return;
142 } else {
143 const pixel = this.scale.scale(selected[0]);
144 if (this.line !== undefined && this.line !== null) {
145 this.line
146 .attr('x1', pixel)
147 .attr('x2', pixel)
148 .attr('visibility', 'visible');
149 }
150 //the selected may be called before the index selector is
151 //active for the first time.
152 this.background.on('click', _.bind(this.click, this));
153 _.each(this.mark_views, (mark_view: any) => {
154 mark_view.invert_point(pixel);
155 });
156 }
157 }
158
159 relayout() {
160 super.relayout();
161 this.line.attr('y1', 0).attr('y2', this.height);
162 this.background.attr('width', this.width).attr('height', this.height);
163 this.set_range([this.scale]);
164 }
165
166 set_range(array) {
167 for (let iter = 0; iter < array.length; iter++) {
168 array[iter].set_range([0, this.width]);
169 }
170 }
171
172 active: boolean;
173 dirty: boolean;
174 line: d3.Selection<SVGLineElement, any, any, any>;
175 background: d3.Selection<SVGRectElement, any, any, any>;
176 }