diff --git a/plugins/CoreVisualizations/javascripts/jqplot.js b/plugins/CoreVisualizations/javascripts/jqplot.js index f6914c57be0d8c0b451b6a87f94e111081d69c4d..96c2822cd312bbe331bec2c73b2cb7964420a3a0 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 } } } @@ -746,6 +747,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... diff --git a/plugins/Morpheus/templates/_jsGlobalVariables.twig b/plugins/Morpheus/templates/_jsGlobalVariables.twig index 35643089eae36c081f7aab52be9d8a02ca3f284b..ed38f98e67763172136b9f992548bd5496161faa 100644 --- a/plugins/Morpheus/templates/_jsGlobalVariables.twig +++ b/plugins/Morpheus/templates/_jsGlobalVariables.twig @@ -3,6 +3,18 @@ piwik.token_auth = "{{ token_auth }}"; piwik.piwik_url = "{{ piwikUrl }}"; piwik.cacheBuster = "{{ cacheBuster }}"; + + piwik.numbers = { + patternPositive: "{{ 'Intl_NumberFormat'|translate }}", + patternNegative: "{{ 'Intl_NumberFormatNegative'|translate }}", + patternPercent: "{{ 'Intl_NumberFormatPercent'|translate }}", + symbolPlus: "{{ 'Intl_NumberSymbolPlus'|translate }}", + symbolMinus: "{{ 'Intl_NumberSymbolMinus'|translate }}", + symbolPercent: "{{ 'Intl_NumberSymbolPercent'|translate }}", + symbolGroup: "{{ 'Intl_NumberSymbolGroup'|translate }}", + symbolDecimal: "{{ 'Intl_NumberSymbolDecimal'|translate }}" + }; + {% if userLogin %}piwik.userLogin = "{{ userLogin|e('js')}}";{% endif %} {% if idSite is defined %}piwik.idSite = "{{ idSite }}";{% endif %}