Skip to content

Introducing plotly.js + Mapbox GL #626

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 24 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
70856b2
introducing the 'mapbox' base plot module
etpinard Jun 10, 2016
3968417
introducting the 'scattermapbox' trace module
etpinard Jun 10, 2016
3648744
add 'mapbox' to layout attributes
etpinard Jun 10, 2016
6254578
make sure that non-svg plot types go through calc on restyle,
etpinard Jun 10, 2016
6a73378
handle mapbox.layers relayouts
etpinard Jun 10, 2016
3e79f0d
add 'pretest' tasks,
etpinard Jun 10, 2016
3ea7436
set mapbox crendentials in test dashboard and image test index
etpinard Jun 10, 2016
3b2c2e2
run pretest on circleci,
etpinard Jun 10, 2016
a47704c
tmp commit: register scattermapbox in main index file,
etpinard Jun 10, 2016
f300d27
add test jasmine for the mapbox base plot module
etpinard Jun 10, 2016
9255ca1
add tests for the scattermapbox trace module
etpinard Jun 10, 2016
b6e4734
exclude mapbox jasmine tests on CircleCI,
etpinard Jun 10, 2016
7167a9f
add mapbox image tests
etpinard Jun 10, 2016
7af55ea
skip mapbox mocks on CircleCI,
etpinard Jun 10, 2016
911e872
clean up update steps upon layer creation
etpinard Jun 13, 2016
a4a9100
improve description for pitch and bearing
etpinard Jun 13, 2016
28125a4
simplify purge layer step
etpinard Jun 13, 2016
2d6db7b
add 'mapbox.layers' to list of _isLinkedAtoArray attr to check
etpinard Jun 13, 2016
d99bfd5
make convert handle bubble with text case:
etpinard Jun 13, 2016
d2fe7f4
clear navigation container on map creation:
etpinard Jun 13, 2016
dceaece
Revert "tmp commit: register scattermapbox in main index file,"
etpinard Jun 13, 2016
fc34488
add scattermapbox trace module to lib/
etpinard Jun 13, 2016
4f2812d
register 'scattermapbox' in jasmine suite that rely on it
etpinard Jun 13, 2016
df77966
skip over mapbox image mock (for now),
etpinard Jun 13, 2016
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"gl-select-box": "^1.0.1",
"gl-spikes2d": "^1.0.1",
"gl-surface3d": "^1.2.3",
"mapbox-gl": "^0.18.0",
"mouse-change": "^1.1.1",
"mouse-wheel": "^1.0.2",
"ndarray": "^1.0.16",
Expand Down
3 changes: 3 additions & 0 deletions src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ module.exports = {
// URL to topojson files used in geo charts
topojsonURL: 'https://cdn.plot.ly/',

// Mapbox access token (required to plot mapbox trace types)
mapboxAccessToken: null,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

N.B. an important distinction for mapbox subplots.


// Turn all console logging on or off (errors will be thrown)
// This should ONLY be set via Plotly.setPlotConfig
logging: false
Expand Down
26 changes: 26 additions & 0 deletions src/plots/mapbox/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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';


module.exports = {
styleUrlPrefix: 'mapbox://styles/mapbox/',
styleUrlSuffix: 'v9',

noAccessTokenErrorMsg: [
'Missing Mapbox access token.',
'Mapbox trace type require a Mapbox access token to be registered.',
'For example:',
' Plotly.plot(gd, data, layout, { mapboxAccessToken: \'my-access-token\' });',
'More info here: https://www.mapbox.com/help/define-access-token/'
].join('\n'),

mapOnErrorMsg: 'Mapbox error.'
};
133 changes: 133 additions & 0 deletions src/plots/mapbox/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* 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 mapboxgl = require('mapbox-gl');

var Plots = require('../plots');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');

var createMapbox = require('./mapbox');
var constants = require('./constants');


exports.name = 'mapbox';

exports.attr = 'subplot';

exports.idRoot = 'mapbox';

exports.idRegex = /^mapbox([2-9]|[1-9][0-9]+)?$/;

exports.attrRegex = /^mapbox([2-9]|[1-9][0-9]+)?$/;

exports.attributes = {
subplot: {
valType: 'subplotid',
role: 'info',
dflt: 'mapbox',
description: [
'Sets a reference between this trace\'s data coordinates and',
'a mapbox subplot.',
'If *mapbox* (the default value), the data refer to `layout.mapbox`.',
'If *mapbox2*, the data refer to `layout.mapbox2`, and so on.'
].join(' ')
}
};

exports.layoutAttributes = require('./layout_attributes');

exports.supplyLayoutDefaults = require('./layout_defaults');

exports.plot = function plotMapbox(gd) {

if(!gd._context.mapboxAccessToken) {
throw new Error(constants.noAccessTokenErrorMsg);
}
else {
mapboxgl.accessToken = gd._context.mapboxAccessToken;
}

var fullLayout = gd._fullLayout,
calcData = gd.calcdata,
mapboxIds = Plots.getSubplotIds(fullLayout, 'mapbox');

for(var i = 0; i < mapboxIds.length; i++) {
var id = mapboxIds[i],
subplotCalcData = getSubplotCalcData(calcData, id),
mapbox = fullLayout[id]._subplot;

if(!mapbox) {
mapbox = createMapbox({
gd: gd,
container: fullLayout._glcontainer.node(),
id: id,
fullLayout: fullLayout,
staticPlot: gd._context.staticPlot
});

fullLayout[id]._subplot = mapbox;
}

mapbox.plot(subplotCalcData, fullLayout, gd._promises);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

pass the calcData to subplot .plot method.

}
};

exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var oldMapboxKeys = Plots.getSubplotIds(oldFullLayout, 'mapbox');

for(var i = 0; i < oldMapboxKeys.length; i++) {
var oldMapboxKey = oldMapboxKeys[i];

if(!newFullLayout[oldMapboxKey] && !!oldFullLayout[oldMapboxKey]._subplot) {
oldFullLayout[oldMapboxKey]._subplot.destroy();
}
}
};

