Search code examples
powerbivega-litedeneb

How do you change the font formatting of a single axis label value


I have a text field ['Region'] with four values {Global, AMER, APAC, EMEA}. I have a simple bar chart and want the label to be bold only for 'Global' but have been unable to do so.

I tried:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {    "values": [
      {"Region": "Global", "Months": 1, "RegionRank":1},
      {"Region": "AMER", "Months": -1, "RegionRank":2},
      {"Region": "APAC", "Months": 3, "RegionRank":3},    {"Region": "EMEA", "Months": 2, "RegionRank":4}

    ]},
  "layer": [
    {
      "mark": {
        "type": "bar",
        "filled": false,
        "stroke": "gray",
        "strokeDash": [3.6],
        "strokeWidth": 3
      }
    },
    {
      "mark": {
        "type": "text",
          "fontSize": 14,
        "xOffset": {
          "expr": "datum['Months']<0 ? -15:15"
        }
      },
      "encoding": {
        "text": {
          "field": "Months",
          "format": "+.0f"
        }
      }
    }
  ],
  "encoding": {
    "y": {
      "field": "Region",
      "type": "nominal",
      "axis": {
        "offset": 10,
        "title": null,
// problem section
       "labelFontWeight": {
         "condition": {"test": "datum['Region'] == 'Global'","value":"bold"},
            "value":"normal"
       }
      },
      "sort": {
        "op": "min",
        "field": "RegionRank",
        "order": "ascending"
      }
    },
    "x": {
      "field": "Months",
      "type": "quantitative",
      "axis": {"title": null}
    }
  }
}

but there is no change to the label font weight.

What is interesting is that I switched the result conditions, making the '==Global' result "normal" and the else "bold" and it changed font weight on all the labels to bold. That leads me to believe that my condition is never being evaluated as true. Any ideas why? Is what I want to do - changing a signal value in an axis label - possible?


Solution

  • You mean like this?

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {
        "values": [
          {"Region": "Global", "Months": 1, "RegionRank": 1},
          {"Region": "AMER", "Months": -1, "RegionRank": 2},
          {"Region": "APAC", "Months": 3, "RegionRank": 3},
          {"Region": "EMEA", "Months": 2, "RegionRank": 4}
        ]
      },
      "layer": [
        {
          "mark": {
            "type": "bar",
            "filled": false,
            "stroke": "gray",
            "strokeDash": [3.6],
            "strokeWidth": 3
          }
        },
        {
          "mark": {
            "type": "text",
            "fontSize": 14,
            "xOffset": {"expr": "datum['Months']<0 ? -15:15"}
          },
          "encoding": {"text": {"field": "Months", "format": "+.0f"}}
        }
      ],
      "encoding": {
        "y": {
          "field": "Region",
          "type": "nominal",
          "axis": {
            "offset": 10,
            "title": null,
            "labelFontWeight": {
              "condition": {"test": "datum.label == 'Global'", "value": "bold"},
              "value": "normal"
            }
          },
          "sort": {"op": "min", "field": "RegionRank", "order": "ascending"}
        },
        "x": {"field": "Months", "type": "quantitative", "axis": {"title": null}}
      }
    }