Debian Codegraph @

js/src/ColorScale.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 d3 from 'd3';
17 // var d3 =Object.assign({}, require("d3-scale"));
18 import { Scale } from './Scale';
19 import { ColorScaleModel } from './ColorScaleModel';
20
21 export class ColorScale extends Scale {
22 render() {
23 this.create_d3_scale();
24 this.update_extrapolation();
25 if (this.model.domain.length > 0) {
26 this.scale.domain(this.model.domain);
27 }
28 this.offset = 0;
29 this.create_event_listeners();
30 this.set_range();
31 }
32
33 create_d3_scale() {
34 this.scale = d3.scaleLinear() as d3.ScaleLinear<number, number>;
35 }
36
37 create_event_listeners() {
38 super.create_event_listeners();
39 this.listenTo(this.model, 'colors_changed', this.set_range);
40 this.model.on(
41 'change:extrapolation',
42 function () {
43 this.update_extrapolation();
44 this.trigger('color_scale_range_changed');
45 },
46 this
47 );
48 }
49
50 update_extrapolation() {
51 this.scale.clamp(this.model.get('extrapolation') === 'constant');
52 }
53
54 set_range() {
55 this.scale.range(this.model.color_range);
56 this.trigger('color_scale_range_changed');
57 }
58
59 scale: d3.ScaleLinear<number, number> | d3.ScaleTime<Date, number>;
60 // Overriding super class
61 model: ColorScaleModel;
62 }
63
64 export function isColorScale(scale: Scale): scale is ColorScale {
65 return scale.model.type === 'color_linear';
66 }