From 1e1bce0606661d66dea8a35f7383bc0e0f33f530 Mon Sep 17 00:00:00 2001 From: Adam Weingarten Date: Tue, 22 Apr 2014 14:16:44 -0400 Subject: [PATCH] ON-50: Added numerical formatting to the numerical output on choropleth map. --- choropleth.module | 2 +- static/recline.view.ChoroplethMap.js | 92 ++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/choropleth.module b/choropleth.module index e6e467e..816b8cf 100644 --- a/choropleth.module +++ b/choropleth.module @@ -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, diff --git a/static/recline.view.ChoroplethMap.js b/static/recline.view.ChoroplethMap.js index 861a77d..f2fd318 100644 --- a/static/recline.view.ChoroplethMap.js +++ b/static/recline.view.ChoroplethMap.js @@ -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; @@ -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; + }, + + /** @@ -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 @@ -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 = ' \ {{name}} \ @@ -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,