forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
185 lines (148 loc) · 4.98 KB
/
convert.js
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
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var createContour2D = require('gl-contour2d');
var createHeatmap2D = require('gl-heatmap2d');
var Axes = require('../../plots/cartesian/axes');
var makeColorMap = require('../contour/make_color_map');
var str2RGBArray = require('../../lib/str2rgbarray');
function Contour(scene, uid) {
this.scene = scene;
this.uid = uid;
this.type = 'contourgl';
this.name = '';
this.hoverinfo = 'all';
this.xData = [];
this.yData = [];
this.zData = [];
this.textLabels = [];
this.idToIndex = [];
this.bounds = [0, 0, 0, 0];
this.contourOptions = {
z: new Float32Array(0),
x: [],
y: [],
shape: [0, 0],
levels: [0],
levelColors: [0, 0, 0, 1],
lineWidth: 1
};
this.contour = createContour2D(scene.glplot, this.contourOptions);
this.contour._trace = this;
this.heatmapOptions = {
z: new Float32Array(0),
x: [],
y: [],
shape: [0, 0],
colorLevels: [0],
colorValues: [0, 0, 0, 0]
};
this.heatmap = createHeatmap2D(scene.glplot, this.heatmapOptions);
this.heatmap._trace = this;
}
var proto = Contour.prototype;
proto.handlePick = function(pickResult) {
var options = this.heatmapOptions,
shape = options.shape,
index = pickResult.pointId,
xIndex = index % shape[0],
yIndex = Math.floor(index / shape[0]),
zIndex = index;
return {
trace: this,
dataCoord: pickResult.dataCoord,
traceCoord: [
options.x[xIndex],
options.y[yIndex],
options.z[zIndex]
],
textLabel: this.textLabels[index],
name: this.name,
pointIndex: [xIndex, yIndex],
hoverinfo: this.hoverinfo
};
};
proto.update = function(fullTrace, calcTrace) {
var calcPt = calcTrace[0];
this.name = fullTrace.name;
this.hoverinfo = fullTrace.hoverinfo;
// convert z from 2D -> 1D
var z = calcPt.z,
rowLen = z[0].length,
colLen = z.length,
colorOptions;
this.contourOptions.z = flattenZ(z, rowLen, colLen);
this.heatmapOptions.z = [].concat.apply([], z);
this.contourOptions.shape = this.heatmapOptions.shape = [rowLen, colLen];
this.contourOptions.x = this.heatmapOptions.x = calcPt.x;
this.contourOptions.y = this.heatmapOptions.y = calcPt.y;
// pass on fill information
if(fullTrace.contours.coloring === 'fill') {
colorOptions = convertColorScale(fullTrace, {fill: true});
this.contourOptions.levels = colorOptions.levels.slice(1);
// though gl-contour2d automatically defaults to a transparent layer for the last
// band color, it's set manually here in case the gl-contour2 API changes
this.contourOptions.fillColors = colorOptions.levelColors;
this.contourOptions.levelColors = [].concat.apply([], this.contourOptions.levels.map(function() {
return [0.25, 0.25, 0.25, 1.0];
}));
} else {
colorOptions = convertColorScale(fullTrace, {fill: false});
this.contourOptions.levels = colorOptions.levels;
this.contourOptions.levelColors = colorOptions.levelColors;
}
// convert text from 2D -> 1D
this.textLabels = [].concat.apply([], fullTrace.text);
this.contour.update(this.contourOptions);
this.heatmap.update(this.heatmapOptions);
// expand axes
Axes.expand(this.scene.xaxis, calcPt.x);
Axes.expand(this.scene.yaxis, calcPt.y);
};
proto.dispose = function() {
this.contour.dispose();
this.heatmap.dispose();
};
function flattenZ(zIn, rowLen, colLen) {
var zOut = new Float32Array(rowLen * colLen);
var pt = 0;
for(var i = 0; i < rowLen; i++) {
for(var j = 0; j < colLen; j++) {
zOut[pt++] = zIn[j][i];
}
}
return zOut;
}
function convertColorScale(fullTrace, options) {
var contours = fullTrace.contours,
start = contours.start,
end = contours.end,
cs = contours.size || 1,
fill = options.fill;
var colorMap = makeColorMap(fullTrace);
var N = Math.floor((end - start) / cs) + (fill ? 2 : 1), // for K thresholds (contour linees) there are K+1 areas
levels = new Array(N),
levelColors = new Array(4 * N);
for(var i = 0; i < N; i++) {
var level = levels[i] = start + cs * (i) - (fill ? cs / 2 : 0); // in case of fill, use band midpoint
var color = str2RGBArray(colorMap(level));
for(var j = 0; j < 4; j++) {
levelColors[(4 * i) + j] = color[j];
}
}
return {
levels: levels,
levelColors: levelColors
};
}
function createContour(scene, fullTrace, calcTrace) {
var plot = new Contour(scene, fullTrace.uid);
plot.update(fullTrace, calcTrace);
return plot;
}
module.exports = createContour;