| 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 |
import { ScatterBaseModel } from './ScatterBaseModel'; |
| 18 |
import * as serialize from './serialize'; |
| 19 |
|
| 20 |
export class LabelModel extends ScatterBaseModel { |
| 21 |
defaults() { |
| 22 |
return { |
| 23 |
...ScatterBaseModel.prototype.defaults(), |
| 24 |
_model_name: 'LabelModel', |
| 25 |
_view_name: 'Label', |
| 26 |
|
| 27 |
x_offset: 0, |
| 28 |
y_offset: 0, |
| 29 |
colors: d3.scaleOrdinal(d3.schemeCategory10).range(), |
| 30 |
rotate_angle: 0.0, |
| 31 |
text: null, |
| 32 |
default_size: 16.0, |
| 33 |
drag_size: 1.0, |
| 34 |
font_unit: 'px', |
| 35 |
font_weight: 'bold', |
| 36 |
align: 'start', |
| 37 |
}; |
| 38 |
} |
| 39 |
|
| 40 |
initialize(attributes, options) { |
| 41 |
// TODO: Normally, color, opacity and size should not require a redraw |
| 42 |
super.initialize(attributes, options); |
| 43 |
this.on('change:text', this.update_data, this); |
| 44 |
} |
| 45 |
|
| 46 |
update_mark_data() { |
| 47 |
super.update_mark_data(); |
| 48 |
const text = this.get('text'); |
| 49 |
|
| 50 |
this.mark_data.forEach((d, i) => { |
| 51 |
d.text = text[i]; |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
update_unique_ids() { |
| 56 |
this.mark_data.forEach((data, index) => { |
| 57 |
data.unique_id = 'Label' + index; |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
static serializers = { |
| 62 |
...ScatterBaseModel.serializers, |
| 63 |
text: serialize.array_or_json_serializer, |
| 64 |
}; |
| 65 |
} |