Skip to content

Commit 6197c4d

Browse files
committed
add 'contourgl' module (first pass)
1 parent 6d2e5f3 commit 6197c4d

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ Core.register([
3030
require('./scattergeo'),
3131
require('./choropleth'),
3232
require('./scattergl'),
33-
require('./scatterternary')
33+
require('./scatterternary'),
34+
35+
// TODO remove
36+
require('../src/traces/contourgl')
3437
]);
3538

3639
module.exports = Core;

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: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 str2RGBArray = require('../../lib/str2rgbarray');
16+
var makeScaleFunc = require('../../components/colorscale/make_scale_function');
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.contourOptions,
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+
this.contourOptions.z = this.heatmapOptions.z = new Float32Array([].concat.apply([], z));
88+
89+
var rowLen = z[0].length,
90+
colLen = z.length;
91+
this.contourOptions.shape = this.heatmapOptions.shape = [rowLen, colLen];
92+
93+
this.contourOptions.x = this.heatmapOptions.x = calcPt.x;
94+
this.contourOptions.y = this.heatmapOptions.y = calcPt.y;
95+
96+
var colorOptions = convertColorscale(fullTrace);
97+
this.contourOptions.levels = colorOptions.levels;
98+
this.contourOptions.levelColors = colorOptions.levelColors;
99+
100+
// convert text from 2D -> 1D
101+
this.textLabels = [].concat.apply([], fullTrace.text);
102+
103+
this.contour.update(this.contourOptions);
104+
this.heatmap.update(this.heatmapOptions);
105+
};
106+
107+
proto.dispose = function() {
108+
this.contour.dispose();
109+
this.heatmap.dispose();
110+
};
111+
112+
function convertColorscale(fullTrace) {
113+
var contours = fullTrace.contours,
114+
start = contours.start,
115+
end = contours.end,
116+
size = contours.size;
117+
118+
var sclFunc = makeScaleFunc(
119+
fullTrace.colorscale,
120+
fullTrace.zmin, fullTrace.zmax
121+
);
122+
123+
var N = Math.floor((end - start) / size),
124+
levels = new Array(N),
125+
levelColors = new Array(4 * N);
126+
127+
for(var i = 0; i < N; i++) {
128+
var level = levels[i] = start + size * i;
129+
var color = str2RGBArray(sclFunc(level));
130+
131+
for(var j = 0; j < 4; j++) {
132+
levelColors[(4 * i) + j] = color[j];
133+
}
134+
}
135+
136+
return {
137+
levels: levels,
138+
levelColors: levelColors
139+
};
140+
}
141+
142+
function createContour(scene, fullTrace, calcTrace) {
143+
var plot = new Contour(scene, fullTrace.uid);
144+
plot.update(fullTrace, calcTrace);
145+
146+
return plot;
147+
}
148+
149+
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)