For more info about plotlyjs, refer to our online documentation page.
Last active
October 20, 2015 04:49
-
-
Save etpinard/fecb663134dc6d7fc013 to your computer and use it in GitHub Desktop.
Plotlyjs map with click event handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plotlyjs-map-with-click-events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/237913/jquery-2.1.4.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.5/d3.js"></script> | |
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/237913/plotly.min.js"></script> | |
</head> | |
<body> | |
<div id="plot"></div> | |
<script src="./main.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global Plotly:false, PLOTLYENV:false, d3:false */ | |
(function() { | |
PLOTLYENV = { TOPOJSON_URL: './' }; | |
var divid = 'plot', | |
currentLocation = null, | |
padding = 20; | |
function choroplethusa() { | |
var data = [{ | |
type: 'choropleth', | |
locationmode: 'USA-states', | |
locations: [ | |
'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', | |
'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', | |
'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', | |
'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', | |
'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', | |
'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', | |
'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY' | |
], | |
z: [ | |
1390.63, 13.31, 1463.17, 3586.02, 16472.88, 1851.33, | |
259.62, 282.19, 3764.09, 2860.84, 401.84, 2078.89, | |
8709.48, 5050.23, 11273.76, 4589.01, 1889.15, 1914.23, | |
278.37, 692.75, 248.65, 3164.16, 7192.33, 2170.8, 3933.42, | |
1718, 7114.13, 139.89, 73.06, 500.4, 751.58, 1488.9, | |
3806.05, 3761.96, 3979.79, 1646.41, 1794.57, 1969.87, 31.59, | |
929.93, 3770.19, 1535.13, 6648.22, 453.39, 180.14, 1146.48, | |
3894.81, 138.89, 3090.23, 349.69 | |
], | |
zmin: 0, | |
zmax: 17000, | |
colorscale: [ | |
[0, 'rgb(242,240,247)'], [0.2, 'rgb(218,218,235)'], | |
[0.4, 'rgb(188,189,220)'], [0.6, 'rgb(158,154,200)'], | |
[0.8, 'rgb(117,107,177)'], [1, 'rgb(84,39,143)'] | |
], | |
colorbar: { | |
title: 'Millions USD' | |
}, | |
marker: { | |
'line': { | |
'color': 'rgb(255,255,255)', | |
'width': 2 | |
} | |
} | |
}]; | |
var layout = { | |
title: '2011 US Agriculture Exports by State', | |
'geo': { | |
scope: 'usa', | |
showlakes: true, | |
lakecolor: 'rgb(255, 255, 255)' | |
} | |
}; | |
Plotly.plot(divid, data, layout); | |
attach(); | |
} | |
function attach() { | |
setTimeout(function() { | |
d3.selectAll('path.choroplethlocation') | |
.on('click', handleClick) | |
.on('dblclick.zoom', null); | |
}, 500); | |
} | |
function handleClick(d) { | |
if(d3.event.defaultPrevented) return; | |
var gd = d3.select('#plot').node(), | |
geoLayout = gd._fullLayout.geo, | |
geo = geoLayout._geo, | |
projection = geo.projection, | |
bb = d3.geo.path().projection(projection).bounds(d); | |
function invert(p) { return projection.invert(p); } | |
if(currentLocation === d.id) { | |
currentLocation = null; | |
Plotly.relayout(gd, | |
{ | |
'geo.lonaxis.range': geoLayout.lonaxis._fullRange, | |
'geo.lataxis.range': geoLayout.lataxis._fullRange | |
} | |
); | |
} | |
else { | |
currentLocation = d.id; | |
var lonlat0 = invert([bb[0][0] - padding, bb[0][1] - padding]), | |
lonlat1 = invert([bb[1][0] + padding, bb[1][1] + padding]); | |
Plotly.relayout(gd, | |
{ | |
'geo.lonaxis.range': [lonlat0[0], lonlat1[0]], | |
'geo.lataxis.range': [lonlat1[1], lonlat0[1]] | |
} | |
); | |
} | |
Plotly.Fx.loneUnhover(gd); | |
attach(); | |
} | |
choroplethusa(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment