| 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 _ from 'underscore'; |
| 17 |
import * as d3 from 'd3'; |
| 18 |
// var d3 =Object.assign({}, require("d3-array"), require("d3-scale")); |
| 19 |
import { LinearScaleModel } from './LinearScaleModel'; |
| 20 |
import * as colorutils from './ColorUtils'; |
| 21 |
|
| 22 |
export class ColorScaleModel extends LinearScaleModel { |
| 23 |
defaults() { |
| 24 |
return { |
| 25 |
...LinearScaleModel.prototype.defaults(), |
| 26 |
_model_name: 'ColorScaleModel', |
| 27 |
_view_name: 'ColorScale', |
| 28 |
mid: null, |
| 29 |
scheme: 'RdYlGn', |
| 30 |
extrapolation: 'constant', |
| 31 |
colors: null, |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
set_init_state() { |
| 36 |
this.type = 'color_linear'; |
| 37 |
this.color_range = []; |
| 38 |
this.mid = null; |
| 39 |
} |
| 40 |
|
| 41 |
set_listeners() { |
| 42 |
super.set_listeners(); |
| 43 |
this.on_some_change(['colors', 'scheme'], this.colors_changed, this); |
| 44 |
this.on('change:mid', this.update_domain, this); |
| 45 |
this.colors_changed(); |
| 46 |
} |
| 47 |
|
| 48 |
update_domain() { |
| 49 |
// Compute domain min and max |
| 50 |
const that = this; |
| 51 |
const min = !this.min_from_data |
| 52 |
? this.min |
| 53 |
: d3.min( |
| 54 |
_.map(this.domains, (d: any[]) => { |
| 55 |
return d.length > 0 ? d[0] : that.global_max; |
| 56 |
}) |
| 57 |
); |
| 58 |
const max = !this.max_from_data |
| 59 |
? this.max |
| 60 |
: d3.max( |
| 61 |
_.map(this.domains, (d: any[]) => { |
| 62 |
return d.length > 0 ? d[d.length - 1] : that.global_min; |
| 63 |
}) |
| 64 |
); |
| 65 |
const prev_mid = this.mid; |
| 66 |
this.mid = this.get('mid'); |
| 67 |
|
| 68 |
// If the min/mid/max has changed, or the number of colors has changed, |
| 69 |
// update the domain |
| 70 |
const prev_domain = this.domain; |
| 71 |
const prev_length = prev_domain.length; |
| 72 |
const n_colors = this.color_range.length; |
| 73 |
|
| 74 |
if ( |
| 75 |
min != prev_domain[0] || |
| 76 |
max != prev_domain[prev_length - 1] || |
| 77 |
n_colors != prev_length || |
| 78 |
this.mid != prev_mid |
| 79 |
) { |
| 80 |
this.domain = this.create_domain(min, this.mid, max, n_colors); |
| 81 |
this.trigger('domain_changed', this.domain); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
create_domain(min, mid, max, n_colors) { |
| 86 |
// Domain ranges from min to max, with the same number of |
| 87 |
// elements as the color range |
| 88 |
const scale = d3.scaleLinear(); |
| 89 |
|
| 90 |
if (mid === undefined || mid === null) { |
| 91 |
scale.domain([0, n_colors - 1]).range([min, max]); |
| 92 |
} else { |
| 93 |
const mid_index = (n_colors - 1) / 2; |
| 94 |
scale.domain([0, mid_index, n_colors - 1]).range([min, mid, max]); |
| 95 |
} |
| 96 |
|
| 97 |
const domain = []; |
| 98 |
for (let i = 0; i < n_colors; i++) { |
| 99 |
const j = this.reverse ? n_colors - 1 - i : i; |
| 100 |
domain.push(this.toDomainType(scale(j))); |
| 101 |
} |
| 102 |
return domain; |
| 103 |
} |
| 104 |
|
| 105 |
colors_changed() { |
| 106 |
const colors = this.get('colors'); |
| 107 |
this.color_range = |
| 108 |
colors.length > 0 |
| 109 |
? colors |
| 110 |
: colorutils.get_linear_scale_range(this.get('scheme')); |
| 111 |
// If the number of colors has changed, the domain must be updated |
| 112 |
this.update_domain(); |
| 113 |
// Update the range of the views. For a color scale the range doesn't depend |
| 114 |
// on the view, so ideally we could get rid of this |
| 115 |
this.trigger('colors_changed'); |
| 116 |
} |
| 117 |
|
| 118 |
protected toDomainType(value: number): any { |
| 119 |
return value; |
| 120 |
} |
| 121 |
|
| 122 |
color_range: number[]; |
| 123 |
mid: number; |
| 124 |
reverse: boolean; |
| 125 |
} |