How to use pivotConfig in Cube.js?

1.2k Views Asked by At

My resultSet looks like this:

0: Object { "Error.type": "A", "Error.criticity": "ORANGE", "Error.count": 10 }
​​
1: Object { "Error.type": "B", "Error.criticity": "ORANGE", "Error.count": 8 }
​​
2: Object { "Error.type": "B", "Error.criticity": "GREEN", "Error.count": 6 }
​​
3: Object { "Error.type": "C", "Error.criticity": "ORANGE", "Error.count": 5 }
​​
4: Object { "Error.type": "C", "Error.criticity": "GREEN", "Error.count": 1 }
​​
5: Object { "Error.type": "A", "Error.criticity": "GREEN", "Error.count": 1 }
​​

I would like to pivot it to get this:

0: Object { "Error.type": "A", "ORANGE": 10, "GREEN": 1}
​
1: Object { "Error.type": "B", "ORANGE": 8, "GREEN": 6 }
​
2: Object { "Error.type": "C", "ORANGE": 5, "GREEN": 1}

Can I use tablePivot to do this? If yes, how to set pivotConfig?

The final goal is to render this formatted resultSet as a stacked barchart with Recharts.


I have tried:

resultSet.tablePivot({
                x: ['Error.type'],
                y: ['Error.criticity', 'Error.count'],
              })

which returns

0: Object { "Error.type": "A", "Error.criticity": "ORANGE", "Error.count": undefined }
​
1: Object { "Error.type": "B", "Error.criticity": "ORANGE", "Error.count": undefined }
​
2: Object { "Error.type": "C", "Error.criticity": "ORANGE", "Error.count": 5 }

and

resultSet.tablePivot({
                x: ['Error.type'],
                y: ['Error.criticity', 'measures'],
              })

which returns

0: Object { "Error.type": "A", "Error.criticity": "GREEN", "Error.count": 1 }
​
1: Object { "Error.type": "B", "Error.criticity": "GREEN", "Error.count": 6 }
​
2: Object { "Error.type": "C", "Error.criticity": "GREEN", "Error.count": 1 }

In both cases I lose some information.

1

There are 1 best solutions below

0
On

I believe that you need a pivotConfig like this:

resultSet.tablePivot({
  x: [ 'Error.type' ],
  y: [ 'Error.criticity', 'measures' ]
})

The pivoted result will look like this, which is pretty much what you expect it to be, right?

[
  {Error.type: "A", ORANGE,Error.count: "10", GREEN,Error.count: "1"},
  {Error.type: "B", ORANGE,Error.count: "8", GREEN,Error.count: "6"},
  {Error.type: "C", ORANGE,Error.count: "5", GREEN,Error.count: "1"}
]

You can read more about pivotConfig in Cube.js documentation.