Skip to content

Instantly share code, notes, and snippets.

@mapsense-examples
Last active August 29, 2015 14:21
Show Gist options
  • Save mapsense-examples/84ae92261794f5d8db19 to your computer and use it in GitHub Desktop.
Save mapsense-examples/84ae92261794f5d8db19 to your computer and use it in GitHub Desktop.
Data-Driven Visualization
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js" charset="utf-8"></script>
<script src="https://developer.mapsense.co/mapsense.js" charset="utf-8"></script>
<link type="text/css" href="https://developer.mapsense.co/mapsense.css" rel="stylesheet"/>
<style>
body, #myMap{
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="myMap"><div/>
<script>
var colors = ['rgb(158,1,66)','rgb(213,62,79)','rgb(244,109,67)','rgb(253,174,97)','rgb(254,224,139)','rgb(255,255,191)','rgb(230,245,152)','rgb(171,221,164)','rgb(102,194,165)','rgb(50,136,189)','rgb(94,79,162)'],
colorIndex = 0,
colorHash = {};
var map = mapsense
.map("#myMap") // init the map
.add(mapsense.basemap().apiKey("key-2d5eacd8b924489c8ed5e8418bd883bc").style("dark"))
.zoom(2.3)
.center({lon: -80.92137305637061, lat: 30.315819754033953});
var colorRamp = d3.scale.linear()
.domain([900,950,1100])
.range(['rgb(237,248,177)','rgb(127,205,187)','rgb(44,127,184)']);
var sizeRamp = d3.scale.linear()
.domain([0,250])
.range([1,30]);
var hurricaneUrl = "https://{S}-api.mapsense.co/explore/api/universes/mapsense.hurricanes/{Z}/{X}/{Y}.topojson?api-key=key-2d5eacd8b924489c8ed5e8418bd883bc&where=(1335066259296%3C=datetime%3C=1409724000000)&density=175";
var hurricaneLayer = mapsense.topoJson()
.url(mapsense.url(hurricaneUrl).hosts(['a','b','c','d']))
.selection(
function(d){
d.attr("class", "points")
.style("fill", function(d){
// console.log(d.properties);
if(!colorHash[d.properties.name]) {
colorHash[d.properties.name] = getRandomColor();
colorIndex = (colorIndex + 1 ) % colors.length;
}
return colorHash[d.properties.name];
})
.attr("r", function(d){
if (!d.properties.max_sustained_wind){
return 4.5;
}
else{
return Math.abs(sizeRamp(d.properties.max_sustained_wind));
}
})
}
)
.clip(false)
.scale("fixed");
map.add(hurricaneLayer);
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment