| 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 topojson from 'topojson'; |
| 18 |
|
| 19 |
export class MapModel extends MarkModel { |
| 20 |
defaults() { |
| 21 |
return { |
| 22 |
...MarkModel.prototype.defaults(), |
| 23 |
_model_name: 'MapModel', |
| 24 |
_view_name: 'Map', |
| 25 |
|
| 26 |
color: {}, |
| 27 |
hover_highlight: true, |
| 28 |
hovered_styles: { |
| 29 |
hovered_fill: 'Orange', |
| 30 |
hovered_stroke: null, |
| 31 |
hovered_stroke_width: 2.0, |
| 32 |
}, |
| 33 |
|
| 34 |
stroke_color: null, |
| 35 |
default_color: null, |
| 36 |
scales_metadata: { |
| 37 |
color: { dimension: 'color' }, |
| 38 |
projection: { dimension: 'geo' }, |
| 39 |
}, |
| 40 |
selected_styles: { |
| 41 |
selected_fill: 'Red', |
| 42 |
selected_stroke: null, |
| 43 |
selected_stroke_width: 2.0, |
| 44 |
}, |
| 45 |
map_data: undefined, |
| 46 |
}; |
| 47 |
} |
| 48 |
|
| 49 |
initialize(attributes, options) { |
| 50 |
super.initialize(attributes, options); |
| 51 |
this.on('change:map_data', this.update_data, this); |
| 52 |
this.on('change:color', this.color_data_updated, this); |
| 53 |
this.update_data(); |
| 54 |
this.update_domains(); |
| 55 |
} |
| 56 |
|
| 57 |
update_data() { |
| 58 |
this.dirty = true; |
| 59 |
const data = this.get('map_data'); |
| 60 |
if (data.type == 'Topology') { |
| 61 |
this.geodata = topojson.feature(data, data.objects.subunits).features; |
| 62 |
} else { |
| 63 |
this.geodata = data.features; |
| 64 |
} |
| 65 |
this.color_data_updated(); |
| 66 |
this.dirty = false; |
| 67 |
this.trigger('data_updated'); |
| 68 |
} |
| 69 |
|
| 70 |
update_properties(d) { |
| 71 |
if (!d.properties) { |
| 72 |
d.properties = { color: this.color_data[d.id] }; |
| 73 |
} else { |
| 74 |
d.properties.color = this.color_data[d.id]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
color_data_updated() { |
| 79 |
this.update_domains(); |
| 80 |
this.geodata.map((d) => { |
| 81 |
return this.update_properties(d); |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
update_domains() { |
| 86 |
const scales = this.get('scales'); |
| 87 |
const color_scale = scales.color; |
| 88 |
this.color_data = this.get('color'); |
| 89 |
if (color_scale !== null && color_scale !== undefined) { |
| 90 |
if (!this.get('preserve_domain').color) { |
| 91 |
color_scale.compute_and_set_domain( |
| 92 |
Object.keys(this.color_data).map((d) => { |
| 93 |
return this.color_data[d]; |
| 94 |
}), |
| 95 |
this.model_id + '_color' |
| 96 |
); |
| 97 |
} else { |
| 98 |
color_scale.del_domain([], this.model_id + '_color'); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
get_data_dict(data, index) { |
| 104 |
return { ...data.properties, id: data.id }; |
| 105 |
} |
| 106 |
|
| 107 |
geodata: any[]; |
| 108 |
color_data: { [key: string]: number }; |
| 109 |
} |