pat310 / quick-pivot

Quickly format data to create a pivot table
https://www.npmjs.com/package/quick-pivot
MIT License
64 stars 21 forks source link

Grouping order changes upon filtering of sub-grouping #55

Closed turnerniles closed 7 years ago

turnerniles commented 7 years ago

When you filter on certain sub groupings the main grouping order changes and get moved to the bottom.

const dataArray = [['name', 'gender', 'house', 'age'],['Jon', 'm', 'Stark', 14],['Arya', 'f', 'Stark', 10],['Cersei', 'f', 'Baratheon', 38],['Tywin', 'm', 'Lannister', 67],['Tyrion', 'm', 'Lannister', 34],['Joffrey', 'm', 'Baratheon', 18],['Bran', 'm', 'Stark', 8],['Jaime', 'm', 'Lannister', 32],['Sansa', 'f', 'Stark', 12] ];

const testPivot = new pivot(dataArray, ['gender', 'house'], [], 'age', 'sum');

Result of JSON.stringify(testPivot.data.table): "[{"value":["sum age","sum age"],"depth":0,"type":"colHeader","row":0},{"value":["m",173],"depth":0,"type":"rowHeader","row":1},{"value":["Stark",22],"type":"data","depth":1,"row":2},{"value":["Lannister",133],"type":"data","depth":1,"row":3},{"value":["Baratheon",18],"type":"data","depth":1,"row":4},{"value":["f",60],"depth":0,"type":"rowHeader","row":5},{"value":["Stark",22],"type":"data","depth":1,"row":6},{"value":["Baratheon",38],"type":"data","depth":1,"row":7}]"

testPivot.filter('house', ['Stark'])

Result of JSON.stringify(testPivot.data.table): "[{"value":["sum age","sum age"],"depth":0,"type":"colHeader","row":0},{"value":["f",38],"depth":0,"type":"rowHeader","row":1},{"value":["Baratheon",38],"type":"data","depth":1,"row":2},{"value":["m",151],"depth":0,"type":"rowHeader","row":3},{"value":["Lannister",133],"type":"data","depth":1,"row":4},{"value":["Baratheon",18],"type":"data","depth":1,"row":5}]"

Notice how the 'm' grouping is shifted to the second position. However, filtering on Baratheon does not cause the shift in order. It's unclear to me what exact scenarios cause the shift.

const dataArray = [['name', 'gender', 'house', 'age'],['Jon', 'm', 'Stark', 14],['Arya', 'f', 'Stark', 10],['Cersei', 'f', 'Baratheon', 38],['Tywin', 'm', 'Lannister', 67],['Tyrion', 'm', 'Lannister', 34],['Joffrey', 'm', 'Baratheon', 18],['Bran', 'm', 'Stark', 8],['Jaime', 'm', 'Lannister', 32],['Sansa', 'f', 'Stark', 12] ];

const testPivot = new pivot(dataArray, ['gender', 'house'], [], 'age', 'sum');

Result of JSON.stringify(testPivot.data.table): "[{"value":["sum age","sum age"],"depth":0,"type":"colHeader","row":0},{"value":["m",173],"depth":0,"type":"rowHeader","row":1},{"value":["Stark",22],"type":"data","depth":1,"row":2},{"value":["Lannister",133],"type":"data","depth":1,"row":3},{"value":["Baratheon",18],"type":"data","depth":1,"row":4},{"value":["f",60],"depth":0,"type":"rowHeader","row":5},{"value":["Stark",22],"type":"data","depth":1,"row":6},{"value":["Baratheon",38],"type":"data","depth":1,"row":7}]"

testPivot.filter('house', ['Baratheon'])

Result of JSON.stringify(testPivot.data.table): "[{"value":["sum age","sum age"],"depth":0,"type":"colHeader","row":0},{"value":["m",155],"depth":0,"type":"rowHeader","row":1},{"value":["Stark",22],"type":"data","depth":1,"row":2},{"value":["Lannister",133],"type":"data","depth":1,"row":3},{"value":["f",22],"depth":0,"type":"rowHeader","row":4},{"value":["Stark",22],"type":"data","depth":1,"row":5}]"

