Skip to content

Instantly share code, notes, and snippets.

@defvol
Created June 30, 2015 21:11
Show Gist options
  • Select an option

  • Save defvol/620b7ab9ed5a777c0d94 to your computer and use it in GitHub Desktop.

Select an option

Save defvol/620b7ab9ed5a777c0d94 to your computer and use it in GitHub Desktop.
Bubble tree de FONDEN usando la librería y API de OpenSpending
<html>
<head>
<meta charset="UTF-8"/>
<title>BubbleTree FONDEN (México)</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="http://openspending.org/static/openspendingjs/lib/vendor/jquery.history.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script type="text/javascript" src="http://openspending.org/static/openspendingjs/lib/vendor/vis4.js"></script>
<script type="text/javascript" src="http://openspending.org/static/openspendingjs/lib/vendor/Tween.js"></script>
<script type="text/javascript" src="http://openspending.org/static/openspendingjs/lib/vendor/bubbletree/2.0/bubbletree.js"></script>
<script type="text/javascript" src="https://openspending.org/static/openspendingjs/lib/aggregator.js"></script>
<script type="text/javascript" src="https://openspending.org/static/openspendingjs/lib/vendor/accounting.js"></script>
<link rel="stylesheet" type="text/css" href="http://openspending.org/static/openspendingjs/lib/vendor/bubbletree/1.0/bubbletree.css" />
<style>
.tooltip {
position: absolute;
bottom: 0;
right: 0;
margin: 20px;
font-size: 1.2em;
}
</style>
<script type="text/javascript">
function randomColorForIndex(i) {
return vis4color.fromHSL(i / 32 * 360, .7, .5).x;
}
function generateChildrenByBreakdowns(node) {
$.each(node.breakdowns, function(i, e) {
if (e.amount > 0) {
e.color = randomColorForIndex(i);
node.children.push(e);
}
});
}
$(function() {
var $tooltip = $('<span class="tooltip"></span>');
$('body').append($tooltip);
$tooltip.hide();
new OpenSpending.Aggregator({
apiUrl: 'https://openspending.org/api/2/aggregate?',
dataset: 'segob_fonden',
drilldowns: ['type'],
cuts: ['year:2013'],
rootNodeLabel: 'Total',
breakdown: 'state',
callback: function(data) {
$('.bubbletree').empty();
$tooltip.html(data.label + ': ' + data.amount);
$tooltip.show();
new BubbleTree({
data: data,
container: '.bubbletree',
tooltip: function(e) {
$tooltip.html(e.node.label + ': ' + e.node.amount);
}
});
$.each(data.children, function(index, element) {
generateChildrenByBreakdowns(element);
});
console.log(data);
}
});
});
</script>
</head>
<body>
<div class="bubbletree-wrapper">
<div class="bubbletree"></div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment