Debian Codegraph @

bqplot/js/src/ScatterModel.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 { ScatterBaseModel } from './ScatterBaseModel';
17 import * as serialize from './serialize';
18 import * as _ from 'underscore';
19
20 export class ScatterModel extends ScatterBaseModel {
21 defaults() {
22 return {
23 ...ScatterBaseModel.prototype.defaults(),
24 _model_name: 'ScatterModel',
25 _view_name: 'Scatter',
26 skew: null,
27 marker: 'circle',
28 stroke: null,
29 stroke_width: 1.5,
30 default_skew: 0.5,
31 default_size: 64,
32 names: [],
33 display_names: true,
34 fill: true,
35 drag_color: null,
36 drag_size: 5.0,
37 names_unique: true,
38 };
39 }
40
41 initialize(attributes, options) {
42 // TODO: Normally, color, opacity and size should not require a redraw
43 super.initialize(attributes, options);
44 this.on('change:skew', this.update_data, this);
45 this.on_some_change(
46 ['names', 'names_unique'],
47 function () {
48 this.update_unique_ids();
49 this.trigger('data_updated');
50 },
51 this
52 );
53 }
54
55 update_mark_data() {
56 super.update_mark_data();
57 const skew = this.get('skew') || [];
58
59 this.mark_data.forEach((d, i) => {
60 d.skew = skew[i];
61 });
62 }
63
64 update_unique_ids() {
65 let names = this.get('names');
66 const show_labels = names != null && names.length !== 0;
67 names = show_labels
68 ? names
69 : this.mark_data.map((data, index) => {
70 return 'Dot' + index;
71 });
72 let unique_ids = [];
73 if (this.get('names_unique')) {
74 unique_ids = names.slice(0);
75 } else {
76 unique_ids = _.range(this.mark_data.length);
77 }
78
79 this.mark_data.forEach((data, index) => {
80 data.name = names[index];
81 data.unique_id = unique_ids[index];
82 });
83 }
84
85 static serializers = {
86 ...ScatterBaseModel.serializers,
87 skew: serialize.array_or_json_serializer,
88 names: serialize.array_or_json_serializer,
89 };
90 }