Fernando Blat // @ferblape
d3.csv('/datasets/presupuestos-municipales.csv', function(error, data){
// Format the data
data.forEach(function(d){
d.budget = +d.budget;
d.population = +d.population;
d.year = +d.year;
d.date = new Date(d.year, 0, 1);
d.budgetPerInhabitant = (d.budget / d.population);
});
// Construct a new crossfilter
var budgets = crossfilter(data);
// Insert your code here
// ...
});
data[0]
{year: 2010, city: "Albacete",
budget: 173159214.03, population: 171390,
province: "Albacete",...}
d3.csv('/datasets/presupuestos-municipales.csv', function(error, data){
// Format the data
data.forEach(function(d){
d.budget = +d.budget;
...
});
// Construct a new crossfilter
var budgets = crossfilter(data);
var yearsDim = budgets.dimension(function(d){ return d.date; });
var provinceDim = budgets.dimension(function(d){ return d.province; });
var yearsDim = budgets.dimension(function(d){ return d.date; });
var budgetPerYearGroup = yearsDim
.group()
.reduceSum(function(d){
return d.budget;
});
> budgetPerYearGroup.top(10).map(function(d){console.log(d);});
[Log] {key: Fri Jan 01 2010 00:00:00, value: 19953790555.04}
[Log] {key: Sat Jan 01 2011 00:00:00, value: 18620250780.48}
[Log] {key: Sun Jan 01 2012 00:00:00, value: 18106387923.29}
[Log] {key: Thu Jan 01 2015 00:00:00, value: 17121299191.05}
[Log] {key: Wed Jan 01 2014 00:00:00, value: 16805983230.21}
[Log] {key: Tue Jan 01 2013 00:00:00, value: 16069487367.37}
var yearsDim = budgets.dimension(function(d){ return d.date; });
var meanBudgetPerYearGroup = yearsDim
.group()
.reduce(addElement, removeElement,
initialize);
var initialize = function(){
return {
count: 0,
totalBudget: 0,
totalBudgetPerInhabitant: 0
};
}
var addElement = function(p, v){
p.count++;
p.totalBudget += v.budget;
p.totalBudgetPerInhabitant += v.budgetPerInhabitant;
p.meanBudget = p.totalBudget / p.count;
p.meanBudgetPerInhabitant = p.totalBudgetPerInhabitant / p.count;
return p;
}
var removeElement = function(p, v){
p.count--;
p.totalBudget -= v.budget;
p.totalBudgetPerInhabitant -= v.budgetPerInhabitant;
p.meanBudget = p.totalBudget / p.count;
p.meanBudgetPerInhabitant = p.totalBudgetPerInhabitant / p.count;
return p;
}
> meanBudgetPerYearGroup.top(2).map(function(d){console.log(d);});
[Log] {key: Tue Jan 01 2011 00:00:00, {count: 50,
totalBudget: 18620250780.48, meanBudget: 372405015.60}}
[Log] {key: Tue Jan 01 2012 00:00:00, {count: 50,
totalBudget: 17232302324.38, meanBudget: 632403010.82}}
d3.csv('/datasets/presupuestos-municipales.csv', function(error, data){
// Format the data
data.forEach(function(d){
d.budget = +d.budget;
...
});
// Construct a new crossfilter
var budgets = crossfilter(data);
var yearsDim = budgets.dimension(function(d){ return d.date; });
var budgetPerYearGroup = yearsDim.group().reduceSum(function(d){ return d.budget; });
yearsChart = dc.pieChart("#years");
...
yearsChart = dc.pieChart("#years");
yearsChart
.dimension(yearsDim)
.group(budgetPerYearGroup)
.title(function(d){
return accounting.formatNumber(d.value, {precision: 0}) + '€';
})
.label(function(d){
return d.key.getFullYear();
});
...
autonomousRegionsChart = dc.rowChart("#autonomous-regions");
autonomousRegionsChart
.dimension(autonomousRegionsDim)
.group(budgetPerAutonomousRegionGroup)
.title(function(d) {
return d.key + " " + accounting.formatNumber(d.value);
})
.elasticX(true)
.height(400)
.xAxis().ticks(2);
evolutionChart = dc.lineChart("#evolution");
evolutionChart
.dimension(yearsDim)
.group(meanBudgetPerYearGroup)
.margins({top: 50, right: 50, bottom: 25, left: 90})
.x(d3.time.scale().domain([new Date(2010, 0, 1), new Date(2015, 0, 1)]))
.valueAccessor(function(d) {
return d.value.meanBudgetPerInhabitant;
})
.yAxisPadding(50)
.title(function(d) {
return d.key + " " + accounting.formatNumber(d.value);
})
.height(250)
.elasticY(true)
.yAxis()
.tickFormat(function(v){return accounting.formatNumber(v, {precision: 0}) + '€';});
mapChart = dc.geoChoroplethChart("#map");
d3.json("provinces_carto.geojson", function(error, json){
var colors = ["#d75231", "#ec8b66", "#fcdbc8", "#eff3ff", "#a0cae0", "#4893c4", "#022977"];
mapChart
.height(450)
.colors(colors)
.dimension(provinceDim)
.group(budgetPerProvinceGroup)
.valueAccessor(function(d){
return d.value.meanBudgetPerInhabitant;
})
.overlayGeoJson(json.features, "provinces", function(p){
return p.properties.nombre99;
})
.on('preRender', function(chart, filter){
})
.on('preRedraw', function(chart, filter){
})
.on('renderlet', function(){
});
});
.on('preRender', function(chart, filter){
var mapColors = d3.scale
.quantize()
.domain(d3.extent(chart.group().all().map(function(d){
return d.value.meanBudgetPerInhabitant;
})))
.range(colors);
chart
.colors(mapColors)
.colorCalculator(function(d){
return (d !== undefined) ? mapColors(d) : '#ccc';
});
})
.on('preRedraw', function(chart, filter){
// Same code
})
.on('renderlet', function(){
if(d3.select("#map-legend svg")[0][0] !== null) {
var e = document.getElementById('map-legend');
e.innerHTML = '';
}
var mapColors = d3.scale
.quantize()
.domain(d3.extent(mapChart.group().all().map(function(d){
return d.value.meanBudgetPerInhabitant;
})))
.range(colors);
var svg = d3.select("#map-legend")
.append("svg")
.attr('width',500);
svg.append("g")
.attr("class", "legendLinear");
var legendLinear = d3.legend.color()
.shapeWidth(40)
.cells(mapColors.domain())
.orient('vertical')
.labelFormat(function(v){
return accounting.formatNumber(v, {precision: 0});
})
.scale(mapColors);
svg.select(".legendLinear")
.call(legendLinear);
})