nicolaskruchten / pivottable

Open-source Javascript Pivot Table (aka Pivot Grid, Pivot Chart, Cross-Tab) implementation with drag'n'drop.
https://pivottable.js.org/
MIT License
4.36k stars 1.08k forks source link

Add format to "List Unique Values" aggregator #1185

Open crestAT opened 4 years ago

crestAT commented 4 years ago

Hello, I tried to add a number format the "List Unique Values" aggregator but it seems to ignore it. If I do this with "Count Unique Values" then it's showing the desired format. Could you guide how to perform number format to the "List Unique Values" aggregator.

I tried this frFmt = nf({ thousandsSep: ".", decimalSep: "," }); and aggregators: { // call all aggregators with one appended whitespace char for nice output on c3 diagramms "Anzahl ": tpl.count(frFmtInt), "Anzahl eindeutiger Werte ": tpl.countUnique(frFmtInt), "Liste eindeutiger Werte ": tpl.listUnique(", ", frFmt), but without success. Thank you in advance!

nicolaskruchten commented 4 years ago

The basic reason this isn't working the way you might expect is that a list of unique values isn't a number, so it can't be formatted as a number. If you want to format the numbers within the string "1, 2, 3" you'll have to write a custom number formatter that splits the string on ", ", formats the individual pieces, and then joins them back together with that delimiter :)

crestAT commented 4 years ago

Thank you for the fast response and also for your great plugin!!!

Ok, I found a way to fulfill what you mentioned - kind of a hack but uses the addSeparators() function in pivot.js and is more or less a one-liner, fair enough for me.

Another formatting issue for me is the fixed formats eg usFmt defaults = { digitsAfterDecimal: 2, scaler: 1, thousandsSep: ",", decimalSep: ".", prefix: "", suffix: "" }; Since the application I'm working on is very dynamic I wanted to take decimals as they come from the server and I created an "open" format in regard to digitsAfterDecimal, very simple like this frFmtRaw = nf({ digitsAfterDecimal: -1, // use toString rather than toFixed thousandsSep: ".", decimalSep: "," }); and in pivot.js numberFormat = function(opts) { var defaults; defaults = { digitsAfterDecimal: 2, scaler: 1, thousandsSep: ",", decimalSep: ".", prefix: "", suffix: "" }; opts = $.extend({}, defaults, opts); return function(x) { var result; if (isNaN(x) || !isFinite(x)) { return ""; } if (opts.digitsAfterDecimal < 0) result = addSeparators((opts.scaler * x).toString(), opts.thousandsSep, opts.decimalSep); else result = addSeparators((opts.scaler * x).toFixed(opts.digitsAfterDecimal), opts.thousandsSep, opts.decimalSep); return "" + opts.prefix + result + opts.suffix; }; }; Here the if (opts.digitsAfterDecimal < 0) decides if a fixed format or no format in regard to digitsAfterDecimal should be used, just the thousands and decimals seperators should be set. It works but one problem apears here with the totals - if the values are integers everything is fine but on some tables there are decimals with 6 digits after the decimal. Here I get ugly results in the totals row/columns with many digits after the decimal, like 0,7576189999999999 instead of 0,757619. Since I'm aware of the problems which appears in Javascript on decimals I am searching WHERE the totals are created in pivot.js to implement a solution for this but maybe due to the lack of knowledge I'm unable to find the line where the sum is created. Maybe you could lead me to the right line ...

Thanks in advance !

daveisagit commented 4 years ago

Hi having similar DP float issue here too https://github.com/nicolaskruchten/pivottable/issues/1189#issue-584289957