| 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")); |
| 19 |
import { ScaleModel } from './ScaleModel'; |
| 20 |
|
| 21 |
export class LinearScaleModel extends ScaleModel { |
| 22 |
defaults() { |
| 23 |
return { |
| 24 |
...ScaleModel.prototype.defaults(), |
| 25 |
_model_name: 'LinearScaleModel', |
| 26 |
_view_name: 'LinearScale', |
| 27 |
min: null, |
| 28 |
max: null, |
| 29 |
min_range: 0.6, |
| 30 |
mid_range: 0.8, |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
typedRange(values) { |
| 35 |
return new Float64Array(values.map(Number)); |
| 36 |
} |
| 37 |
|
| 38 |
set_init_state() { |
| 39 |
this.type = 'linear'; |
| 40 |
this.global_min = Number.NEGATIVE_INFINITY; |
| 41 |
this.global_max = Number.POSITIVE_INFINITY; |
| 42 |
} |
| 43 |
|
| 44 |
set_listeners() { |
| 45 |
this.on('change:reverse', this.reverse_changed, this); |
| 46 |
this.reverse_changed(undefined, undefined, undefined); |
| 47 |
this.on_some_change(['min', 'max'], this.min_max_changed, this); |
| 48 |
this.min_max_changed(); |
| 49 |
this.on_some_change( |
| 50 |
['min_range', 'mid_range', 'stabilized'], |
| 51 |
this.update_domain, |
| 52 |
this |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
min_max_changed() { |
| 57 |
this.min = this.get('min'); |
| 58 |
this.max = this.get('max'); |
| 59 |
this.min_from_data = this.min === null; |
| 60 |
this.max_from_data = this.max === null; |
| 61 |
this.update_domain(); |
| 62 |
} |
| 63 |
|
| 64 |
reverse_changed(model, value, options) { |
| 65 |
const prev_reverse = |
| 66 |
model === undefined ? false : model.previous('reverse'); |
| 67 |
this.reverse = this.get('reverse'); |
| 68 |
|
| 69 |
// the domain should be reversed only if the previous value of reverse |
| 70 |
// is different from the current value. During init, domain should be |
| 71 |
// reversed only if reverse is set to True. |
| 72 |
const reverse_domain = (prev_reverse + this.reverse) % 2; |
| 73 |
if (this.domain.length > 0 && reverse_domain === 1) { |
| 74 |
this.domain.reverse(); |
| 75 |
this.trigger('domain_changed', this.domain); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
update_domain() { |
| 80 |
const that = this; |
| 81 |
// if all domains are empty, and min or max are not set, we reset to the default domain of (0, 1) |
| 82 |
if ( |
| 83 |
(this.min_from_data || this.max_from_data) && |
| 84 |
_.every(this.domains, (d) => { |
| 85 |
return d.length === 0; |
| 86 |
}) |
| 87 |
) { |
| 88 |
this.domain = this.reverse ? [1, 0] : [0, 1]; |
| 89 |
this.trigger('domain_changed', this.domain); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
const min = !this.min_from_data |
| 94 |
? this.min |
| 95 |
: d3.min( |
| 96 |
_.map(this.domains, (d: any[]) => { |
| 97 |
return d.length > 0 ? d[0] : that.global_max; |
| 98 |
}) |
| 99 |
); |
| 100 |
const max = !this.max_from_data |
| 101 |
? this.max |
| 102 |
: d3.max( |
| 103 |
_.map(this.domains, (d: any[]) => { |
| 104 |
return d.length > 1 ? d[1] : that.global_min; |
| 105 |
}) |
| 106 |
); |
| 107 |
const mid = (min + max) * 0.5; |
| 108 |
const new_width = ((max - min) * 0.5) / this.get('mid_range'); |
| 109 |
const prev_domain = this.domain; |
| 110 |
const min_index = this.reverse ? 1 : 0; |
| 111 |
const prev_min = prev_domain[min_index]; |
| 112 |
const prev_max = prev_domain[1 - min_index]; |
| 113 |
const prev_mid = (prev_max + prev_min) * 0.5; |
| 114 |
const min_width = (prev_max - prev_min) * 0.5 * this.get('min_range'); |
| 115 |
|
| 116 |
const stabilized = this.get('stabilized'); |
| 117 |
|
| 118 |
// If the scale is stabilized, only update if the new min/max is without |
| 119 |
// a certain range, else update as soon as the new min/max is different. |
| 120 |
const update_domain = stabilized |
| 121 |
? !(min >= prev_min) || |
| 122 |
!(min <= prev_mid - min_width) || |
| 123 |
!(max <= prev_max) || |
| 124 |
!(max >= prev_mid + min_width) |
| 125 |
: min !== prev_min || max !== prev_max; |
| 126 |
|
| 127 |
if (update_domain) { |
| 128 |
const new_min = stabilized ? mid - new_width : min; |
| 129 |
const new_max = stabilized ? mid + new_width : max; |
| 130 |
this.domain = this.reverse ? [new_max, new_min] : [new_min, new_max]; |
| 131 |
this.trigger('domain_changed', this.domain); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
compute_and_set_domain(data_array, id) { |
| 136 |
// Takes an array and calculates the domain for the particular |
| 137 |
// view. If you have the domain already calculated on your side, |
| 138 |
// call set_domain function. |
| 139 |
if (!data_array || data_array.length === 0) { |
| 140 |
this.set_domain([], id); |
| 141 |
return; |
| 142 |
} |
| 143 |
const data = data_array[0] instanceof Array ? data_array : [data_array]; |
| 144 |
const min = d3.min( |
| 145 |
data.map((d) => { |
| 146 |
return d3.min(d); |
| 147 |
}) |
| 148 |
); |
| 149 |
const max = d3.max( |
| 150 |
data.map((d) => { |
| 151 |
return d3.max(d); |
| 152 |
}) |
| 153 |
); |
| 154 |
this.set_domain([min, max], id); |
| 155 |
} |
| 156 |
|
| 157 |
type: string; |
| 158 |
min: number | Date; |
| 159 |
max: number | Date; |
| 160 |
min_from_data: boolean; |
| 161 |
max_from_data: boolean; |
| 162 |
global_min: number | Date; |
| 163 |
global_max: number | Date; |
| 164 |
} |