Debian Codegraph @

bqplot/js/src/HeatMap.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 import * as _ from 'underscore';
18 import { HeatMapModel } from './HeatMapModel';
19 import { Mark } from './Mark';
20
21 export class HeatMap extends Mark {
22 async render() {
23 const baseRenderPromise = super.render();
24
25 this.displayed.then(() => {
26 this.parent.tooltip_div.node().appendChild(this.tooltip_div.node());
27 this.create_tooltip();
28 });
29
30 this.image = d3
31 .select(this.el)
32 .append('image')
33 .classed('heatmap', true)
34 .attr('width', this.parent.width)
35 .attr('height', this.parent.height);
36
37 this.canvas = document.createElement('canvas');
38
39 await baseRenderPromise;
40
41 this.event_listeners = {};
42 this.process_interactions();
43 this.create_listeners();
44 this.compute_view_padding();
45 this.draw();
46 }
47
48 set_ranges() {
49 if (this.scales.x) {
50 const xRange = this.parent.padded_range('x', this.scales.x.model);
51 this.scales.x.set_range(xRange);
52 }
53
54 if (this.scales.y) {
55 this.scales.y.set_range(
56 this.parent.padded_range('y', this.scales.y.model)
57 );
58 }
59 }
60
61 set_positional_scales() {
62 this.listenTo(this.scales.x, 'domain_changed', () => {
63 if (!this.model.dirty) this.draw();
64 });
65 this.listenTo(this.scales.y, 'domain_changed', () => {
66 if (!this.model.dirty) this.draw();
67 });
68 }
69
70 initialize_additional_scales() {
71 if (this.scales.color) {
72 this.listenTo(this.scales.color, 'domain_changed', function () {
73 this.draw();
74 });
75 this.scales.color.on('color_scale_range_changed', this.draw, this);
76 }
77 }
78
79 create_listeners() {
80 super.create_listeners();
81
82 this.d3el
83 .on('mouseover', () => {
84 this.event_dispatcher('mouse_over');
85 })
86 .on('mousemove', () => {
87 this.event_dispatcher('mouse_move');
88 })
89 .on('mouseout', () => {
90 this.event_dispatcher('mouse_out');
91 });
92 this.listenTo(this.model, 'data_updated', this.draw);
93 this.listenTo(this.model, 'change:tooltip', this.create_tooltip);
94 this.listenTo(this.parent, 'bg_clicked', () => {
95 this.event_dispatcher('parent_clicked');
96 });
97 this.listenTo(this.model, 'change:interactions', this.process_interactions);
98 }
99
100 relayout() {
101 this.set_ranges();
102 this.compute_view_padding();
103 this.draw();
104 }
105
106 private drawCanvas() {
107 this.image.attr('href', this.canvas.toDataURL('image/png'));
108 }
109
110 draw() {
111 this.set_ranges();
112
113 const plottingData = this.getPlottingData();
114
115 this.canvas.setAttribute('width', plottingData.totalWidth.toString());
116 this.canvas.setAttribute('height', plottingData.totalHeight.toString());
117
118 const ctx = this.canvas.getContext('2d');
119 const colors = this.model.mark_data.color;
120 colors.forEach((row: number[], i: number) => {
121 const height = plottingData.heights[i];
122 const y = plottingData.yOrigin + plottingData.yStartPoints[i];
123
124 row.forEach((d: number, j: number) => {
125 const width = plottingData.widths[j];
126 const x = plottingData.xOrigin + plottingData.xStartPoints[j];
127 ctx.fillStyle = this.getElementFill(d);
128 ctx.fillRect(x, y, this.expandRect(width), this.expandRect(height));
129 });
130 });
131
132 this.image
133 .attr('width', plottingData.totalWidth)
134 .attr('height', plottingData.totalHeight)
135 .attr('x', plottingData.x0)
136 .attr('y', plottingData.y0);
137
138 this.drawCanvas();
139 }
140
141 private expandRect(value: number): number {
142 // Add 0.5px to width and height to fill gaps between rectangles
143 return value > 0 ? value + 0.5 : value - 0.5;
144 }
145
146 private getPlottingData() {
147 const xData: Array<number> = Array.from(this.model.mark_data.x).map(
148 this.scales.x.scale
149 );
150 const yData: Array<number> = Array.from(this.model.mark_data.y).map(
151 this.scales.y.scale
152 );
153
154 const xReverse = this.scales.x.model.get('reverse');
155 const yReverse = this.scales.y.model.get('reverse');
156
157 const padding = this.getPadding(xData, yData);
158
159 const widths = this.computeRectSizes(xData, padding.left, padding.right);
160 const heights = this.computeRectSizes(
161 yData,
162 padding.bottom,
163 padding.top,
164 true
165 );
166
167 const totalWidth = Math.abs(
168 xData[xData.length - 1] - xData[0] + padding.left + padding.right
169 );
170 const totalHeight = Math.abs(
171 yData[0] - yData[yData.length - 1] + padding.top + padding.bottom
172 );
173
174 const x0 = xData[0] - padding.left;
175 const y0 = yData[yData.length - 1] - padding.top;
176
177 const xStartPoints = xData.map((d, i) => {
178 if (i == 0) {
179 return 0;
180 } else {
181 return (d + xData[i - 1]) * 0.5 - x0;
182 }
183 });
184 const yStartPoints = yData.map((d, i) => {
185 if (i == yData.length - 1) {
186 return 0;
187 } else {
188 return (d + yData[i + 1]) * 0.5 - y0;
189 }
190 });
191
192 return {
193 widths,
194 heights,
195 totalWidth,
196 totalHeight,
197 xOrigin: xReverse ? totalWidth : 0,
198 yOrigin: yReverse ? totalHeight : 0,
199 xStartPoints,
200 yStartPoints,
201 x0: xReverse ? x0 - totalWidth : x0,
202 y0: yReverse ? y0 - totalHeight : y0,
203 };
204 }
205
206 private getPadding(xData: number[], yData: number[]) {
207 const numCols = xData.length;
208 const numRows = yData.length;
209
210 return {
211 left: (xData[1] - xData[0]) * 0.5,
212 right: (xData[numCols - 1] - xData[numCols - 2]) * 0.5,
213 bottom: -(yData[1] - yData[0]) * 0.5,
214 top: -(yData[numRows - 1] - yData[numRows - 2]) * 0.5,
215 };
216 }
217
218 computeRectSizes(data, padding1, padding2, reversed = false) {
219 const factor = reversed ? -1 : 1;
220
221 return data.map((d, i) => {
222 if (i == 0) {
223 return factor * (data[1] - d) * 0.5 + padding1;
224 } else if (i == data.length - 1) {
225 return factor * (d - data[i - 1]) * 0.5 + padding2;
226 } else {
227 return factor * (data[i + 1] - data[i - 1]) * 0.5;
228 }
229 });
230 }
231
232 private getElementFill(color: number | null) {
233 if (color === null) {
234 return this.model.get('null_color');
235 }
236
237 return this.scales.color.scale(color);
238 }
239
240 clear_style(style_dict, indices?, elements?) {}
241
242 compute_view_padding() {}
243
244 set_default_style(indices, elements?) {}
245
246 set_style_on_elements(style, indices, elements?) {}
247
248 private image: d3.Selection<SVGImageElement, any, any, any>;
249 private canvas: HTMLCanvasElement;
250
251 model: HeatMapModel;
252 }