exports.toSVG = function(gd) {
var fullLayout = gd._fullLayout,
subplotIds = Plots.getSubplotIds(fullLayout, 'mapbox'),
size = fullLayout._size;

for(var i = 0; i < subplotIds.length; i++) {
var opts = fullLayout[subplotIds[i]],
domain = opts.domain,
mapbox = opts._subplot;

var imageData = mapbox.toImage('png');
var image = fullLayout._glimages.append('svg:image');

image.attr({
xmlns: xmlnsNamespaces.svg,
'xlink:href': imageData,
x: size.l + size.w * domain.x[0],
y: size.t + size.h * (1 - domain.y[1]),
width: size.w * (domain.x[1] - domain.x[0]),
height: size.h * (domain.y[1] - domain.y[0]),
preserveAspectRatio: 'none'
});

mapbox.destroy();
}
};

function getSubplotCalcData(calcData, id) {
var subplotCalcData = [];

for(var i = 0; i < calcData.length; i++) {
var calcTrace = calcData[i],
trace = calcTrace[0].trace;

if(trace.subplot === id) subplotCalcData.push(calcTrace);
}

return subplotCalcData;
}
173 changes: 173 additions & 0 deletions src/plots/mapbox/layers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* 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 Lib = require('../../lib');


function MapboxLayer(mapbox, index, opts) {
this.mapbox = mapbox;
this.map = mapbox.map;

this.uid = mapbox.uid + '-' + 'layer' + index;

this.idSource = this.uid + '-source';
this.idLayer = this.uid + '-layer';

// some state variable to check if a remove/add step is needed
this.sourceType = null;
this.source = null;
this.layerType = null;
this.below = null;

// IMPORTANT: must create source before layer to not cause errors
this.updateSource(opts);
this.updateLayer(opts);
}

var proto = MapboxLayer.prototype;

proto.update = function update(opts) {
if(this.needsNewSource(opts)) {

// IMPORTANT: must delete layer before source to not cause errors
this.updateLayer(opts);
this.updateSource(opts);
}
else if(this.needsNewLayer(opts)) {
this.updateLayer(opts);
}

var paintOpts = convertPaintOpts(opts);

if(isVisible(opts)) {
this.mapbox.setOptions(this.idLayer, 'setPaintProperty', paintOpts);
}
};

proto.needsNewSource = function(opts) {
return (
this.sourceType !== opts.sourcetype ||
this.source !== opts.source
);
};

proto.needsNewLayer = function(opts) {
return (
this.layerType !== opts.type ||
this.below !== opts.below
);
};

proto.updateSource = function(opts) {
var map = this.map;

if(map.getSource(this.idSource)) map.removeSource(this.idSource);

this.sourceType = opts.sourcetype;
this.source = opts.source;

if(!isVisible(opts)) return;

var sourceOpts = convertSourceOpts(opts);

map.addSource(this.idSource, sourceOpts);
};

proto.updateLayer = function(opts) {
var map = this.map;

if(map.getLayer(this.idLayer)) map.removeLayer(this.idLayer);

this.layerType = opts.type;

if(!isVisible(opts)) return;

map.addLayer({
id: this.idLayer,
source: this.idSource,
'source-layer': opts.sourcelayer || '',
type: opts.type
}, opts.below);

// the only way to make a layer invisible is to remove it
var layoutOpts = { visibility: 'visible' };
this.mapbox.setOptions(this.idLayer, 'setLayoutProperty', layoutOpts);
};

proto.dispose = function dispose() {
var map = this.map;

map.removeLayer(this.idLayer);
map.removeSource(this.idSource);
};

function isVisible(opts) {
var source = opts.source;

// For some weird reason Lib.isPlainObject fails
// to detect `source` as a plain object in nw.js 0.12.

return (
typeof source === 'object' ||
(typeof source === 'string' && source.length > 0)
);
}

function convertPaintOpts(opts) {
var paintOpts = {};

switch(opts.type) {

case 'line':
Lib.extendFlat(paintOpts, {
'line-width': opts.line.width,
'line-color': opts.line.color,
'line-opacity': opts.opacity
});
break;

case 'fill':
Lib.extendFlat(paintOpts, {
'fill-color': opts.fillcolor,
'fill-outline-color': opts.line.color,
'fill-opacity': opts.opacity

// no way to pass line.width at the moment
});
break;
}

return paintOpts;
}

function convertSourceOpts(opts) {
var sourceType = opts.sourcetype,
source = opts.source,
sourceOpts = { type: sourceType },
isSourceAString = (typeof source === 'string'),
field;

if(sourceType === 'geojson') field = 'data';
else if(sourceType === 'vector') {
field = isSourceAString ? 'url' : 'tiles';
}

sourceOpts[field] = source;

return sourceOpts;
}

module.exports = function createMapboxLayer(mapbox, index, opts) {
var mapboxLayer = new MapboxLayer(mapbox, index, opts);
mapboxLayer.update(opts);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this call to update the layer required? (It's also the last line of the constructor).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not. I'll check this pm. Thanks for checking!


return mapboxLayer;
};
Loading