Debian Codegraph @

js/src/LinearScaleModel.ts

raw
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 const min = !this.min_from_data
82 ? this.min
83 : d3.min(
84 _.map(this.domains, (d: any[]) => {
85 return d.length > 0 ? d[0] : that.global_max;
86 })
87 );
88 const max = !this.max_from_data
89 ? this.max
90 : d3.max(
91 _.map(this.domains, (d: any[]) => {
92 return d.length > 1 ? d[1] : that.global_min;
93 })
94 );
95 const mid = (min + max) * 0.5;
96 const new_width = ((max - min) * 0.5) / this.get('mid_range');
97 const prev_domain = this.domain;
98 const min_index = this.reverse ? 1 : 0;
99 const prev_min = prev_domain[min_index];
100 const prev_max = prev_domain[1 - min_index];
101 const prev_mid = (prev_max + prev_min) * 0.5;
102 const min_width = (prev_max - prev_min) * 0.5 * this.get('min_range');
103
104 const stabilized = this.get('stabilized');
105
106 // If the scale is stabilized, only update if the new min/max is without
107 // a certain range, else update as soon as the new min/max is different.
108 const update_domain = stabilized
109 ? !(min >= prev_min) ||
110 !(min <= prev_mid - min_width) ||
111 !(max <= prev_max) ||
112 !(max >= prev_mid + min_width)
113 : min !== prev_min || max !== prev_max;
114
115 if (update_domain) {
116 const new_min = stabilized ? mid - new_width : min;
117 const new_max = stabilized ? mid + new_width : max;
118 this.domain = this.reverse ? [new_max, new_min] : [new_min, new_max];
119 this.trigger('domain_changed', this.domain);
120 }
121 }
122
123 compute_and_set_domain(data_array, id) {
124 // Takes an array and calculates the domain for the particular
125 // view. If you have the domain already calculated on your side,
126 // call set_domain function.
127 if (!data_array || data_array.length === 0) {
128 this.set_domain([], id);
129 return;
130 }
131 const data = data_array[0] instanceof Array ? data_array : [data_array];
132 const min = d3.min(
133 data.map((d) => {
134 return d3.min(d);
135 })
136 );
137 const max = d3.max(
138 data.map((d) => {
139 return d3.max(d);
140 })
141 );
142 this.set_domain([min, max], id);
143 }
144
145 type: string;
146 min: number | Date;
147 max: number | Date;
148 min_from_data: boolean;
149 max_from_data: boolean;
150 global_min: number | Date;
151 global_max: number | Date;
152 }