| 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 { MarkModel } from './MarkModel'; |
| 17 |
import * as serialize from './serialize'; |
| 18 |
|
| 19 |
export class ScatterBaseModel extends MarkModel { |
| 20 |
defaults() { |
| 21 |
return { |
| 22 |
...MarkModel.prototype.defaults(), |
| 23 |
_model_name: 'ScatterBaseModel', |
| 24 |
_view_name: 'ScatterBase', |
| 25 |
|
| 26 |
x: [], |
| 27 |
y: [], |
| 28 |
color: null, |
| 29 |
opacity: null, |
| 30 |
size: null, |
| 31 |
rotation: null, |
| 32 |
hovered_point: null, |
| 33 |
scales_metadata: { |
| 34 |
x: { orientation: 'horizontal', dimension: 'x' }, |
| 35 |
y: { orientation: 'vertical', dimension: 'y' }, |
| 36 |
color: { dimension: 'color' }, |
| 37 |
size: { dimension: 'size' }, |
| 38 |
opacity: { dimension: 'opacity' }, |
| 39 |
rotation: { dimension: 'rotation' }, |
| 40 |
}, |
| 41 |
label_display_horizontal_offset: null, |
| 42 |
label_display_vertical_offset: null, |
| 43 |
hovered_style: {}, |
| 44 |
unhovered_style: {}, |
| 45 |
colors: ['steelblue'], |
| 46 |
opacities: [1.0], |
| 47 |
enable_move: false, |
| 48 |
enable_delete: false, |
| 49 |
restrict_x: false, |
| 50 |
restrict_y: false, |
| 51 |
update_on_move: false, |
| 52 |
}; |
| 53 |
} |
| 54 |
|
| 55 |
initialize(attributes, options) { |
| 56 |
// TODO: Normally, color, opacity and size should not require a redraw |
| 57 |
super.initialize(attributes, options); |
| 58 |
this.on_some_change( |
| 59 |
['x', 'y', 'color', 'opacity', 'size', 'rotation'], |
| 60 |
this.update_data, |
| 61 |
this |
| 62 |
); |
| 63 |
// FIXME: replace this with on("change:preserve_domain"). It is not done here because |
| 64 |
// on_some_change depends on the GLOBAL backbone on("change") handler which |
| 65 |
// is called AFTER the specific handlers on("change:foobar") and we make that |
| 66 |
// assumption. |
| 67 |
this.on_some_change(['preserve_domain'], this.update_domains, this); |
| 68 |
this.update_data(); |
| 69 |
} |
| 70 |
|
| 71 |
update_mark_data() { |
| 72 |
let x_data = this.get('x'); |
| 73 |
const y_data = this.get('y'); |
| 74 |
|
| 75 |
if (x_data.length === 0 || y_data.length === 0) { |
| 76 |
this.mark_data = []; |
| 77 |
} else { |
| 78 |
//FIXME:Temporary fix to avoid misleading NaN error due to X and Y |
| 79 |
//being of different lengths. In particular, if Y is of a smaller |
| 80 |
//length, throws an error on the JS side |
| 81 |
const min_len = Math.min(x_data.length, y_data.length); |
| 82 |
x_data = x_data.slice(0, min_len); |
| 83 |
const color = this.get('color') || [], |
| 84 |
size = this.get('size') || [], |
| 85 |
opacity = this.get('opacity') || [], |
| 86 |
rotation = this.get('rotation') || []; |
| 87 |
|
| 88 |
// since x_data may be a TypedArray, explicitly use Array.map |
| 89 |
this.mark_data = Array.prototype.map.call(x_data, (d, i) => { |
| 90 |
return { |
| 91 |
x: d, |
| 92 |
y: y_data[i], |
| 93 |
color: color[i], |
| 94 |
size: size[i], |
| 95 |
opacity: opacity[i], |
| 96 |
rotation: rotation[i], |
| 97 |
index: i, |
| 98 |
}; |
| 99 |
}); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
update_data() { |
| 104 |
this.dirty = true; |
| 105 |
this.update_mark_data(); |
| 106 |
this.update_unique_ids(); |
| 107 |
this.update_domains(); |
| 108 |
this.dirty = false; |
| 109 |
this.trigger('data_updated'); |
| 110 |
} |
| 111 |
|
| 112 |
update_unique_ids() {} |
| 113 |
|
| 114 |
get_data_dict(data, index) { |
| 115 |
return data; |
| 116 |
} |
| 117 |
|
| 118 |
update_domains() { |
| 119 |
if (!this.mark_data) { |
| 120 |
return; |
| 121 |
} |
| 122 |
// color scale needs an issue in DateScaleModel to be fixed. It |
| 123 |
// should be moved here as soon as that is fixed. |
| 124 |
|
| 125 |
const scales = this.get('scales'); |
| 126 |
for (const key in scales) { |
| 127 |
if (scales.hasOwnProperty(key)) { |
| 128 |
const scale = scales[key]; |
| 129 |
if (!this.get('preserve_domain')[key]) { |
| 130 |
scale.compute_and_set_domain( |
| 131 |
this.mark_data.map((elem) => { |
| 132 |
return elem[key]; |
| 133 |
}), |
| 134 |
this.model_id + key |
| 135 |
); |
| 136 |
} else { |
| 137 |
scale.del_domain([], this.model_id + key); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static serializers = { |
| 144 |
...MarkModel.serializers, |
| 145 |
x: serialize.array_or_json_serializer, |
| 146 |
y: serialize.array_or_json_serializer, |
| 147 |
color: serialize.array_or_json_serializer, |
| 148 |
opacity: serialize.array_or_json_serializer, |
| 149 |
size: serialize.array_or_json_serializer, |
| 150 |
rotation: serialize.array_or_json_serializer, |
| 151 |
opacities: serialize.array_or_json_serializer, |
| 152 |
}; |
| 153 |
} |