1

I have a bar chart created with Deneb, which I originally sorted by a DAX measure ("Measure1").

After I added a new mark to show data labels, the chart is not sorted correctly anymore.

Why is this happening?

{
  
  "title": {"text": "Simple Bar chart - labels", "fontSize": 14, "anchor": "start", "align": "left"},
  
  "data": {"name": "dataset"},

  "encoding": {
  "y": {
    "field": "Site Name", 
    "type": "nominal", 
    "sort":  {"field": "Measure1", "order": "ascending"}
    },
      
  "x": {
    "field": "Measure1", 
    "type": "quantitative"}
    },

  "layer": [

  {
    "mark":
     {"type": "bar", 
     "color": "pink"}
  },
    
  {
    "mark": 
    {"type": "text", 
    "color": "black", 
    "align": "left", 
    "dx": 6
    },
    
    "encoding": {
      "text": {"field": "Measure1", 
      "type": "quantitative"} }
  }
      
  ]
  
}

I'd like the bar chart to be sorted by "Measure1" instead of "Site Name"

1 Answers1

1

I recommend taking a look at the vega-lite examples and documentation.

Here is a sorted bar chart you can review:

https://vega.github.io/vega-lite/examples/bar_aggregate_sort_by_encoding.html

Try one of these options to sort by the x axis:

"sort": "-x"

"sort": "x"

--UPDATE 1

Another nifty tick is to create a calculate transform which combines these 2 data fields and then you can split them apart later. Add this just under your data.

"transform": [
        {
          "calculate": "format(datum.['Measure1'],'.0f') + '|' + datum['Site Name']", "as": "Full_Site_Name"
        }
      ],

Then in your y axis you can do this to just display the Site name. [1] picks up the second part after the | character.

"y": {
"field": "Full_Site_Name", 
"type": "nominal",
    "axis": {
    "labelExpr": "split(datum.label, '|')[1]"
    }
},
APB Reports
  • 985
  • 10
  • I tried that, unfortunately it does not solve my issue – Davide Di Franco May 11 '23 at 20:23
  • Hi Davide, Please update your question with your entire vega-lite spec from deneb so we can run some tests. – APB Reports May 15 '23 at 07:32
  • 1
    Hey @DavideDiFranco, the sort issue in Deneb is due to an issue inside the vega-lite library https://github.com/vega/vega-lite/pull/8567 This issue has already been fixed on the vega-lite site, but the current version of Deneb is not using the new library at the moment. As far as I know the author of Deneb already initiated the process to get a new version of Deneb that has the current libraries into app source. I assume it will become 1.5.2 – Tom Martens May 18 '23 at 16:27
  • Hi Davide, I just updated the answer with another solution you can try. – APB Reports May 31 '23 at 10:46