Skip to content

Commit

Permalink
This is a fix for Github issue: Fix number display for data points in…
Browse files Browse the repository at this point in the history
… sidebar DCActionforChildren#29

https://github.com/nickmcclellan/dcaction/issues/29
Numbers are whole.
Percentages become percents.
Dollar amounts display as dollars.
  • Loading branch information
akilism committed Jan 30, 2014
1 parent 03884e6 commit d0afc64
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion js/visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ function drawChoropleth(){
.await(setUpChoropleth);

function setUpChoropleth(error, dc, choropleth) {
choropleth_data = choropleth;
//clean choropleth data for display.
choropleth_data = cleanData(choropleth);
choropleth_data.forEach(function(d) {
all_data[d.gis_id] = d;
choropleth_data[d.gis_id] = +d.population_total;
Expand Down Expand Up @@ -221,6 +222,10 @@ function drawSchools(){
.on("click", displaySchoolData)
.append("title").text(function(d){return d.name;});
packMetros();

//clean school data for display.
school_data = cleanData(school_data);

function displaySchoolData(school) {
$("#details").prepend("<div class='well well-sm'><h3>"+school.name+"</h3><h4>enrollment: " +
school.enrollment + "</h4><h4>allocation: " +
Expand Down Expand Up @@ -312,3 +317,50 @@ function hoverNeighborhood(d) {
displayPopBox(d);
}
}

function cleanData(rawData) {
var d, cleanedData = [];

for (var i = 0, len = rawData.length; i < len; i++) {
d = rawData[i];

for(var key in d){
if(d.hasOwnProperty(key) && !isNaN(parseInt(d[key], 10))) {
d[key] = cleanNumber(d[key], key);
}
}

cleanedData.push(d);
}

return cleanedData;

function cleanNumber(strNum, name) {
var num = parseFloat(strNum);

if (name.indexOf('_perc') !== -1) { //percentage
num = num * 100;
num = Math.round(num);
return num + '%';
} else if (name.indexOf('alloc') !== -1) { //some kind of allocation amount
return '$' + num.addCommas();
}

num = Math.round(num)
return num.addCommas(0);
}
}

Number.prototype.addCommas = function(decimalPlaces) {
var n = this,
c = isNaN(decimalPlaces) ? 2 : Math.abs(decimalPlaces),
d = '.',
t = ',',
sign = (n < 0) ? '-' : '',
i = parseInt(n = Math.abs(n).toFixed(c), 10) + '',
initialDigits = ((initialDigits = i.length) > 3) ? initialDigits % 3 : 0;

return sign + (initialDigits? i.substr(0, initialDigits) + t : '')
+ i.substr(initialDigits).replace(/(\d{3})(?=\d)/g, '$1' + t)
+ (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
};

0 comments on commit d0afc64

Please sign in to comment.