Skip to content

Commit bb8137c

Browse files
committed
Merge pull request #434 from plotly/contourgl
Add contourgl trace module
2 parents a66f3f4 + 4151080 commit bb8137c

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

lib/contourgl.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
module.exports = require('../src/traces/contourgl');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"delaunay-triangulate": "^1.1.6",
5353
"es6-promise": "^3.0.2",
5454
"fast-isnumeric": "^1.1.1",
55+
"gl-contour2d": "^1.0.1",
5556
"gl-error2d": "^1.0.0",
5657
"gl-error3d": "^1.0.0",
5758
"gl-heatmap2d": "^1.0.2",

src/traces/contourgl/convert.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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;

src/traces/contourgl/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ContourGl = {};
13+
14+
ContourGl.attributes = require('../contour/attributes');
15+
ContourGl.supplyDefaults = require('../contour/defaults');
16+
ContourGl.colorbar = require('../contour/colorbar');
17+
18+
ContourGl.calc = require('../contour/calc');
19+
ContourGl.plot = require('./convert');
20+
21+
ContourGl.moduleType = 'trace';
22+
ContourGl.name = 'contourgl';
23+
ContourGl.basePlotModule = require('../../plots/gl2d');
24+
ContourGl.categories = ['gl2d', '2dMap'];
25+
ContourGl.meta = {
26+
description: [
27+
'WebGL contour (beta)'
28+
].join(' ')
29+
};
30+
31+
module.exports = ContourGl;

0 commit comments

Comments
 (0)