|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, 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 createContour2D = require('gl-contour2d'); |
| 13 | +var createHeatmap2D = require('gl-heatmap2d'); |
| 14 | + |
| 15 | +var makeColorMap = require('../contour/make_color_map'); |
| 16 | +var str2RGBArray = require('../../lib/str2rgbarray'); |
| 17 | + |
| 18 | + |
| 19 | +function Contour(scene, uid) { |
| 20 | + this.scene = scene; |
| 21 | + this.uid = uid; |
| 22 | + |
| 23 | + this.name = ''; |
| 24 | + this.hoverinfo = 'all'; |
| 25 | + |
| 26 | + this.xData = []; |
| 27 | + this.yData = []; |
| 28 | + this.zData = []; |
| 29 | + this.textLabels = []; |
| 30 | + |
| 31 | + this.idToIndex = []; |
| 32 | + this.bounds = [0, 0, 0, 0]; |
| 33 | + |
| 34 | + this.contourOptions = { |
| 35 | + z: new Float32Array(), |
| 36 | + x: [], |
| 37 | + y: [], |
| 38 | + shape: [0, 0], |
| 39 | + levels: [0], |
| 40 | + levelColors: [0, 0, 0, 1], |
| 41 | + lineWidth: 1 |
| 42 | + }; |
| 43 | + this.contour = createContour2D(scene.glplot, this.contourOptions); |
| 44 | + this.contour._trace = this; |
| 45 | + |
| 46 | + this.heatmapOptions = { |
| 47 | + z: new Float32Array(), |
| 48 | + x: [], |
| 49 | + y: [], |
| 50 | + shape: [0, 0], |
| 51 | + colorLevels: [0], |
| 52 | + colorValues: [0, 0, 0, 0] |
| 53 | + }; |
| 54 | + this.heatmap = createHeatmap2D(scene.glplot, this.heatmapOptions); |
| 55 | + this.heatmap._trace = this; |
| 56 | +} |
| 57 | + |
| 58 | +var proto = Contour.prototype; |
| 59 | + |
| 60 | +proto.handlePick = function(pickResult) { |
| 61 | + var index = pickResult.pointId, |
| 62 | + options = this.heatmapOptions, |
| 63 | + shape = options.shape; |
| 64 | + |
| 65 | + return { |
| 66 | + trace: this, |
| 67 | + dataCoord: pickResult.dataCoord, |
| 68 | + traceCoord: [ |
| 69 | + options.x[index % shape[0]], |
| 70 | + options.y[Math.floor(index / shape[0])], |
| 71 | + options.z[index] |
| 72 | + ], |
| 73 | + textLabel: this.textLabels[index], |
| 74 | + name: this.name, |
| 75 | + hoverinfo: this.hoverinfo |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +proto.update = function(fullTrace, calcTrace) { |
| 80 | + var calcPt = calcTrace[0]; |
| 81 | + |
| 82 | + this.name = fullTrace.name; |
| 83 | + this.hoverinfo = fullTrace.hoverinfo; |
| 84 | + |
| 85 | + // convert z from 2D -> 1D |
| 86 | + var z = calcPt.z, |
| 87 | + rowLen = z[0].length, |
| 88 | + colLen = z.length; |
| 89 | + |
| 90 | + this.contourOptions.z = flattenZ(z, rowLen, colLen); |
| 91 | + this.heatmapOptions.z = [].concat.apply([], z); |
| 92 | + |
| 93 | + this.contourOptions.shape = this.heatmapOptions.shape = [rowLen, colLen]; |
| 94 | + |
| 95 | + this.contourOptions.x = this.heatmapOptions.x = calcPt.x; |
| 96 | + this.contourOptions.y = this.heatmapOptions.y = calcPt.y; |
| 97 | + |
| 98 | + var colorOptions = convertColorscale(fullTrace); |
| 99 | + this.contourOptions.levels = colorOptions.levels; |
| 100 | + this.contourOptions.levelColors = colorOptions.levelColors; |
| 101 | + |
| 102 | + // convert text from 2D -> 1D |
| 103 | + this.textLabels = [].concat.apply([], fullTrace.text); |
| 104 | + |
| 105 | + this.contour.update(this.contourOptions); |
| 106 | + this.heatmap.update(this.heatmapOptions); |
| 107 | +}; |
| 108 | + |
| 109 | +proto.dispose = function() { |
| 110 | + this.contour.dispose(); |
| 111 | + this.heatmap.dispose(); |
| 112 | +}; |
| 113 | + |
| 114 | +function flattenZ(zIn, rowLen, colLen) { |
| 115 | + var zOut = new Float32Array(rowLen * colLen); |
| 116 | + var pt = 0; |
| 117 | + |
| 118 | + for(var i = 0; i < rowLen; i++) { |
| 119 | + for(var j = 0; j < colLen; j++) { |
| 120 | + zOut[pt++] = zIn[j][i]; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return zOut; |
| 125 | +} |
| 126 | + |
| 127 | +function convertColorscale(fullTrace) { |
| 128 | + var contours = fullTrace.contours, |
| 129 | + start = contours.start, |
| 130 | + end = contours.end, |
| 131 | + cs = contours.size || 1; |
| 132 | + |
| 133 | + var colorMap = makeColorMap(fullTrace); |
| 134 | + |
| 135 | + var N = Math.floor((end - start) / cs) + 1, |
| 136 | + levels = new Array(N), |
| 137 | + levelColors = new Array(4 * N); |
| 138 | + |
| 139 | + for(var i = 0; i < N; i++) { |
| 140 | + var level = levels[i] = start + cs * (i); |
| 141 | + var color = str2RGBArray(colorMap(level)); |
| 142 | + |
| 143 | + for(var j = 0; j < 4; j++) { |
| 144 | + levelColors[(4 * i) + j] = color[j]; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return { |
| 149 | + levels: levels, |
| 150 | + levelColors: levelColors |
| 151 | + }; |
| 152 | +} |
| 153 | + |
| 154 | +function createContour(scene, fullTrace, calcTrace) { |
| 155 | + var plot = new Contour(scene, fullTrace.uid); |
| 156 | + plot.update(fullTrace, calcTrace); |
| 157 | + |
| 158 | + return plot; |
| 159 | +} |
| 160 | + |
| 161 | +module.exports = createContour; |
0 commit comments