Debian Codegraph @

bqplot/js/src/LinesModel.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-scale"), require("d3-scale-chromatic"), require("d3-array"));
18 import * as _ from 'underscore';
19 import { MarkModel } from './MarkModel';
20 import * as serialize from './serialize';
21 import * as utils from './utils';
22
23 export class LinesModel extends MarkModel {
24 defaults() {
25 return {
26 ...MarkModel.prototype.defaults(),
27 _model_name: 'LinesModel',
28 _view_name: 'Lines',
29 x: [],
30 y: [],
31 color: null,
32 scales_metadata: {
33 x: { orientation: 'horizontal', dimension: 'x' },
34 y: { orientation: 'vertical', dimension: 'y' },
35 color: { dimension: 'color' },
36 },
37 colors: d3.scaleOrdinal(d3.schemeCategory10).range(),
38 fill_colors: d3.scaleOrdinal(d3.schemeCategory10).range(),
39 stroke_width: 2.0,
40 labels_visibility: 'none',
41 curves_subset: [],
42 line_style: 'solid',
43 interpolation: 'linear',
44 close_path: false,
45 fill: 'none',
46 marker: null,
47 marker_size: 64,
48 opacities: [],
49 fill_opacities: [],
50 };
51 }
52
53 initialize(attributes, options) {
54 super.initialize(attributes, options);
55 this.on_some_change(['x', 'y', 'color'], this.update_data, this);
56 this.on('change:labels', this.update_labels, this);
57 // FIXME: replace this with on("change:preserve_domain"). It is not done here because
58 // on_some_change depends on the GLOBAL backbone on("change") handler which
59 // is called AFTER the specific handlers on("change:foobar") and we make that
60 // assumption.
61 this.on_some_change(['preserve_domain'], this.update_domains, this);
62 this.update_data();
63 this.update_domains();
64 }
65
66 update_data() {
67 this.dirty = true;
68 // Handling data updates
69 const that = this;
70 this.x_data = this.get('x');
71 this.y_data = this.get('y');
72 this.color_data = this.get('color') || [];
73
74 let curve_labels = this.get('labels');
75 if (this.x_data.length === 0 || this.y_data.length === 0) {
76 this.mark_data = [];
77 } else {
78 this.x_data = utils.is_array(this.x_data[0])
79 ? this.x_data
80 : [this.x_data];
81 this.y_data = utils.is_array(this.y_data[0])
82 ? this.y_data
83 : [this.y_data];
84 curve_labels = this.get_labels();
85
86 const y_length = this.y_data.length;
87
88 if (this.x_data.length == 1 && y_length > 1) {
89 // same x for all y
90 this.mark_data = curve_labels.map((name, i) => {
91 return {
92 name: name,
93 // since y_data may be a TypedArray, explicitly use Array.map
94 values: Array.prototype.map.call(that.y_data[i], (d, j) => {
95 return {
96 x: that.x_data[0][j],
97 y: d,
98 y0: that.y_data[Math.min(i + 1, y_length - 1)][j],
99 sub_index: j,
100 };
101 }),
102 color: that.color_data[i],
103 index: i,
104 };
105 });
106 } else {
107 this.mark_data = curve_labels.map((name, i) => {
108 const xy_data = d3.zip(that.x_data[i], that.y_data[i]);
109 return {
110 name: name,
111 values: xy_data.map((d, j) => {
112 return {
113 x: d[0],
114 y: d[1],
115 y0: that.y_data[Math.min(i + 1, y_length - 1)][j],
116 sub_index: j,
117 };
118 }),
119 color: that.color_data[i],
120 index: i,
121 };
122 });
123 }
124 }
125 this.update_domains();
126 this.dirty = false;
127 this.trigger('data_updated');
128 }
129
130 update_labels() {
131 // update the names in mark_data
132 const labels = this.get_labels();
133 this.mark_data.forEach((element, i) => {
134 element.name = labels[i];
135 });
136 this.trigger('labels_updated');
137 }
138
139 get_labels() {
140 // Function to set the labels appropriately.
141 // Setting the labels to the value sent and filling in the
142 // remaining values.
143 let curve_labels = this.get('labels');
144 const data_length =
145 this.x_data.length == 1
146 ? this.y_data.length
147 : Math.min(this.x_data.length, this.y_data.length);
148 if (curve_labels.length > data_length) {
149 curve_labels = curve_labels.slice(0, data_length);
150 } else if (curve_labels.length < data_length) {
151 _.range(curve_labels.length, data_length).forEach((index) => {
152 curve_labels[index] = 'C' + (index + 1);
153 });
154 }
155 return curve_labels;
156 }
157
158 update_domains() {
159 if (!this.mark_data) {
160 return;
161 }
162 const scales = this.get('scales');
163 const x_scale = scales.x,
164 y_scale = scales.y;
165 const color_scale = scales.color;
166
167 if (!this.get('preserve_domain').x) {
168 x_scale.compute_and_set_domain(
169 this.mark_data.map((elem) => {
170 return elem.values.map((d) => {
171 return d.x;
172 });
173 }),
174 this.model_id + '_x'
175 );
176 } else {
177 x_scale.del_domain([], this.model_id + '_x');
178 }
179
180 if (!this.get('preserve_domain').y) {
181 y_scale.compute_and_set_domain(
182 this.mark_data.map((elem) => {
183 return elem.values.map((d) => {
184 return d.y;
185 });
186 }),
187 this.model_id + '_y'
188 );
189 } else {
190 y_scale.del_domain([], this.model_id + '_y');
191 }
192 if (color_scale !== null && color_scale !== undefined) {
193 if (!this.get('preserve_domain').color) {
194 color_scale.compute_and_set_domain(
195 this.mark_data.map((elem) => {
196 return elem.color;
197 }),
198 this.model_id + '_color'
199 );
200 } else {
201 color_scale.del_domain([], this.model_id + '_color');
202 }
203 }
204 }
205
206 get_data_dict(data, index) {
207 return data;
208 }
209
210 static serializers = {
211 ...MarkModel.serializers,
212 x: serialize.array_or_json_serializer,
213 y: serialize.array_or_json_serializer,
214 color: serialize.array_or_json_serializer,
215 };
216
217 x_data: any[];
218 y_data: any[];
219 color_data: number[];
220 }
221
222 export class FlexLineModel extends LinesModel {
223 defaults() {
224 return {
225 ...LinesModel.prototype.defaults(),
226 _model_name: 'FlexLineModel',
227 _view_name: 'FlexLine',
228
229 x: [],
230 y: [],
231 color: null,
232 scales_metadata: {
233 x: { orientation: 'horizontal', dimension: 'x' },
234 y: { orientation: 'vertical', dimension: 'y' },
235 color: { dimension: 'color' },
236 },
237 colors: d3.scaleOrdinal(d3.schemeCategory10).range(),
238 fill_colors: d3.scaleOrdinal(d3.schemeCategory10).range(),
239 stroke_width: 2.0,
240 labels_visibility: 'none',
241 curves_subset: [],
242 line_style: 'solid',
243 interpolation: 'linear',
244 close_path: false,
245 fill: 'none',
246 marker: null,
247 marker_size: 64,
248 opacities: [],
249 fill_opacities: [],
250 };
251 }
252
253 update_data() {
254 this.dirty = true;
255 // Handling data updates
256 const that = this;
257 this.x_data = this.get('x');
258 this.y_data = this.get('y');
259
260 let curve_labels = this.get('labels');
261 if (this.x_data.length === 0 || this.y_data.length === 0) {
262 this.mark_data = [];
263 this.data_len = 0;
264 } else {
265 this.x_data = !_.isNumber(this.x_data[0]) ? this.x_data : [this.x_data];
266 this.y_data = !_.isNumber(this.y_data[0]) ? this.y_data : [this.y_data];
267 curve_labels = this.get_labels();
268 const color_data = this.get('color') || [];
269 const width_data = [this.get('width') || []];
270 this.data_len = Math.min(this.x_data[0].length, this.y_data[0].length);
271
272 this.mark_data = [
273 {
274 name: curve_labels[0],
275 values: _.range(this.data_len - 1).map((val, index) => {
276 return {
277 x1: that.x_data[0][index],
278 y1: that.y_data[0][index],
279 x2: that.x_data[0][index + 1],
280 y2: that.y_data[0][index + 1],
281 color: color_data[index],
282 size: width_data[0][index],
283 };
284 }),
285 },
286 ];
287 }
288
289 this.update_domains();
290 this.dirty = false;
291 this.trigger('data_updated');
292 }
293
294 update_domains() {
295 if (!this.mark_data) {
296 return;
297 }
298 const scales = this.get('scales');
299 const x_scale = scales.x,
300 y_scale = scales.y;
301 const color_scale = scales.color;
302 const width_scale = scales.width;
303
304 if (!this.get('preserve_domain').x) {
305 x_scale.compute_and_set_domain(
306 this.x_data[0].slice(0, this.data_len),
307 this.model_id + '_x'
308 );
309 } else {
310 x_scale.del_domain([], this.model_id + '_x');
311 }
312
313 if (!this.get('preserve_domain').y) {
314 y_scale.compute_and_set_domain(
315 this.y_data[0].slice(0, this.data_len),
316 this.model_id + '_y'
317 );
318 } else {
319 y_scale.del_domain([], this.model_id + '_y');
320 }
321
322 if (color_scale !== null && color_scale !== undefined) {
323 if (!this.get('preserve_domain').color) {
324 color_scale.compute_and_set_domain(
325 this.mark_data.map((elem) => {
326 return elem.values.map((d) => {
327 return d.color;
328 });
329 }),
330 this.model_id + '_color'
331 );
332 } else {
333 color_scale.del_domain([], this.model_id + '_color');
334 }
335 }
336 if (width_scale !== null && width_scale !== undefined) {
337 if (!this.get('preserve_domain').width) {
338 width_scale.compute_and_set_domain(
339 this.mark_data.map((elem) => {
340 return elem.values.map((d) => {
341 return d.size;
342 });
343 }),
344 this.model_id + '_width'
345 );
346 } else {
347 width_scale.del_domain([], this.model_id + '_width');
348 }
349 }
350 }
351
352 static serializers = {
353 ...LinesModel.serializers,
354 width: serialize.array_or_json_serializer,
355 };
356
357 data_len: number;
358 }