diff options
author | sgiehl <stefan@piwik.org> | 2015-09-25 22:38:10 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2015-10-11 12:08:58 +0300 |
commit | a9b59fd73afd46c5fc03e0ced28dd7df42385b2d (patch) | |
tree | 2f8b4464e74890b567d47eafd6d92824900f760e /plugins/CoreVisualizations | |
parent | 7558648190511ff01ed72392888aa3bcf7f09e76 (diff) |
added number formatter for jqplot and use it for numbers in yaxis
Diffstat (limited to 'plugins/CoreVisualizations')
-rw-r--r-- | plugins/CoreVisualizations/javascripts/jqplot.js | 101 |
1 files changed, 100 insertions, 1 deletions
diff --git a/plugins/CoreVisualizations/javascripts/jqplot.js b/plugins/CoreVisualizations/javascripts/jqplot.js index f6914c57be..96c2822cd3 100644 --- a/plugins/CoreVisualizations/javascripts/jqplot.js +++ b/plugins/CoreVisualizations/javascripts/jqplot.js @@ -114,7 +114,8 @@ axes: { yaxis: { tickOptions: { - formatString: '%d' + formatString: '%d', + formatter: $.jqplot.NumberFormatter } } } @@ -747,6 +748,104 @@ RowEvolutionSeriesToggle.prototype.beforeReplot = function () { }; // ------------------------------------------------------------ +// PIWIK NUMBERFORMATTER PLUGIN FOR JQPLOT +// Handle number formats for piwik... +// ------------------------------------------------------------ +(function($){ + + var usesGrouping = (piwik.numbers.patternPositive.indexOf(',') != -1); + // if pattern has number groups, parse them. + if (usesGrouping) { + var primaryGroupMatches = piwik.numbers.patternPositive.match(/#+0/); + var primaryGroupSize = primaryGroupMatches[0].length; + var secondaryGroupSize = primaryGroupMatches[0].length; + var numberGroups = piwik.numbers.patternPositive.split(','); + // check for distinct secondary group size. + if (numberGroups.length > 2) { + secondaryGroupSize = numberGroups[1].length; + } + } + + function replaceSymbols(value) { + var replacements = { + '.': piwik.numbers.symbolDecimal, + ',': piwik.numbers.symbolGroup, + '+': piwik.numbers.symbolPlus, + '-': piwik.numbers.symbolMinus, + '%': piwik.numbers.symbolPercent + }; + + var newValue = ''; + var valueParts = value.split(''); + + $.each(valueParts, function(index, value) { + $.each(replacements, function(char, replacement) { + if (value.indexOf(char) != -1) { + value = value.replace(char, replacement); + return false; + } + }); + newValue += value; + }); + + return newValue; + } + + var minimumFractionDigits = 0; + var maximumFractionDigits = 2; + + $.jqplot.NumberFormatter = function (format, value) { + + if (!$.isNumeric(value)) { + return value; + } + // Ensure that the value is positive and has the right number of digits. + var negative = value < 0; + var pattern = negative ? piwik.numbers.patternNegative : piwik.numbers.patternPositive; + var signMultiplier = negative ? '-1' : '1'; + value = value * signMultiplier; + // Split the number into major and minor digits. + var valueParts = value.toString().split('.'); + var majorDigits = valueParts[0]; + // Account for maximumFractionDigits = 0, where the number won't + // have a decimal point, and $valueParts[1] won't be set. + minorDigits = valueParts[1] || ''; + if (usesGrouping) { + // Reverse the major digits, since they are grouped from the right. + majorDigits = majorDigits.split('').reverse(); + // Group the major digits. + var groups = []; + groups.push(majorDigits.splice(0, primaryGroupSize).reverse().join('')); + while (majorDigits.length) { + groups.push(majorDigits.splice(0, secondaryGroupSize).reverse().join('')); + } + // Reverse the groups and the digits inside of them. + groups = groups.reverse(); + // Reconstruct the major digits. + majorDigits = groups.join(','); + } + if (minimumFractionDigits < maximumFractionDigits) { + // Strip any trailing zeroes. + var minorDigits = minorDigits.replace(/0+$/,''); + if (minorDigits.length < minimumFractionDigits) { + // Now there are too few digits, re-add trailing zeroes + // until the desired length is reached. + var neededZeroes = minimumFractionDigits - minorDigits.length; + minorDigits += (new Array(neededZeroes+1)).join('0'); + } + } + // Assemble the final number and insert it into the pattern. + value = minorDigits ? majorDigits + '.' + minorDigits : majorDigits; + value = pattern.replace(/#(?:[\.,]#+)*0(?:[,\.][0#]+)*/, value); + // Localize the number. + value = replaceSymbols(value); + return value; + } + +})(jQuery); + + +// ------------------------------------------------------------ // PIWIK TICKS PLUGIN FOR JQPLOT // Handle ticks the piwik way... // ------------------------------------------------------------ |