2

I am using pivotable.js library, in this pivotable.js I want show average values of all cells in the total column instead of total of all cell values. Please have a look at the image below, which show how I want to display values in in pivottable.js

enter image description here

var average = $.pivotUtilities.aggregatorTemplates.average;
var numberFormat = $.pivotUtilities.numberFormat;
var intFormat = numberFormat({digitsAfterDecimal: 1});

$("#output").pivot(
  [
    {color: "green", shape: "null", value: 0},
    {color: "blue", shape: "circle", value: 1},
    {color: "red", shape: "triangle", value: 2},
    {color: "red", shape: "triangle", value: 12},
    {color: "blue", shape: "circle", value: 3},
    {color: "blue", shape: "triangle", value: 12}
  ],
  {
    rows: ["color"],
    cols: ["shape"],
    aggregator: average(intFormat)(["value"])
  }
);

Could you please let me know how can I achieve this. Here is fiddle

https://jsfiddle.net/dky0hh1y/5/

My custom aggregator

 var successRate = function() {
  return function() {
    return {
      sumSuccesses: 0,
      sumTrials: 0,
      push: function(record) {
        if (!isNaN(parseFloat(record.student))) {
          this.sumSuccesses += parseFloat(record.student);
        }

      },
      value: function() { return this.sumSuccesses },
      format: function(x) { return x; },
      numInputs: 0
    };
  };
};

Thanks

niran
  • 1,920
  • 8
  • 34
  • 60

1 Answers1

0

PivoTable.js uses the same aggregator function for all cells, total or not. If you want to have a different method of computing values in the total row/col you'll need to write your own custom aggregator. Here is the documentation for this https://github.com/nicolaskruchten/pivottable/wiki/Aggregators

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
  • I using custom aggregator only, please look at the edited question. From the custom aggregator function how can I find out Total col or row, so that I can apply different logic for total calculation – niran Jun 13 '18 at 06:15