Flourish number localization
Flourish module for setting the numeric separators used in a template. Used in conjunction with Flourish number formatter.
Install
To install the module, type:
npm install @flourish/number-localization
To add the module's settings block, include the following in a templates' template.yml file:
- property: localization
import: "@flourish/number-localization"
Usage
Include a (usually empty) object in the state declaration:
var state = {
/* Other declarations of default values */
localization: {},
/* Any further state properties */
}
Make sure an instance of the module is initialised somewhere in the code before draw is called:
import initLocalization from "@flourish/number-localization";;
/* Other imports and code */
var localization = initLocalization(state.localization);
/* Other initialisation code, eg Flourish-number-formatter instances */
export { localization/*, etc */ };
This will add two properties to state.localization
(if they don't already exist), input_decimal_separator
(defaults to ".") and output_separators
(defaults to ",."). The exported localization
object contains two methods, getParser
and getFormatterFunction
.
The getParser
method returns a function for parsing text into numbers based on the value of input_decimal_separator
at the time it is called;
import { localization } from "init.js";
var parser = localization.getParser();
["1.2", "-4,000", "$326k"].map(parser) // Returns [1.2, -4000, 326] for input_decimal_separator === "."
// The above would return [12, -4, 326] for input_decimal_separator === ","
The getFormatterFunction
method creates a locale function based on the value of output_separators
at the time it is called. It is meant to be passed on to instances of Flourish number formatter:
import { localization, getXAxisFormatter, getYAxisFormatter } from "init.js";
var localeFunction = localization.getFormatterFunction();
var xFormat = getXAxisFormatter(localeFunction);
var yFormat = getYAxisFormatter(localeFunction);