Skip to content

Commit 123e453

Browse files
committed
Merge pull request #449 from mikolalysenko/connect-gaps-2d
Implement connect gaps for 2d WebGL plots
2 parents dc5c7c8 + 4aa202a commit 123e453

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"cibuild": "node tasks/cibundle.js",
2929
"watch": "node tasks/watch_plotly.js",
3030
"lint": "eslint . || true",
31+
"lint-fix": "eslint . --fix",
3132
"test-jasmine": "karma start test/jasmine/karma.conf.js",
3233
"citest-jasmine": "karma start test/jasmine/karma.ciconf.js",
3334
"test-image": "./tasks/test_image.sh",
@@ -57,7 +58,7 @@
5758
"gl-error2d": "^1.0.0",
5859
"gl-error3d": "^1.0.0",
5960
"gl-heatmap2d": "^1.0.2",
60-
"gl-line2d": "^1.2.1",
61+
"gl-line2d": "^1.3.0",
6162
"gl-line3d": "^1.1.0",
6263
"gl-mat4": "^1.1.2",
6364
"gl-mesh3d": "^1.0.7",
@@ -87,7 +88,7 @@
8788
"browserify": "^13.0.0",
8889
"browserify-transform-tools": "^1.5.1",
8990
"ecstatic": "^1.4.0",
90-
"eslint": "^2.1.0",
91+
"eslint": "^2.8.0",
9192
"falafel": "^1.2.0",
9293
"fs-extra": "^0.28.0",
9394
"fuse.js": "^2.2.0",

src/traces/scattergl/attributes.js

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = {
8686
reversescale: scatterMarkerLineAttrs.reversescale
8787
}
8888
},
89+
connectgaps: scatterAttrs.connectgaps,
8990
fill: extendFlat({}, scatterAttrs.fill, {
9091
values: ['none', 'tozeroy', 'tozerox']
9192
}),

src/traces/scattergl/convert.js

+34-11
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ function LineWithMarkers(scene, uid) {
3333
this.scene = scene;
3434
this.uid = uid;
3535

36+
this.pickXData = [];
37+
this.pickYData = [];
3638
this.xData = [];
3739
this.yData = [];
3840
this.textLabels = [];
3941
this.color = 'rgb(0, 0, 0)';
4042
this.name = '';
4143
this.hoverinfo = 'all';
44+
this.connectgaps = true;
4245

4346
this.idToIndex = [];
4447
this.bounds = [0, 0, 0, 0];
@@ -103,14 +106,17 @@ function LineWithMarkers(scene, uid) {
103106
var proto = LineWithMarkers.prototype;
104107

105108
proto.handlePick = function(pickResult) {
106-
var index = this.idToIndex[pickResult.pointId];
109+
var index = pickResult.pointId;
110+
if(pickResult.object !== this.line || this.connectgaps) {
111+
index = this.idToIndex[pickResult.pointId];
112+
}
107113

108114
return {
109115
trace: this,
110116
dataCoord: pickResult.dataCoord,
111117
traceCoord: [
112-
this.xData[index],
113-
this.yData[index]
118+
this.pickXData[index],
119+
this.pickYData[index]
114120
],
115121
textLabel: Array.isArray(this.textLabels) ?
116122
this.textLabels[index] :
@@ -248,6 +254,7 @@ proto.update = function(options) {
248254
this.name = options.name;
249255
this.hoverinfo = options.hoverinfo;
250256
this.bounds = [Infinity, Infinity, -Infinity, -Infinity];
257+
this.connectgaps = !!options.connectgaps;
251258

252259
if(this.isFancy(options)) {
253260
this.updateFancy(options);
@@ -262,8 +269,8 @@ proto.update = function(options) {
262269
};
263270

264271
proto.updateFast = function(options) {
265-
var x = this.xData = options.x;
266-
var y = this.yData = options.y;
272+
var x = this.xData = this.pickXData = options.x;
273+
var y = this.yData = this.pickYData = options.y;
267274

268275
var len = x.length,
269276
idToIndex = new Array(len),
@@ -344,8 +351,11 @@ proto.updateFancy = function(options) {
344351
bounds = this.bounds;
345352

346353
// makeCalcdata runs d2c (data-to-coordinate) on every point
347-
var x = this.xData = xaxis.makeCalcdata(options, 'x');
348-
var y = this.yData = yaxis.makeCalcdata(options, 'y');
354+
var x = this.pickXData = xaxis.makeCalcdata(options, 'x').slice();
355+
var y = this.pickYData = yaxis.makeCalcdata(options, 'y').slice();
356+
357+
this.xData = x.slice();
358+
this.yData = y.slice();
349359

350360
// get error values
351361
var errorVals = ErrorBars.calcFromTrace(options, scene.fullLayout);
@@ -370,8 +380,8 @@ proto.updateFancy = function(options) {
370380
var i, j, xx, yy, ex0, ex1, ey0, ey1;
371381

372382
for(i = 0; i < len; ++i) {
373-
xx = getX(x[i]);
374-
yy = getY(y[i]);
383+
this.xData[i] = xx = getX(x[i]);
384+
this.yData[i] = yy = getY(y[i]);
375385

376386
if(isNaN(xx) || isNaN(yy)) continue;
377387

@@ -460,16 +470,29 @@ proto.updateFancy = function(options) {
460470
};
461471

462472
proto.updateLines = function(options, positions) {
473+
var i;
463474
if(this.hasLines) {
464-
this.lineOptions.positions = positions;
475+
var linePositions = positions;
476+
if(!options.connectgaps) {
477+
var p = 0;
478+
var x = this.xData;
479+
var y = this.yData;
480+
linePositions = new Float32Array(2 * x.length);
481+
482+
for(i=0; i<x.length; ++i) {
483+
linePositions[p++] = x[i];
484+
linePositions[p++] = y[i];
485+
}
486+
}
487+
this.lineOptions.positions = linePositions;
465488

466489
var lineColor = str2RGBArray(options.line.color);
467490
if(this.hasMarkers) lineColor[3] *= options.marker.opacity;
468491

469492
var lineWidth = Math.round(0.5 * this.lineOptions.width),
470493
dashes = (DASHES[options.line.dash] || [1]).slice();
471494

472-
for(var i = 0; i < dashes.length; ++i) dashes[i] *= lineWidth;
495+
for(i = 0; i < dashes.length; ++i) dashes[i] *= lineWidth;
473496

474497
switch(options.fill) {
475498
case 'tozeroy':

src/traces/scattergl/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3737
coerce('mode', len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines');
3838

3939
if(subTypes.hasLines(traceOut)) {
40+
coerce('connectgaps');
4041
handleLineDefaults(traceIn, traceOut, defaultColor, coerce);
4142
}
4243

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"data": [
3+
{
4+
"x": [1, 2, 3, 4, 5],
5+
"y": [1, 1, null, -1, -1],
6+
"connectgaps": false,
7+
"mode": "lines",
8+
"type": "scattergl"
9+
},
10+
{
11+
"x": [1, 2, 3, 4, 5],
12+
"y": [-2, -2, null, 2, 2],
13+
"connectgaps": true,
14+
"mode": "lines",
15+
"type": "scattergl"
16+
}
17+
],
18+
"layout": {
19+
"title": "Connect gaps test"
20+
}
21+
}

0 commit comments

Comments
 (0)