-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
70856b2
3968417
3648744
6254578
6a73378
3e79f0d
3ea7436
3b2c2e2
a47704c
f300d27
9255ca1
b6e4734
7167a9f
7af55ea
911e872
a4a9100
28125a4
2d6db7b
d99bfd5
d2fe7f4
dceaece
fc34488
4f2812d
df77966
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
module.exports = require('../src/traces/scattermapbox'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1648,7 +1648,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) { | |
// to not go through a full replot | ||
var doPlotWhiteList = ['cartesian', 'pie', 'ternary']; | ||
fullLayout._basePlotModules.forEach(function(_module) { | ||
if(doPlotWhiteList.indexOf(_module.name) === -1) doplot = true; | ||
if(doPlotWhiteList.indexOf(_module.name) === -1) docalc = true; | ||
}); | ||
|
||
// make a new empty vals array for undoit | ||
|
@@ -2286,6 +2286,19 @@ Plotly.relayout = function relayout(gd, astr, val) { | |
Images.supplyLayoutDefaults(gd.layout, gd._fullLayout); | ||
Images.draw(gd); | ||
} | ||
else if(p.parts[0] === 'mapbox' && p.parts[1] === 'layers') { | ||
Lib.extendDeepAll(gd.layout, Lib.objectFromPath(ai, vi)); | ||
|
||
// append empty container to mapbox.layers | ||
// so that relinkPrivateKeys does not complain | ||
|
||
var fullLayers = (gd._fullLayout.mapbox || {}).layers || []; | ||
var diff = (p.parts[2] + 1) - fullLayers.length; | ||
|
||
for(i = 0; i < diff; i++) fullLayers.push({}); | ||
|
||
doplot = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. merging this logic with |
||
} | ||
// alter gd.layout | ||
else { | ||
// check whether we can short-circuit a full redraw | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* 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', | ||
|
||
controlContainerClassName: 'mapboxgl-control-container', | ||
|
||
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.' | ||
}; |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pass the |
||
} | ||
}; | ||
|
||
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
npm run start
now requires devs to sign up for mapbox and runnpm run pretest
Maybe we should make this less stringent?