| 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 |
interface HeatMapData { |
| 20 |
x: number[]; |
| 21 |
y: number[]; |
| 22 |
color: number[][]; |
| 23 |
} |
| 24 |
|
| 25 |
export class HeatMapModel extends MarkModel { |
| 26 |
defaults() { |
| 27 |
return { |
| 28 |
...MarkModel.prototype.defaults(), |
| 29 |
_model_name: 'HeatMapModel', |
| 30 |
_view_name: 'HeatMap', |
| 31 |
x: [], |
| 32 |
y: [], |
| 33 |
color: null, |
| 34 |
scales_metadata: { |
| 35 |
x: { orientation: 'horizontal', dimension: 'x' }, |
| 36 |
y: { orientation: 'vertical', dimension: 'y' }, |
| 37 |
color: { dimension: 'color' }, |
| 38 |
}, |
| 39 |
null_color: 'black', |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
initialize(attributes, options) { |
| 44 |
super.initialize(attributes, options); |
| 45 |
|
| 46 |
this.on_some_change(['x', 'y', 'color'], this.update_data, this); |
| 47 |
this.on_some_change(['preserve_domain'], this.update_domains, this); |
| 48 |
this.update_data(); |
| 49 |
this.update_domains(); |
| 50 |
} |
| 51 |
|
| 52 |
update_data() { |
| 53 |
this.dirty = true; |
| 54 |
// Handling data updates |
| 55 |
this.mark_data = { |
| 56 |
x: this.get('x'), |
| 57 |
y: this.get('y'), |
| 58 |
color: this.get('color'), |
| 59 |
}; |
| 60 |
this.update_domains(); |
| 61 |
this.dirty = false; |
| 62 |
this.trigger('data_updated'); |
| 63 |
} |
| 64 |
|
| 65 |
update_domains() { |
| 66 |
if (!this.mark_data) return; |
| 67 |
|
| 68 |
const scales = this.get('scales'); |
| 69 |
const flat_colors = [].concat.apply( |
| 70 |
[], |
| 71 |
this.mark_data.color.map((x) => Array.prototype.slice.call(x, 0)) |
| 72 |
); |
| 73 |
|
| 74 |
if (!this.get('preserve_domain').x) { |
| 75 |
scales.x.compute_and_set_domain(this.mark_data.x, this.model_id + '_x'); |
| 76 |
} else { |
| 77 |
scales.x.del_domain([], this.model_id + '_x'); |
| 78 |
} |
| 79 |
|
| 80 |
if (!this.get('preserve_domain').y) { |
| 81 |
scales.y.compute_and_set_domain(this.mark_data.y, this.model_id + '_y'); |
| 82 |
} else { |
| 83 |
scales.y.del_domain([], this.model_id + '_y'); |
| 84 |
} |
| 85 |
if (scales.color !== null && scales.color !== undefined) { |
| 86 |
if (!this.get('preserve_domain').color) { |
| 87 |
scales.color.compute_and_set_domain( |
| 88 |
flat_colors, |
| 89 |
this.model_id + '_color' |
| 90 |
); |
| 91 |
} else { |
| 92 |
scales.color.del_domain([], this.model_id + '_color'); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
mark_data: HeatMapData; |
| 98 |
|
| 99 |
static serializers = { |
| 100 |
...MarkModel.serializers, |
| 101 |
x: serialize.array_or_json_serializer, |
| 102 |
y: serialize.array_or_json_serializer, |
| 103 |
color: serialize.array_or_json_serializer, |
| 104 |
}; |
| 105 |
} |