aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/components/vz_heatmap/vz-heatmap.html
blob: 7acba47d5f5b9c42cac364fb745bd67e6b935763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!--
@license
Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../tf-imports/d3.html">

<!--
`vz-heatmap` A simple heatmap to visualize 2D data using predefined or user
defined colour scheme, with a dependency on d3.js. The heatmap automatically
fits itself to the width of the container it is placed in.

@element vz-heatmap
@demo demo/index.html
-->
<dom-module id="vz-heatmap">
  <template>
    <div>
      <canvas id="heatmap"
              style="width: 100%; image-rendering:pixelated"></canvas>
    </div>
  </template>
  <script>
    Polymer({
      is: 'vz-heatmap',
      properties: {
        /**
         * An 2-element array which defines the lower and upper bound of the
         * automatically created color scale for the heatmap.
         */
        colors: {
          type: Array,
          value: ['white', 'steelblue'],
          observer: '_onColorsChange'
        },
        /**
         * A function which returns a hex-formatted color string
         * given a number type.
         * i.e. (input: number) => string
         *
         * Passing a colorFunction deactivates the color
         * function generated by the `colors` and `values` fields.
         */
        colorFunction: {
          type: Object,
          observer: '_onColorFunctionChange'
        },
        /**
         * A 2D data array containing the data to be visualized.
         */
        data: {
          type: Array,
          value: [],
          observer: '_onDataChange'
        },
        /**
         * A 2-element array containing the domain of values which
         * maps to the color range defined by the `colors` array.
         */
        values: {
          type: Array,
          observer: '_onValuesChange'
        },
        _currentDataValid: {
          type: Boolean,
          value: false
        },
        _dataExtent: {
          type: Array,
          value: [0, 1]
        },
        _debug: {
          type: Boolean,
          value: false
        },
        _generatedColorScale: {
          type: Object,
          value: (function () {
            var retFunction = d3.scale.linear();
            if (this.colors) {
              retFunction.range(this.colors)
            }

            return retFunction;
          })()
        },
        _isReady: {
          type: Boolean,
          value: false
        },
        _height: {
          type: Number,
          value: 0
        },
        _width: {
          type: Number,
          value: 0
        },
        _useUserColorPickerFunction: {
          type: Boolean,
          value: false
        }
      },
      behaviors: [
        Polymer.IronResizableBehavior
      ],
      listeners: {
        'iron-resize': '_onWidthChange'
      },
      _onWidthChange: function () {
        // Re-render the chart if the data has already been set.
        if (this._currentDataValid) {
          this._renderData();
        }
      },
      ready: function () {
        this._isReady = true;
        this._onWidthChange();
      },
      attached: function () {
        this._onWidthChange();
      },
      /**
       * Data change handler, if new data is valid, sets flag to indicate data
       * now valid, updates the data extent and renders the new data.
       */
      _onDataChange: function () {
        // Validate new data.
        if (!this._isDataValid(this.data)) {
          return;
        }

        // Set flag to indicate data is now valid.
        this._currentDataValid = true;

        // Calculate new data extent.
        this._updateDataExtent();

        if (this._isReady) {
          this._renderData();
        }
      },
      /**
       * (Re-)Renders the heatmap. Called when data, color mapping, or width
       * changes.
       * @private
       */
      _renderData: function () {
        // Ensure dimensions are up-to-date and valid before starting rendering.
        this._updateDimensions();
        if (!this._width || !this._height) return;

        // Ensure the color function is up-to-date.
        this._updateColorFunction();
        var width = this._width;
        var rows = this.data.length;
        var columns = this.data[0].length;

        // Calculate side length of each tile.
        var sideLength = width / columns;

        // Set domain and range of the axis.
        var colorScale = (this._useUserColorPickerFunction) ?
            this.colorFunction : this._generatedColorScale;

        // Clear the canvas.
        this.resetCanvas();

        // Render heatmap.
        var ctx = this.$.heatmap.getContext("2d");

        for (var row = 0; row < rows; row++) {
          for (var column = 0; column < columns; column++) {
            var value = this.data[row][column];
            // Set location and dimensions.
            ctx.fillStyle = colorScale(value);
            // Preferred way to set color of individual pixels.
            ctx.fillRect(column, row, 1, 1);
          }
        }
      },
      /**
       * Observer for this.colors.
       * @private
       */
      _onColorsChange: function () {
        if (Array.isArray(this.colors) && this.colors.length == 2) {
          this._updateColorFunction();
        }
        this._useUserColorPickerFunction = false;
        this._renderData();
      },
      /**
       * Observer for this.values. Updates the
       * internal color function. If data invalid, internal color scale uses
       * the extent of the data as domain.
       * @private
       */
      _onValuesChange: function () {
        // Verify that validity of the new content of values.
        // If data not valid, set this.values to undefined, as
        // _updateColorFunction will then use the data's extent as the color
        // scale's domain.
        if (!(Array.isArray(this.values) && this.values.length == 2)) {
          this.values = undefined;
        }

        this._updateColorFunction();
        this._useUserColorPickerFunction = false;
        this._renderData();
      },
      /**
       * Observer for this.colorFunction. This field allows the user to
       * provide a custom color scale. Updates to this value are ignored, if the new
       * value is not a function object.
       * @private
       */
      _onColorFunctionChange: function () {
        if (typeof this.colorFunction === 'function') {
          this._useUserColorPickerFunction = true;
          this._renderData();
        } else if (this._debug) {
          console.log('The colorFunction provided is not of function type, and was not set as default.')
        }
      },
      /**
       * Calculates the extent of the data if it is required by the
       * current color function. This function is called when
       * properties change which would result in the color scale
       * changing, or when the computed color scale is started to be used.
       * @private
       */
      _updateDataExtent: function () {
        if (this._currentDataValid) {
          var rows = this.data.length;
          var columns = this.data[0].length;

          var min = this.data[0][0];
          var max = this.data[0][0];

          for (var row = 0; row < rows; row++) {
            for (var col = 0; col < columns; col++) {
              var currentElement = this.data[row][col];

              if (currentElement < min) {
                min = currentElement;
              } else if (currentElement > max) {
                max = currentElement;
              }
            }
          }

          this._dataExtent = [min, max];
        } else if (!this._dataExtent) {
          this._dataExtent = [0, 1];
        }
      },
      /**
       * Updates the internal color function only when the number of
       * elements in this.colors is equal to the number of elements in
       * this.values or if this.values is empty, in which case the
       * extent of the data is used.
       * @private
       */
      _updateColorFunction: function () {
        if (Array.isArray(this.colors) && this.colors.length) {
          this._generatedColorScale = d3.scale.linear().range(this.colors);
        }

        if (this.values) {
          // Check that the number of elements in this.values and
          // this.colors have an identical number of elements.
          if (this.colors.length === this.values.length) {
            this._generatedColorScale.domain(this.values);
          }

          // If values reset, and data field contains valid data, set colour scale
          // to use the data extent as domain.
        } else if (this._currentDataValid) {
          this._generatedColorScale.domain(this._dataExtent);
        }
      },
      _getLinearInterpolation: function (domain, range) {
        return d3.scale.linear().domain(domain).range(range);
      },
      /**
       * Find side length of each tile in heat map by dividing current width
       * by number of columns
       */
      _findTileSideLength: function () {
        if (this._currentDataValid) {
          var numOfColumns = this.data[0].length;

          // Make sure value is finite.
          if (numOfColumns === 0) {
            var returnValue = 0;
          } else {
            returnValue = this._width / numOfColumns;
          }
          return returnValue;

        } else {
          if (this._debug == true) {
            console.log("WARNING: vz-heatmap is reverting to zero height as passed data is invalid.")
          }
          return 0; // Fall back to 0 height, if data is invalid.
        }
      },
      /**
       * Verifies whether input data is valid.
       * @param newData Number[][]
       * @private
       */
      _isDataValid: function (newData) {
        return Array.isArray(newData) && newData.length &&
            Array.isArray(newData[0]);
      },
      _updateDimensions: function () {
        var canvasElement = this.$.heatmap;

        var rows = 0;
        var columns = 0;
        if (this._currentDataValid) {
          rows = this.data.length;
          columns = this.data[0].length;
        }

        // Recalculate height and width, and ensure data can be rendered.
        this._width = canvasElement.parentNode.clientWidth;
        this._height = this._findTileSideLength() * rows;
        // Update the attribute height and width.
        canvasElement.setAttribute('height', rows);
        canvasElement.setAttribute('width', columns);
      },
      /**
       * Function which clears the canvas.
       */
      resetCanvas: function () {
        // Reset the canvas.
        var canvas = this.$.heatmap;
        var ctx = canvas.getContext("2d");
        ctx.clearRect(0, 0, canvas.width, canvas.height);
      }
    });
  </script>
</dom-module>