Skip to content

Implement connect gaps for 2d WebGL plots #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cibuild": "node tasks/cibundle.js",
"watch": "node tasks/watch_plotly.js",
"lint": "eslint . || true",
"lint-fix": "eslint . --fix",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicely done!

"test-jasmine": "karma start test/jasmine/karma.conf.js",
"citest-jasmine": "karma start test/jasmine/karma.ciconf.js",
"test-image": "./tasks/test_image.sh",
Expand Down Expand Up @@ -55,7 +56,7 @@
"gl-error2d": "^1.0.0",
"gl-error3d": "^1.0.0",
"gl-heatmap2d": "^1.0.2",
"gl-line2d": "^1.2.1",
"gl-line2d": "^1.3.0",
"gl-line3d": "^1.1.0",
"gl-mat4": "^1.1.2",
"gl-mesh3d": "^1.0.7",
Expand Down Expand Up @@ -85,7 +86,7 @@
"browserify": "^13.0.0",
"browserify-transform-tools": "^1.5.1",
"ecstatic": "^1.4.0",
"eslint": "^2.1.0",
"eslint": "^2.8.0",
"falafel": "^1.2.0",
"fs-extra": "^0.27.0",
"fuse.js": "^2.2.0",
Expand Down
1 change: 1 addition & 0 deletions src/traces/scattergl/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
reversescale: scatterMarkerLineAttrs.reversescale
}
},
connectgaps: scatterAttrs.connectgaps,
fill: extendFlat({}, scatterAttrs.fill, {
values: ['none', 'tozeroy', 'tozerox']
}),
Expand Down
45 changes: 34 additions & 11 deletions src/traces/scattergl/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ function LineWithMarkers(scene, uid) {
this.scene = scene;
this.uid = uid;

this.pickXData = [];
this.pickYData = [];
this.xData = [];
this.yData = [];
this.textLabels = [];
this.color = 'rgb(0, 0, 0)';
this.name = '';
this.hoverinfo = 'all';
this.connectgaps = true;

this.idToIndex = [];
this.bounds = [0, 0, 0, 0];
Expand Down Expand Up @@ -103,14 +106,17 @@ function LineWithMarkers(scene, uid) {
var proto = LineWithMarkers.prototype;

proto.handlePick = function(pickResult) {
var index = this.idToIndex[pickResult.pointId];
var index = pickResult.pointId;
if(pickResult.object !== this.line || this.connectgaps) {
index = this.idToIndex[pickResult.pointId];
}

return {
trace: this,
dataCoord: pickResult.dataCoord,
traceCoord: [
this.xData[index],
this.yData[index]
this.pickXData[index],
this.pickYData[index]
],
textLabel: Array.isArray(this.textLabels) ?
this.textLabels[index] :
Expand Down Expand Up @@ -248,6 +254,7 @@ proto.update = function(options) {
this.name = options.name;
this.hoverinfo = options.hoverinfo;
this.bounds = [Infinity, Infinity, -Infinity, -Infinity];
this.connectgaps = !!options.connectgaps;

if(this.isFancy(options)) {
this.updateFancy(options);
Expand All @@ -262,8 +269,8 @@ proto.update = function(options) {
};

proto.updateFast = function(options) {
var x = this.xData = options.x;
var y = this.yData = options.y;
var x = this.xData = this.pickXData = options.x;
var y = this.yData = this.pickYData = options.y;

var len = x.length,
idToIndex = new Array(len),
Expand Down Expand Up @@ -344,8 +351,11 @@ proto.updateFancy = function(options) {
bounds = this.bounds;

// makeCalcdata runs d2c (data-to-coordinate) on every point
var x = this.xData = xaxis.makeCalcdata(options, 'x');
var y = this.yData = yaxis.makeCalcdata(options, 'y');
var x = this.pickXData = xaxis.makeCalcdata(options, 'x').slice();
var y = this.pickYData = yaxis.makeCalcdata(options, 'y').slice();

this.xData = x.slice();
this.yData = y.slice();

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

for(i = 0; i < len; ++i) {
xx = getX(x[i]);
yy = getY(y[i]);
this.xData[i] = xx = getX(x[i]);
this.yData[i] = yy = getY(y[i]);

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

Expand Down Expand Up @@ -460,16 +470,29 @@ proto.updateFancy = function(options) {
};

proto.updateLines = function(options, positions) {
var i;
if(this.hasLines) {
this.lineOptions.positions = positions;
var linePositions = positions;
if(!options.connectgaps) {
var p = 0;
var x = this.xData;
var y = this.yData;
linePositions = new Float32Array(2 * x.length);

for(i=0; i<x.length; ++i) {
linePositions[p++] = x[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might have to ♻️ this block.

linePositions[p++] = y[i];
}
}
this.lineOptions.positions = linePositions;

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

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

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

switch(options.fill) {
case 'tozeroy':
Expand Down
1 change: 1 addition & 0 deletions src/traces/scattergl/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
coerce('mode', len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines');

if(subTypes.hasLines(traceOut)) {
coerce('connectgaps');
handleLineDefaults(traceIn, traceOut, defaultColor, coerce);
}

Expand Down
21 changes: 21 additions & 0 deletions test/image/mocks/gl2d_connect_gaps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"data": [
{
"x": [1, 2, 3, 4, 5],
"y": [1, 1, null, -1, -1],
"connectgaps": false,
"mode": "lines",
"type": "scattergl"
},
{
"x": [1, 2, 3, 4, 5],
"y": [-2, -2, null, 2, 2],
"connectgaps": true,
"mode": "lines",
"type": "scattergl"
}
],
"layout": {
"title": "Connect gaps test"
}
}