Debian Codegraph @

bqplot/js/src/LinearScale.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 d3Scale from 'd3-scale';
17 import { Scale } from './Scale';
18 import { LinearScaleModel } from './LinearScaleModel';
19
20 export class LinearScale extends Scale {
21 render() {
22 this.scale = d3Scale.scaleLinear() as d3.ScaleLinear<number, number>;
23 if (this.model.domain.length > 0) {
24 this.scale.domain(this.model.domain);
25 }
26 this.offset = 0;
27 this.create_event_listeners();
28 }
29
30 expand_domain(old_range, new_range) {
31 // If you have a current range and then a new range and want to
32 // expand the domain to expand to the new range but keep it
33 // consistent with the previous one, this is the function you use.
34
35 // The following code is required to make a copy of the actual
36 // state of the scale. Referring to the model domain and then
37 // setting the range to be the old range in case it is not.
38 const unpadded_scale = this.scale.copy();
39
40 // To handle the case for a clamped scale for which we have to
41 // expand the domain, the copy should be unclamped.
42 unpadded_scale.clamp(false);
43 if (this.model.domain.length) {
44 unpadded_scale.domain(this.model.domain);
45 } else {
46 // if the domain is empty, it will lead to NaN/NaN
47 console.error(
48 'No domain exists for scale, is that any data associated with this scale?'
49 );
50 }
51 unpadded_scale.range(old_range);
52 this.scale.domain(
53 new_range.map((limit) => {
54 return unpadded_scale.invert(limit);
55 })
56 );
57 }
58
59 invert(pixel): number | Date {
60 return this.scale.invert(pixel);
61 }
62
63 invert_range(pixels) {
64 //Pixels is a non-decreasing array of pixel values
65 return pixels.map((pix) => this.invert(pix));
66 }
67
68 scale:
69 | d3.ScaleLinear<number, number>
70 | d3.ScaleTime<Date, number>
71 | d3.ScaleLogarithmic<number, number>;
72
73 model: LinearScaleModel;
74 }
75
76 export function isLinearScale(scale: Scale): scale is LinearScale {
77 return scale.model.type === 'linear';
78 }