turnerniles commented 7 years ago

Data should be returned sorted alphabetically A-Z. Currently, up to two levels of pivoting maintain sort but a third causes the issue above. Instead can we sort all levels A-Z after pivoting?

const dataArray = [ ['name', 'gender', 'house', 'age'], ['Jon', 'm', 'Stark', 14], ['Arya', 'f', 'Stark', 10], ['Cersei', 'f', 'Baratheon', 38], ['Tywin', 'm', 'Lannister', 67], ['Tyrion', 'm', 'Lannister', 34], ['Joffrey', 'm', 'Baratheon', 18], ['Bran', 'm', 'Stark', 8], ['Jaime', 'm', 'Lannister', 32], ['Sansa', 'f', 'Stark', 12] ];

const testPivot = new pivot(dataArray, ['house', 'gender', 'name'], [], 'age', 'sum');

JSON.stringify(testPivot.data.table)

"[{"value":["sum age","sum age"],"depth":0,"type":"colHeader","row":0},{"value":["Stark",44],"depth":0,"type":"rowHeader","row":1},{"value":["f",22],"depth":1,"type":"rowHeader","row":2},{"value":["Arya",10],"type":"data","depth":2,"row":3},{"value":["Sansa",12],"type":"data","depth":2,"row":4},{"value":["m",22],"depth":1,"type":"rowHeader","row":5},{"value":["Bran",8],"type":"data","depth":2,"row":6},{"value":["Jon",14],"type":"data","depth":2,"row":7},{"value":["Baratheon",56],"depth":0,"type":"rowHeader","row":8},{"value":["f",38],"depth":1,"type":"rowHeader","row":9},{"value":["Cersei",38],"type":"data","depth":2,"row":10},{"value":["m",18],"depth":1,"type":"rowHeader","row":11},{"value":["Joffrey",18],"type":"data","depth":2,"row":12},{"value":["Lannister",133],"depth":0,"type":"rowHeader","row":13},{"value":["m",133],"depth":1,"type":"rowHeader","row":14},{"value":["Jaime",32],"type":"data","depth":2,"row":15},{"value":["Tyrion",34],"type":"data","depth":2,"row":16},{"value":["Tywin",67],"type":"data","depth":2,"row":17}]"

testPivot.filter("name", ["Arya"], 'exclude') JSON.stringify(testPivot.data.table) "[{"value":["sum age","sum age"],"depth":0,"type":"colHeader","row":0},{"value":["Stark",34],"depth":0,"type":"rowHeader","row":1},{"value":["m",22],"depth":1,"type":"rowHeader","row":2},{"value":["Bran",8],"type":"data","depth":2,"row":3},{"value":["Jon",14],"type":"data","depth":2,"row":4},{"value":["f",12],"depth":1,"type":"rowHeader","row":5},{"value":["Sansa",12],"type":"data","depth":2,"row":6},{"value":["Baratheon",56],"depth":0,"type":"rowHeader","row":7},{"value":["f",38],"depth":1,"type":"rowHeader","row":8},{"value":["Cersei",38],"type":"data","depth":2,"row":9},{"value":["m",18],"depth":1,"type":"rowHeader","row":10},{"value":["Joffrey",18],"type":"data","depth":2,"row":11},{"value":["Lannister",133],"depth":0,"type":"rowHeader","row":12},{"value":["m",133],"depth":1,"type":"rowHeader","row":13},{"value":["Jaime",32],"type":"data","depth":2,"row":14},{"value":["Tyrion",34],"type":"data","depth":2,"row":15},{"value":["Tywin",67],"type":"data","depth":2,"row":16}]"

See how the gender under Stark is reordered from 'f' to 'm' after Arya is removed?