Skip to content

Instantly share code, notes, and snippets.

@oriolbx
Last active June 28, 2016 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oriolbx/edf8327839d4cb235027e6bbccc3f753 to your computer and use it in GitHub Desktop.
Save oriolbx/edf8327839d4cb235027e6bbccc3f753 to your computer and use it in GitHub Desktop.
deep-insights-js: toggle layers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name=viewport content="width=device-width initial-scale=1">
<link rel="stylesheet" href="http://libs.cartocdn.com/di.js/v0/themes/css/deep-insights.css" />
<script src=" https://code.jquery.com/jquery-3.0.0.min.js"></script>
<script src=" http://libs.cartocdn.com/di.js/v0/deep-insights.uncompressed.js"></script>
<style type="text/css">
html, body {
position:relative;
height: 100%;
padding: 0;
margin: 0;
}
.CDB-Chart-bar {
fill: #ff0000 !important;
}
.CDB-Chart-bar--timeSeries{
fill: #00ff0f !important;
}
</style>
</head>
<body>
<div id="dashboard"></div>
<div style="top: 40px;bottom: auto;position: absolute;z-index: 100;left: 105px;">
<button class="CDB-Button CDB-Button--primary CDB-Button--big" id="button1">
<span class="CDB-Button-Text CDB-Text is-semibold CDB-Size-medium">Layer1</span>
</button>
<button class="CDB-Button CDB-Button--primary CDB-Button--big" id="button2">
<span class="CDB-Button-Text CDB-Text is-semibold CDB-Size-medium">Layer2</span>
</button>
</div>
<script>
var layer1;
var layer2;
window.onload = function() {
// to make the deep insights work, use v3 instead of v2 (v2 is the original url)
var vizJSON = "https://team.cartodb.com/u/oboix/api/v3/viz/35e4f89e-1204-11e6-beab-0e674067d321/viz.json";
cartodb.deepInsights.createDashboard('#dashboard', vizJSON, {
no_cdn: false,
cartodb_logo: false,
},function(err,dashboard){
// update map layers
var map = dashboard.getMap();
layer1 = map.getLayer(1);
layer2 = map.getLayer(2);
var params = {
"type": "histogram",
"title": "Minimum population",
"column": "pop_min",
"operation": "avg",
"bins": 10
};
var params2 = {
"type": "time-series",
"column": "_year",
"bins": 10
}
dashboard.createHistogramWidget(params,map.getLayer(1))
dashboard.createTimeSeriesWidget(params2, map.getLayer(1))
});
$("#button1").click(function(){
if (layer1.attributes.visible == true){
layer1.hide();
$(".cartodb-legend-stack").css("display","none")
}else{
layer1.show();
$(".cartodb-legend-stack").css("display","block")
}
})
$("#button2").click(function(){
if (layer2.attributes.visible == true){
layer2.hide();
}else{
layer2.show();
}
})
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment