|
| 1 | +/** |
| 2 | +* Copyright 2012-2015, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var createHeatmap2D = require('gl-heatmap2d'); |
| 13 | + |
| 14 | +var maxRowLength = require('../heatmap/max_row_length'); |
| 15 | +var str2RGBArray = require('../../lib/str2rgbarray'); |
| 16 | + |
| 17 | +var AXES = ['xaxis', 'yaxis']; |
| 18 | + |
| 19 | + |
| 20 | +function Heatmap(scene, uid) { |
| 21 | + this.scene = scene; |
| 22 | + this.uid = uid; |
| 23 | + |
| 24 | + this.name = ''; |
| 25 | + this.hoverinfo = 'all'; |
| 26 | + |
| 27 | + this.xData = []; |
| 28 | + this.yData = []; |
| 29 | + this.zData = []; |
| 30 | + this.textLabels = []; |
| 31 | + |
| 32 | + this.idToIndex = []; |
| 33 | + this.bounds = [0, 0, 0, 0]; |
| 34 | + |
| 35 | + this.options = { |
| 36 | + z: [], |
| 37 | + x: [], |
| 38 | + y: [], |
| 39 | + shape: [0, 0], |
| 40 | + colorLevels: [0], |
| 41 | + colorValues: [0, 0, 0, 1] |
| 42 | + }; |
| 43 | + |
| 44 | + this.heatmap = createHeatmap2D(scene.glplot, this.options); |
| 45 | + this.heatmap._trace = this; |
| 46 | +} |
| 47 | + |
| 48 | +var proto = Heatmap.prototype; |
| 49 | + |
| 50 | +proto.handlePick = function(pickResult) { |
| 51 | + var index = this.idToIndex[pickResult.pointId]; |
| 52 | + |
| 53 | +// console.log(pickResult.pointId) |
| 54 | + |
| 55 | + return { |
| 56 | + trace: this, |
| 57 | + dataCoord: pickResult.dataCoord, |
| 58 | + traceCoord: [ |
| 59 | + this.xData[index], |
| 60 | + this.yData[index] |
| 61 | + ], |
| 62 | + textLabel: Array.isArray(this.textLabels) ? |
| 63 | + this.textLabels[index] : |
| 64 | + this.textLabels, |
| 65 | + color: Array.isArray(this.color) ? |
| 66 | + this.color[index] : |
| 67 | + this.color, |
| 68 | + name: this.name, |
| 69 | + hoverinfo: this.hoverinfo |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +proto.update = function(fullTrace, calcTrace) { |
| 74 | + var calcPt = calcTrace[0]; |
| 75 | + |
| 76 | + this.textLabels = fullTrace.text; |
| 77 | + this.name = fullTrace.name; |
| 78 | + this.hoverinfo = fullTrace.hoverinfo; |
| 79 | + |
| 80 | + // convert z from 2D -> 1D |
| 81 | + var z = calcPt.z; |
| 82 | + this.options.z = [].concat.apply([], z); |
| 83 | + |
| 84 | + var rowLen = z[0].length, |
| 85 | + colLen = z.length; |
| 86 | + this.options.shape = [rowLen, colLen]; |
| 87 | + |
| 88 | + // don't use calc'ed bricks |
| 89 | + // maybe use xa.makeCalcdata() ??? |
| 90 | + var x = fullTrace.x; |
| 91 | + if(x) { |
| 92 | + this.options.x = x; |
| 93 | + this.bounds[0] = x[0]; |
| 94 | + this.bounds[2] = x[rowLen - 1]; |
| 95 | + } |
| 96 | + else { |
| 97 | + this.options.x = null; |
| 98 | + this.bounds[0] = 0; |
| 99 | + this.bounds[2] = rowLen |
| 100 | + } |
| 101 | + |
| 102 | + var y = fullTrace.y; |
| 103 | + if(y) { |
| 104 | + this.options.y = y; |
| 105 | + this.bounds[1] = y[0]; |
| 106 | + this.bounds[3] = y[colLen - 1]; |
| 107 | + } |
| 108 | + else { |
| 109 | + this.options.y = null; |
| 110 | + this.bounds[1] = 0; |
| 111 | + this.bounds[3] = colLen |
| 112 | + } |
| 113 | + |
| 114 | + var colorOptions = convertColorscale(fullTrace); |
| 115 | + this.options.colorLevels = colorOptions.colorLevels; |
| 116 | + this.options.colorValues = colorOptions.colorValues; |
| 117 | + |
| 118 | + this.heatmap.update(this.options); |
| 119 | +}; |
| 120 | + |
| 121 | +proto.dispose = function() { |
| 122 | + this.heatmap.dispose(); |
| 123 | +}; |
| 124 | + |
| 125 | +function convertColorscale(fullTrace) { |
| 126 | + var scl = fullTrace.colorscale, |
| 127 | + zmin = fullTrace.zmin, |
| 128 | + zmax = fullTrace.zmax; |
| 129 | + |
| 130 | + var N = scl.length, |
| 131 | + domain = new Array(N), |
| 132 | + range = new Array(4 * N); |
| 133 | + |
| 134 | + for(var i = 0; i < N; i++) { |
| 135 | + var si = scl[i]; |
| 136 | + var color = str2RGBArray(si[1]); |
| 137 | + |
| 138 | + domain[i] = zmin + si[0] * (zmax - zmin); |
| 139 | + |
| 140 | + for(var j = 0; j < 4; j++) { |
| 141 | + range[(4 * i) + j] = color[j]; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return { |
| 146 | + colorLevels: domain, |
| 147 | + colorValues: range |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +function createHeatmap(scene, fullTrace, calcTrace) { |
| 152 | + var plot = new Heatmap(scene, fullTrace.uid); |
| 153 | + plot.update(fullTrace, calcTrace); |
| 154 | + return plot; |
| 155 | +} |
| 156 | + |
| 157 | +module.exports = createHeatmap; |
0 commit comments