Basic sprintf support (#1965)

Implement optional format strings for valueMetadata which allows
developers to use c-style sprintf formatters as desired. Fixes #1562
This commit is contained in:
Pete Richards
2018-06-29 15:52:15 -07:00
committed by Andrew Henry
parent 29de11167f
commit 013eba744d
6 changed files with 20 additions and 4 deletions

View File

@ -30,6 +30,7 @@ define([
{ {
key: "sin", key: "sin",
name: "Sine", name: "Sine",
formatString: '%0.2f',
hints: { hints: {
range: 1 range: 1
} }
@ -37,6 +38,7 @@ define([
{ {
key: "cos", key: "cos",
name: "Cosine", name: "Cosine",
formatString: '%0.2f',
hints: { hints: {
range: 2 range: 2
} }

View File

@ -37,6 +37,7 @@ module.exports = function(config) {
{pattern: 'bower_components/**/*.js', included: false}, {pattern: 'bower_components/**/*.js', included: false},
{pattern: 'node_modules/d3-*/**/*.js', included: false}, {pattern: 'node_modules/d3-*/**/*.js', included: false},
{pattern: 'node_modules/vue/**/*.js', included: false}, {pattern: 'node_modules/vue/**/*.js', included: false},
{pattern: 'node_modules/printj/dist/*.js', included: false},
{pattern: 'src/**/*', included: false}, {pattern: 'src/**/*', included: false},
{pattern: 'node_modules/painterro/build/*.js', included: false}, {pattern: 'node_modules/painterro/build/*.js', included: false},
{pattern: 'node_modules/html2canvas/dist/*', included: false}, {pattern: 'node_modules/html2canvas/dist/*', included: false},

View File

@ -50,7 +50,8 @@ requirejs.config({
"d3-time": "node_modules/d3-time/build/d3-time.min", "d3-time": "node_modules/d3-time/build/d3-time.min",
"d3-time-format": "node_modules/d3-time-format/build/d3-time-format.min", "d3-time-format": "node_modules/d3-time-format/build/d3-time-format.min",
"html2canvas": "node_modules/html2canvas/dist/html2canvas.min", "html2canvas": "node_modules/html2canvas/dist/html2canvas.min",
"painterro": "node_modules/painterro/build/painterro.min" "painterro": "node_modules/painterro/build/painterro.min",
"printj": "node_modules/printj/dist/printj.min"
}, },
"shim": { "shim": {
"angular": { "angular": {

View File

@ -51,6 +51,7 @@
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"moment": "^2.11.1", "moment": "^2.11.1",
"node-bourbon": "^4.2.3", "node-bourbon": "^4.2.3",
"printj": "^1.1.0",
"requirejs": "2.1.x", "requirejs": "2.1.x",
"split": "^1.0.0", "split": "^1.0.0",
"v8-compile-cache": "^1.1.0" "v8-compile-cache": "^1.1.0"

View File

@ -21,9 +21,11 @@
*****************************************************************************/ *****************************************************************************/
define([ define([
'lodash' 'lodash',
'printj'
], function ( ], function (
_ _,
printj
) { ) {
// TODO: needs reference to formatService; // TODO: needs reference to formatService;
@ -71,6 +73,14 @@ define([
return Number(string); return Number(string);
}.bind(this); }.bind(this);
} }
// Check for formatString support once instead of per format call.
if (valueMetadata.formatString) {
var baseFormat = this.formatter.format;
var formatString = valueMetadata.formatString;
this.formatter.format = function (value) {
return printj.sprintf(formatString, baseFormat.call(this, value));
};
}
} }
TelemetryValueFormatter.prototype.parse = function (datum) { TelemetryValueFormatter.prototype.parse = function (datum) {

View File

@ -76,7 +76,8 @@ requirejs.config({
"d3-time": "node_modules/d3-time/build/d3-time.min", "d3-time": "node_modules/d3-time/build/d3-time.min",
"d3-time-format": "node_modules/d3-time-format/build/d3-time-format.min", "d3-time-format": "node_modules/d3-time-format/build/d3-time-format.min",
"html2canvas": "node_modules/html2canvas/dist/html2canvas.min", "html2canvas": "node_modules/html2canvas/dist/html2canvas.min",
"painterro": "node_modules/painterro/build/painterro.min" "painterro": "node_modules/painterro/build/painterro.min",
"printj": "node_modules/printj/dist/printj.min"
}, },
"shim": { "shim": {