简体   繁体   中英

How to use pivotConfig in Cube.js?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM