Debian Codegraph @

bqplot/js/src/Selector.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 // const d3 =Object.assign({}, require("d3-selection"));
18 import { Interaction } from './Interaction';
19 import { LinearScale } from './LinearScale';
20 const convert_dates = require('./utils').convert_dates;
21 import { WidgetView } from '@jupyter-widgets/base';
22
23 export abstract class BaseSelector extends Interaction {
24 initialize(parameters) {
25 this.setElement(
26 document.createElementNS(d3.namespaces.svg, 'g') as HTMLElement
27 );
28 this.d3el = d3.select(this.el);
29 // The following line is a workaround to avoid calling the initialize
30 // method from Interaction. Indeed this last one wraps the area responsible
31 // for capturing mouse events in a rect element whose width and height are
32 // 0. The whole hierarchy of Interaction should be refactored to fix this.
33 WidgetView.prototype.initialize.call(this, parameters);
34 //super.initialize.apply(this, arguments);
35 }
36
37 render() {
38 this.parent = this.options.parent;
39 this.width =
40 this.parent.width - this.parent.margin.left - this.parent.margin.right;
41 this.height =
42 this.parent.height - this.parent.margin.top - this.parent.margin.bottom;
43 this.mark_views_promise = this.populate_mark_views();
44 }
45
46 create_listeners() {
47 this.listenTo(this.parent, 'margin_updated', this.relayout);
48 this.listenTo(this.model, 'change:selected', this.selected_changed);
49 this.listenTo(this.model, 'change:marks', this.marks_changed);
50 this.listenTo(this.model, 'msg:custom', this.handle_custom_messages);
51 }
52
53 relayout() {
54 this.height =
55 this.parent.height - this.parent.margin.top - this.parent.margin.bottom;
56 this.width =
57 this.parent.width - this.parent.margin.left - this.parent.margin.right;
58 }
59
60 async populate_mark_views() {
61 const fig = this.parent;
62 const mark_ids = this.model.get('marks').map((m) => m.model_id);
63 const views = await Promise.all(fig.mark_views.views);
64
65 const fig_mark_ids = fig.mark_views._models.map((m) => m.model_id);
66 const mark_indices = mark_ids.map((mid) => fig_mark_ids.indexOf(mid));
67 // return the views, based on the assumption that fig.mark_views is an
68 // ordered list
69 this.mark_views = mark_indices.map((elem) => views[elem]);
70 }
71
72 marks_changed() {
73 this.populate_mark_views().then(() => this.selected_changed());
74 }
75
76 handle_custom_messages(msg) {
77 if (msg.type === 'reset') {
78 this.reset();
79 }
80 }
81
82 abstract reset();
83
84 abstract selected_changed();
85 abstract create_scales();
86
87 set_selected(name, value) {
88 this.model.set(name, convert_dates(value));
89 }
90
91 width: number;
92 height: number;
93 mark_views_promise: Promise<void>;
94 }
95
96 export abstract class BaseXSelector extends BaseSelector {
97 create_scales() {
98 if (this.scale) {
99 this.scale.remove();
100 }
101 if (this.model.get('scale')) {
102 const that = this;
103 return this.create_child_view(this.model.get('scale')).then((view) => {
104 that.scale = view as WidgetView as LinearScale;
105 // The argument is to suppress the update to gui
106 that.update_scale_domain(true);
107 that.set_range([that.scale]);
108 that.scale.on('domain_changed', that.update_scale_domain, that);
109 return view;
110 });
111 }
112 }
113
114 update_scale_domain(ignore_gui_update = false) {
115 // When the domain of the scale is updated, the domain of the scale
116 // for the selector must be expanded to account for the padding.
117 const xy = this.model.get('orientation') == 'vertical' ? 'y' : 'x';
118 const initial_range = this.parent.padded_range(xy, this.scale.model);
119 const target_range = this.parent.range(xy);
120 this.scale.expand_domain(initial_range, target_range);
121 }
122
123 set_range(array) {
124 const xy = this.model.get('orientation') == 'vertical' ? 'y' : 'x';
125 for (let iter = 0; iter < array.length; iter++) {
126 array[iter].set_range(this.parent.range(xy));
127 }
128 }
129
130 scale: LinearScale;
131 }
132
133 export abstract class BaseXYSelector extends BaseSelector {
134 create_scales() {
135 const that = this;
136 if (this.x_scale) {
137 this.x_scale.remove();
138 }
139 if (this.y_scale) {
140 this.y_scale.remove();
141 }
142 const scale_promises = [];
143 if (this.model.get('x_scale')) {
144 scale_promises.push(
145 this.create_child_view(this.model.get('x_scale')).then((view) => {
146 that.x_scale = view as WidgetView as LinearScale;
147 that.update_xscale_domain();
148 that.set_x_range([that.x_scale]);
149 that.x_scale.on('domain_changed', that.update_xscale_domain, that);
150 return view;
151 })
152 );
153 }
154 if (this.model.get('y_scale')) {
155 scale_promises.push(
156 this.create_child_view(this.model.get('y_scale')).then((view) => {
157 that.y_scale = view as WidgetView as LinearScale;
158 that.update_yscale_domain();
159 that.set_y_range([that.y_scale]);
160 that.y_scale.on('domain_changed', that.update_yscale_domain, that);
161 return view;
162 })
163 );
164 }
165
166 return Promise.all(scale_promises);
167 }
168
169 set_x_range(array) {
170 for (let iter = 0; iter < array.length; iter++) {
171 array[iter].set_range(this.parent.range('x'));
172 }
173 }
174
175 set_y_range(array) {
176 for (let iter = 0; iter < array.length; iter++) {
177 array[iter].set_range(this.parent.range('y'));
178 }
179 }
180
181 update_xscale_domain() {
182 // When the domain of the scale is updated, the domain of the scale
183 // for the selector must be expanded to account for the padding.
184 const initial_range = this.parent.padded_range('x', this.x_scale.model);
185 const target_range = this.parent.range('x');
186 this.x_scale.expand_domain(initial_range, target_range);
187 }
188
189 update_yscale_domain() {
190 // When the domain of the scale is updated, the domain of the scale
191 // for the selector must be expanded to account for the padding.
192 const initial_range = this.parent.padded_range('y', this.y_scale.model);
193 const target_range = this.parent.range('y');
194 this.y_scale.expand_domain(initial_range, target_range);
195 }
196
197 x_scale: LinearScale;
198 y_scale: LinearScale;
199 }