Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
ON-50: Added numerical formatting to the numerical output on chorople…
Browse files Browse the repository at this point in the history
…th map.
  • Loading branch information
Adam Weingarten committed Apr 23, 2014
1 parent cd486b6 commit 1e1bce0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion choropleth.module
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function choropleth_field_widget_form_alter(&$element, &$form_state, $context) {
$choropleth_breakpoints = isset($element[$delta]['#default_value']['choropleth_breakpoints']) ? $element[$delta]['#default_value']['choropleth_breakpoints'] : FALSE;
$element[$delta]['choropleth_breakpoints'] = array(
'#title' => 'Color Breakpoints',
'#description' => t('For the state-by-state map (if applicable) determine what values will be used as "breakpoints" for color shading. Enter breakpoints as a comma separated list. For example, if you want your map to include 4 colors where 0-24 is visualized as the lightest color, 25-49 medium light, 50-74 dark, 75-100 darkest, you would enter this: 25, 50, 75..'),
'#description' => t('For the state-by-state map (if applicable) determine what values will be used as "breakpoints" for color shading. Enter breakpoints as a comma separated list. Please note you a space is required after each comma. For example, if you want your map to include 4 colors where 0-24 is visualized as the lightest color, 25-49 medium light, 50-74 dark, 75-100 darkest, you would enter this: 25, 50, 75..'),
'#type' => 'textfield',
'#weight' => 4,
'#default_value' => $choropleth_breakpoints,
Expand Down
92 changes: 87 additions & 5 deletions static/recline.view.ChoroplethMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,17 @@ this.recline.View = this.recline.View || {};
return breakpoints;
},

// When we add formatting framework to choropleth we should revisit these
// special Units functions and replace.


/**
* Returns the special chars that we might want to strip from the prefix
* or suffix of a number
* @return {string[]}
* Array containing the special units chars
*/

_getSpecialUnits:function(){
var specialUnits = ["$", "%"];
return specialUnits;
Expand Down Expand Up @@ -256,37 +265,81 @@ this.recline.View = this.recline.View || {};
*
* @return {[type]} [description]
*/
_stripSpecialUnitsFromNumber: function(n) {
_formatSpecialUnitsNumber: function(n) {
// This is a good place to add new units.
var specialUnits = this._getSpecialUnits();

var suffix="";
var prefix="";

if (n) {
n = n.toString().trim();

if (this._isSpecialUnitSuffux(n, specialUnits)) {
suffix = n.substr(n.length - 1,1);
n = n.substr(0, n.length - 1);
}

if (this._isSpecialUnitPrefix(n, specialUnits)) {
prefix = n.substr(0,1);
n = n.substr(1, n.length - 1);
}
//When we add formatting framework to choropleth we should revisit this line
return numeral().unformat(n, specialUnits);
}

if (this._isSpecialUnitSuffux(n, specialUnits)) {
suffix = n.substr(n.length - 1,1);
n = n.substr(0, n.length - 1);
}

if (this._isSpecialUnitPrefix(n, specialUnits)) {
prefix = n.substr(0,1);
n = n.substr(1, n.length - 1);
}

return numeral().unformat(n);
//When we add formatting framework to choropleth we should revisit this line
return prefix + (numeral().unformat(n)).toLocaleString() + suffix;
},



/**
* Remove the special units from a number
* @param {string|int} n
*
* @return {[type]} [description]
*/
_stripSpecialUnitsFromNumber: function(n) {
// This is a good place to add new units.
var specialUnits = this._getSpecialUnits();


if (n) {
n = n.toString().trim();

if (this._isSpecialUnitSuffux(n, specialUnits)) {
n = n.substr(0, n.length - 1);
}

if (this._isSpecialUnitPrefix(n, specialUnits)) {
n = n.substr(1, n.length - 1);
}
return n;
//return numeral().unformat(n, specialUnits);
}

if (this._isSpecialUnitSuffux(n, specialUnits)) {
n = n.substr(0, n.length - 1);
}

if (this._isSpecialUnitPrefix(n, specialUnits)) {
n = n.substr(1, n.length - 1);
}

//return numeral().unformat(n);
return n;
},




/**
Expand Down Expand Up @@ -579,6 +632,22 @@ this.recline.View = this.recline.View || {};
// Set flag.
this.mapReady = true;
},

/**
* Counts the number of decimal places
* @params {string} value
* A string to be parsed
* @return {int}
* number of decimal places in the string
*/
_countDecimals: function (value) {
if(Math.floor(value) == value)
return 0;

return value.toString().split(".")[1].length || 0;
},


/**
* Provides the content for the top-right control.
* @params {object} feature
Expand All @@ -591,6 +660,7 @@ this.recline.View = this.recline.View || {};
var filtered_records = self._filteredRecordsForPoly(feature.properties.name);
var units = state['columnToDisplay'];
var v = 0;
var desired_num_decimal_places = 0;
var n = filtered_records.length;
var template = ' \
<b>{{name}}</b> \
Expand All @@ -602,10 +672,22 @@ this.recline.View = this.recline.View || {};
if (!units && units_index > -1) {
units = value[units_index];
}
v += self._stripSpecialUnitsFromNumber(value[field_index]);
var v_temp = self._stripSpecialUnitsFromNumber(value[field_index]);
desired_num_decimal_places = Math.max(desired_num_decimal_places, self._countDecimals(v_temp));
v += numeral().unformat(v_temp);
});

//When we add formatting framework to choropleth we should revisit this line
v = numeral().unformat(v).toLocaleString();

var current_num_decimal_places = self._countDecimals(v);
// Dropped off a trailing zero.
if(desired_num_decimal_places > current_num_decimal_places)
{
var num_dropped_trailing_zeros = desired_num_decimal_places-current_num_decimal_places;
var dropped_trailing_zeros = Array(num_dropped_trailing_zeros + 1).join("0");
v = v+""+dropped_trailing_zeros;
}

return Mustache.render(
template,
Expand Down

0 comments on commit 1e1bce0

Please sign in to comment.