Debian Codegraph @

bqplot/js/src/GridHeatMapModel.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 _ from 'underscore';
17 import { MarkModel } from './MarkModel';
18 import * as serialize from './serialize';
19
20 export interface GridCellData {
21 rowNum: number;
22 row: number;
23 column: number;
24 columnNum: number;
25 color: number;
26 cellNum: number;
27 }
28
29 export enum AxisMode {
30 Middle,
31 Boundaries,
32 ExpandOne,
33 ExpandTwo,
34 }
35
36 export class GridHeatMapModel extends MarkModel {
37 defaults() {
38 return {
39 ...MarkModel.prototype.defaults(),
40 _model_name: 'GridHeatMapModel',
41 _view_name: 'GridHeatMap',
42 row: null,
43 column: null,
44 color: null,
45 scales_metadata: {
46 row: { orientation: 'vertical', dimension: 'y' },
47 column: { orientation: 'horizontal', dimension: 'x' },
48 color: { dimension: 'color' },
49 },
50 row_align: 'start',
51 column_align: 'start',
52 null_color: 'black',
53 stroke: 'black',
54 opacity: 1.0,
55 anchor_style: {},
56 display_format: null,
57 font_style: {},
58 };
59 }
60
61 initialize(attributes, options) {
62 super.initialize(attributes, options);
63 this.on_some_change(['row', 'column', 'color'], this.update_data, this);
64 this.on_some_change(['preserve_domain'], this.update_domains, this);
65 this.update_data();
66 this.update_domains();
67 }
68
69 update_data() {
70 this.dirty = true;
71 // Handling data updates
72 const numCols = this.colors[0].length;
73
74 const flatColors = [].concat.apply(
75 [],
76 this.colors.map((x) => Array.prototype.slice.call(x, 0))
77 );
78
79 this.mark_data = flatColors.map((data, index) => {
80 const rowNum = Math.floor(index / numCols);
81 const colNum = index % numCols;
82
83 return {
84 rowNum: rowNum,
85 row: this.rows[rowNum],
86 column: this.columns[colNum],
87 columnNum: colNum,
88 color: data,
89 cellNum: index,
90 };
91 });
92 this.identifyModes();
93 this.update_domains();
94 this.dirty = false;
95 this.trigger('data_updated');
96 }
97
98 update_domains() {
99 if (!this.mark_data) {
100 return;
101 }
102 const scales = this.get('scales');
103 const y_scale = scales.row,
104 x_scale = scales.column;
105 const color_scale = scales.color;
106
107 if (!this.get('preserve_domain').row) {
108 y_scale.compute_and_set_domain(this.rows, this.model_id + '_row');
109 } else {
110 y_scale.del_domain([], this.model_id + '_row');
111 }
112
113 if (!this.get('preserve_domain').column) {
114 x_scale.compute_and_set_domain(this.columns, this.model_id + '_column');
115 } else {
116 x_scale.del_domain([], this.model_id + '_column');
117 }
118 if (color_scale !== null && color_scale !== undefined) {
119 if (!this.get('preserve_domain').color) {
120 color_scale.compute_and_set_domain(
121 this.mark_data.map((elem) => elem.color),
122 this.model_id + '_color'
123 );
124 } else {
125 color_scale.del_domain([], this.model_id + '_color');
126 }
127 }
128 }
129
130 get_data_dict(data, index) {
131 return data;
132 }
133
134 get colors(): number[][] {
135 if (this.get('color')) {
136 return this.get('color');
137 }
138
139 return [[]];
140 }
141
142 get rows(): number[] {
143 if (this.get('row')) {
144 return Array.from(this.get('row'));
145 }
146
147 return _.range(this.colors.length);
148 }
149
150 get columns(): number[] {
151 if (this.get('column')) {
152 return Array.from(this.get('column'));
153 }
154
155 return _.range(this.colors[0].length);
156 }
157
158 private identifyModes() {
159 //based on the data, identify the mode in which the heatmap should
160 //be plotted.
161 const scales = this.get('scales');
162 const rowScale = scales.row;
163 const columnScale = scales.column;
164 const nRows = this.colors.length;
165 const nColumns = this.colors[0].length;
166 this.modes = { column: AxisMode.Middle, row: AxisMode.Middle };
167
168 if (rowScale.type === 'ordinal') {
169 this.modes.row = AxisMode.Middle;
170 } else {
171 if (nRows === this.rows.length - 1) {
172 this.modes.row = AxisMode.Boundaries;
173 } else if (nRows === this.rows.length) {
174 this.modes.row = AxisMode.ExpandOne;
175 } else if (nRows === this.rows.length + 1) {
176 this.modes.row = AxisMode.ExpandTwo;
177 }
178 }
179 if (columnScale.type === 'ordinal') {
180 this.modes.column = AxisMode.Middle;
181 } else {
182 if (nColumns === this.columns.length - 1) {
183 this.modes.column = AxisMode.Boundaries;
184 } else if (nColumns === this.columns.length) {
185 this.modes.column = AxisMode.ExpandOne;
186 } else if (nColumns === this.columns.length + 1) {
187 this.modes.column = AxisMode.ExpandTwo;
188 }
189 }
190 }
191
192 static serializers = {
193 ...MarkModel.serializers,
194 row: serialize.array_or_json_serializer,
195 column: serialize.array_or_json_serializer,
196 color: serialize.array_or_json_serializer,
197 };
198
199 modes: { column: AxisMode; row: AxisMode };
200 mark_data: GridCellData[];
201 }