| 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 d3 from 'd3'; |
| 17 |
// var d3 =Object.assign({}, require("d3-scale")); |
| 18 |
import { Scale } from './Scale'; |
| 19 |
import * as _ from 'underscore'; |
| 20 |
|
| 21 |
export class OrdinalScale extends Scale { |
| 22 |
render() { |
| 23 |
this.scale = d3.scaleBand(); |
| 24 |
this.scale.domain(this.model.domain); |
| 25 |
this.offset = 0; |
| 26 |
this.create_event_listeners(); |
| 27 |
} |
| 28 |
|
| 29 |
set_range(range, padding) { |
| 30 |
padding = padding === undefined ? 0 : padding; |
| 31 |
this.scale.range(range); |
| 32 |
this.scale.paddingInner(padding); |
| 33 |
this.scale.paddingOuter(padding / 2.0); |
| 34 |
this.offset = |
| 35 |
this.scale.domain().length === 0 ? 0 : this.scale.bandwidth() / 2.0; |
| 36 |
} |
| 37 |
|
| 38 |
expand_domain(old_range, new_range) { |
| 39 |
// If you have a current range and then a new range and want to |
| 40 |
// expand the domain to expand to the new range but keep it |
| 41 |
// consistent with the previous one, this is the function you use. |
| 42 |
|
| 43 |
// I am trying to expand the ordinal scale by setting an |
| 44 |
// appropriate value for the outer padding of the ordinal scale so |
| 45 |
// that the starting point of each of the bins match. once that |
| 46 |
// happens, the labels are placed at the center of the bins |
| 47 |
|
| 48 |
const unpadded_scale = this.scale.copy(); |
| 49 |
unpadded_scale.range(old_range).paddingInner(0).paddingOuter(0); |
| 50 |
const outer_padding = |
| 51 |
unpadded_scale.range().length > 0 |
| 52 |
? Math.abs((new_range[1] - old_range[1]) / unpadded_scale.bandwidth()) |
| 53 |
: 0; |
| 54 |
this.scale.range(new_range); |
| 55 |
this.scale.paddingInner(0.0); |
| 56 |
this.scale.paddingOuter(outer_padding); |
| 57 |
} |
| 58 |
|
| 59 |
invert(pixel) { |
| 60 |
// returns the element in the domain which is closest to pixel |
| 61 |
// value passed. If the pixel is outside the range of the scale, |
| 62 |
const that = this; |
| 63 |
const domain = this.scale.domain(); |
| 64 |
const pixel_vals = domain.map((d) => { |
| 65 |
return that.scale(d) + that.scale.bandwidth() / 2; |
| 66 |
}); |
| 67 |
const abs_diff = pixel_vals.map((d) => { |
| 68 |
return Math.abs(pixel - d); |
| 69 |
}); |
| 70 |
return domain[abs_diff.indexOf(d3.min(abs_diff))]; |
| 71 |
} |
| 72 |
|
| 73 |
invert_range(pixels) { |
| 74 |
//return all the indices between a range |
| 75 |
//pixels should be a non-decreasing two element array |
| 76 |
const that = this; |
| 77 |
const domain = this.scale.domain(); |
| 78 |
const pixel_vals = domain.map((d) => { |
| 79 |
return that.scale(d) + that.scale.bandwidth() / 2; |
| 80 |
}); |
| 81 |
const indices = _.range(pixel_vals.length); |
| 82 |
const filtered_ind = indices.filter((ind) => { |
| 83 |
return pixel_vals[ind] >= pixels[0] && pixel_vals[ind] <= pixels[1]; |
| 84 |
}); |
| 85 |
return filtered_ind.map((ind) => { |
| 86 |
return domain[ind]; |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
scale: d3.ScaleBand<string>; |
| 91 |
} |
| 92 |
|
| 93 |
export function isOrdinalScale(scale: Scale): scale is OrdinalScale { |
| 94 |
return scale.model.type === 'ordinal'; |
| 95 |
} |