powerbipowerbi-desktopvega-litedeneb

Deneb (Vega-lite) - Access Other Columns in Axis Label Expression


I am attempting to use expr to format/style my Yaxis labelFont, but it does not seem to take the expression. I have a helper column called YLabelFontFamily that already has values for reach row.

According to the documentation, labelFont accepts ExprRef, when I specify "datum.YLabelFontFamily", the font is not applied. What am I doing wrong?

See screenshot below.

The pbix file can be found at https://github.com/triCNguyen5/Test/blob/main/YaxisLabelTest.pbix

enter image description here


Solution

  • The axis doesn't have access to the datapoint. It can only see the value which is accessed via datum.label or datum.value (both return the same thing). e.g.

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
      "description": "A simple bar chart with embedded data.",
      "data": {
        "values": [
          {"a": "A", "b": 28},
          {"a": "B", "b": 55},
          {"a": "C", "b": 43}
        ]
      },
      "mark": "bar",
      "encoding": {
        "y": {
          "field": "a",
          "type": "nominal",
          "axis": {"labelFont": {"expr": "datum.label=='A'?'Arial Black':'Arial'"}}
        },
        "x": {"field": "b", "type": "quantitative"}
      }
    }
    
    
    

    You can achieve your end goal as follows:

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
      "description": "A simple bar chart with embedded data.",
      "data": {
        "values": [
          {"a": "A", "b": 28, "c":"Arial"},
          {"a": "B", "b": 55, "c":"Arial Black"},
          {"a": "C", "b": 43, "c":"Arial"}
        ]
      }, "transform": [{"calculate": "datum.a+'|'+datum.c" ,"as": "d"}],
      "mark": "bar",
      "encoding": {
        "y": {
          "field": "d",
          "type": "nominal",
          "axis": {"labelFont": {"expr": "split(datum.label,'|')[1]"}, "labelExpr": "split(datum.label,'|')[0]", "title":"a"}
        },
        "x": {"field": "b", "type": "quantitative"}
      }
    }