Sorting in vegalite screws when changing values from fraction to percentage

46 Views Asked by At

I have specified sorting in vegalite api as -y as shown below:

vl.markLine().data(video_data).encode(
                                vl.x()
                                    .fieldN('names') //nominal
                                    .sort('-y'),  // sort by -y
                                    //...
                                vl.y()
                                    .fieldQ('d') //quantitative
                                    //...

When I dont have percentage values, it correctly sorts:

enter image description here

But when I have percentage values, it screws up sorting:

enter image description here

Why is this so?

1

There are 1 best solutions below

0
MsA On

The problem seems to be that the d values seem to be in string format. Found answer as stated here. Converting it to vegalite api:

vl.markLine()
        .data(video_data)
        .transform([{"calculate": "toNumber(datum.d)", "as": "d2"}])  //convert d to numeric d2
        .encode(
                vl.x()
                  .fieldN('names') 
                  .sort('-y'),  
                  //...
                vl.y()
                  .fieldQ('d2')  //notice d2