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.34k stars 1.08k forks source link

Use pivot on 2 columns only in a data set with for example 4 columns #917

Open marc-vermeulen opened 6 years ago

marc-vermeulen commented 6 years ago

I want to use .pivot() to pivot only 2 columns that I have in a data table. Below is the input format and the expected output format. So far, I was not able to create the expected output. Can you help ?

INPUT - Columns ["Name", "Product", "Measure Type", "Measure Value"]

INPUT - Data ["Andrew", "Tables", "Discount", 20], ["Andrew", "Tables", "Profit", 10], ["Andrew", "Tables", "Sales", 2120], ["Marc", "Tables", "Discount", 40], ["Marc", "Tables", "Profit", 6], ["Marc", "Tables", "Sales", 3200], ["Marc", "Desks", "Discount", 20], ["Marc", "Desks", "Profit", 8], ["Marc", "Desks", "Sales", 2200]

Expected Output - Columns ["Name", "Product", "Discount", "Profit", "Sales"]

Expected Output Data ["Andrew", "Tables", 20, 10, 2120], ["Marc", "Tables", 40, 6, 3200], ["Marc", "Desks", 20, 8, 2200]

nicolaskruchten commented 6 years ago

What parameters have you tried? It seems to me that rows=[name, product], columns=[measure type], vals=[measure value], aggregator=sum would work?

marc-vermeulen commented 6 years ago

Thx Nicolas for your quick response. I give it another try this evening; will do a test in jsfiddle so that I can share if I still have a problem.

marc-vermeulen commented 6 years ago

Nicolas, I can now pivot the data, but I expect as output again an array of arrays; just like my input. And thus not a rendered table or heatmap, etc. How can prevent pivot() from rendering towards these forwards and just give me a outputData back ?

This is my code:

var inputData = [ ["Name", "Product", "Measure Type", "Measure Value"], ["Andrew", "Tables", "Discount", 20], ["Andrew", "Tables", "Profit", 10], ["Andrew", "Tables", "Sales", 2120], ["Marc", "Tables", "Discount", 40], ["Marc", "Tables", "Profit", 6], ["Marc", "Tables", "Sales", 3200], ["Marc", "Desks", "Discount", 20], ["Marc", "Desks", "Profit", 8], ["Marc", "Desks", "Sales", 2200] ];

var utils = $.pivotUtilities; var heatmap = utils.renderers["Heatmap"]; var sum = utils.aggregators["Sum"];

$("#output").pivot( inputData, { rows: ["Name", "Product"], cols: ["Measure Type"], vals: ["Measure Value"], aggregator: sum(["Measure Value"]), renderer: heatmap });

nicolaskruchten commented 6 years ago

Ah, this library is designed as a rendering library, not an array-to-array transformation system.