| 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 widgets from '@jupyter-widgets/base'; |
| 17 |
import { semver_range } from './version'; |
| 18 |
import * as serialize from './serialize'; |
| 19 |
|
| 20 |
export class MarkModel extends widgets.WidgetModel { |
| 21 |
defaults() { |
| 22 |
return { |
| 23 |
...widgets.WidgetModel.prototype.defaults(), |
| 24 |
_model_name: 'MarkModel', |
| 25 |
_model_module: 'bqplot', |
| 26 |
_view_module: 'bqplot', |
| 27 |
_model_module_version: semver_range, |
| 28 |
_view_module_version: semver_range, |
| 29 |
|
| 30 |
scales: {}, |
| 31 |
scales_metadata: {}, |
| 32 |
preserve_domain: {}, |
| 33 |
display_legend: false, |
| 34 |
labels: [], |
| 35 |
apply_clip: true, |
| 36 |
visible: true, |
| 37 |
selected_style: {}, |
| 38 |
unselected_style: {}, |
| 39 |
selected: null, |
| 40 |
enable_hover: true, |
| 41 |
tooltip: null, |
| 42 |
tooltip_style: { opacity: 0.9 }, |
| 43 |
interactions: { hover: 'tooltip' }, |
| 44 |
tooltip_location: 'mouse', |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
// These two attributes are the pixel values which should be appended |
| 49 |
// to the area of the plot to make sure that the entire mark is visible |
| 50 |
initialize(attributes, options) { |
| 51 |
super.initialize(attributes, options); |
| 52 |
this.on('change:scales', this.update_scales, this); |
| 53 |
this.once('destroy', this.handle_destroy, this); |
| 54 |
// `this.dirty` is set to `true` before starting computations that |
| 55 |
// might lead the state of the model to be temporarily inconsistent. |
| 56 |
// certain functions of views on that model might check the value |
| 57 |
// of `this.dirty` before rendering |
| 58 |
this.dirty = false; |
| 59 |
this.update_scales(); |
| 60 |
} |
| 61 |
|
| 62 |
update_data() { |
| 63 |
// Update_data is typically overloaded in each mark |
| 64 |
// it triggers the "data_updated" event |
| 65 |
this.update_domains(); |
| 66 |
this.trigger('data_updated'); |
| 67 |
} |
| 68 |
|
| 69 |
update_domains() { |
| 70 |
// update_domains is typically overloaded in each mark to update |
| 71 |
// the domains related to its scales |
| 72 |
} |
| 73 |
|
| 74 |
update_scales() { |
| 75 |
this.unregister_all_scales(this.previous('scales')); |
| 76 |
this.trigger('scales_updated'); |
| 77 |
this.update_domains(); |
| 78 |
} |
| 79 |
|
| 80 |
unregister_all_scales(scales) { |
| 81 |
// disassociates the mark with the scale |
| 82 |
this.dirty = true; |
| 83 |
for (const key in scales) { |
| 84 |
scales[key].del_domain([], this.model_id + '_' + key); |
| 85 |
} |
| 86 |
this.dirty = false; |
| 87 |
//TODO: Check if the views are being removed |
| 88 |
} |
| 89 |
|
| 90 |
handle_destroy() { |
| 91 |
this.unregister_all_scales(this.get('scales')); |
| 92 |
} |
| 93 |
|
| 94 |
get_key_for_dimension(dimension) { |
| 95 |
const scales_metadata = this.get('scales_metadata'); |
| 96 |
for (const scale in scales_metadata) { |
| 97 |
if (scales_metadata[scale].dimension === dimension) { |
| 98 |
return scale; |
| 99 |
} |
| 100 |
} |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
get_key_for_orientation(orientation) { |
| 105 |
const scales_metadata = this.get('scales_metadata'); |
| 106 |
for (const scale in scales_metadata) { |
| 107 |
if (scales_metadata[scale].orientation === orientation) { |
| 108 |
return scale; |
| 109 |
} |
| 110 |
} |
| 111 |
return null; |
| 112 |
} |
| 113 |
|
| 114 |
// TODO make this abstract |
| 115 |
get_data_dict(data, index) { |
| 116 |
return data; |
| 117 |
} |
| 118 |
|
| 119 |
static serializers = { |
| 120 |
...widgets.WidgetModel.serializers, |
| 121 |
scales: { deserialize: widgets.unpack_models }, |
| 122 |
tooltip: { deserialize: widgets.unpack_models }, |
| 123 |
selected: serialize.array_or_json_serializer, |
| 124 |
}; |
| 125 |
|
| 126 |
dirty: boolean; |
| 127 |
mark_data: any; |
| 128 |
} |