| 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 { ScaleModel } from './ScaleModel'; |
| 17 |
import * as _ from 'underscore'; |
| 18 |
|
| 19 |
export class OrdinalScaleModel extends ScaleModel { |
| 20 |
defaults() { |
| 21 |
return { |
| 22 |
...ScaleModel.prototype.defaults(), |
| 23 |
_model_name: 'OrdinalScaleModel', |
| 24 |
_view_name: 'OrdinalScale', |
| 25 |
domain: [], |
| 26 |
}; |
| 27 |
} |
| 28 |
|
| 29 |
typedRange(values) { |
| 30 |
return values; |
| 31 |
} |
| 32 |
|
| 33 |
initialize(attributes, options) { |
| 34 |
super.initialize(attributes, options); |
| 35 |
} |
| 36 |
|
| 37 |
set_init_state() { |
| 38 |
this.type = 'ordinal'; |
| 39 |
this.min_from_data = true; |
| 40 |
this.max_from_data = true; |
| 41 |
} |
| 42 |
|
| 43 |
set_listeners() { |
| 44 |
this.on('change:domain', this.domain_changed, this); |
| 45 |
this.domain_changed(); |
| 46 |
this.on('change:reverse', this.reverse_changed, this); |
| 47 |
this.reverse_changed(); |
| 48 |
} |
| 49 |
|
| 50 |
domain_changed() { |
| 51 |
this.ord_domain = this.get('domain'); |
| 52 |
if (this.ord_domain !== null && this.ord_domain.length !== 0) { |
| 53 |
this.max_from_data = false; |
| 54 |
this.min_from_data = false; |
| 55 |
this.domain = this.ord_domain.map((d) => { |
| 56 |
return d; |
| 57 |
}); |
| 58 |
this.trigger('domain_changed'); |
| 59 |
} else { |
| 60 |
this.max_from_data = true; |
| 61 |
this.min_from_data = true; |
| 62 |
this.domain = []; |
| 63 |
this.update_domain(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
reverse_changed(model?) { |
| 68 |
const prev_reverse = |
| 69 |
model === undefined ? false : model.previous('reverse'); |
| 70 |
this.reverse = this.get('reverse'); |
| 71 |
|
| 72 |
// the domain should be reversed only if the previous value of reverse |
| 73 |
// is different from the current value. During init, domain should be |
| 74 |
// reversed only if reverse is set to True. |
| 75 |
const reverse_domain = (prev_reverse + this.reverse) % 2; |
| 76 |
if (this.domain.length > 0 && reverse_domain === 1) { |
| 77 |
this.domain.reverse(); |
| 78 |
this.trigger('domain_changed', this.domain); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
update_domain() { |
| 83 |
let domain = []; |
| 84 |
// TODO: check for hasOwnProperty |
| 85 |
for (const id in this.domains) { |
| 86 |
domain = _.union(domain, this.domains[id]); |
| 87 |
} |
| 88 |
if ( |
| 89 |
this.domain.length !== domain.length || |
| 90 |
_.intersection(this.domain, domain).length !== domain.length |
| 91 |
) { |
| 92 |
this.domain = domain; |
| 93 |
this.trigger('domain_changed', domain); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
compute_and_set_domain(data_array, id) { |
| 98 |
// Takes an array and calculates the domain for the particular |
| 99 |
// view. If you have the domain already calculated on your side, |
| 100 |
// call set_domain function. |
| 101 |
if (!this.min_from_data && !this.max_from_data) { |
| 102 |
return; |
| 103 |
} |
| 104 |
if (data_array.length === 0) { |
| 105 |
this.set_domain([], id); |
| 106 |
return; |
| 107 |
} |
| 108 |
const domain = _.flatten(data_array); |
| 109 |
if (this.get('reverse')) { |
| 110 |
domain.reverse(); |
| 111 |
} |
| 112 |
this.set_domain(domain, id); |
| 113 |
} |
| 114 |
|
| 115 |
min_from_data: boolean; |
| 116 |
max_from_data: boolean; |
| 117 |
ord_domain: any; |
| 118 |
} |