Debian Codegraph @

js/src/OrdinalColorScale.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 * as colurutils from './ColorUtils';
20
21 export class OrdinalColorScale extends Scale {
22 render() {
23 this.scale = d3.scaleOrdinal();
24 this.scale.domain(this.model.domain);
25 this.create_event_listeners();
26 this.listenTo(this.model, 'domain_changed', this.model_domain_changed);
27 this.model.on_some_change(['colors', 'scheme'], this.colors_changed, this);
28 this.set_range();
29 }
30
31 set_range() {
32 if (this.model.get('colors').length > 0) {
33 this.scale.range(
34 colurutils.cycle_colors(
35 this.model.get('colors'),
36 this.scale.domain().length
37 )
38 );
39 } else {
40 this.scale.range(
41 colurutils.get_ordinal_scale_range(
42 this.model.get('scheme'),
43 this.scale.domain().length
44 )
45 );
46 }
47 this.trigger('color_scale_range_changed');
48 }
49
50 model_domain_changed() {
51 super.model_domain_changed();
52 this.set_range();
53 }
54
55 colors_changed() {
56 this.set_range();
57 }
58
59 scale: d3.ScaleOrdinal<string, number>;
60 }