Search code examples
powerbivisualizationpowerbi-desktopvega-litedeneb

Pie chart Vega lite : conditional label colors


I would like to have conditional label colors in the "fill" section for my text layer.

Link to my pbix

I have tried this but it won't work...

"fill": {
     "condition":{
     "test": "datum.Name = 'Other players'" ,
            "value": "black"},
     "value": "white"
  }

Thank you for your help.


Solution

  • Here you go.

    enter image description here

    {
      "width": "container",
      "height": "container",
      "data": {"name": "dataset"},
      "encoding": {
        "theta": {
          "field": "Points_calculation",
          "type": "quantitative",
          "stack": true
        },
        "order": {
          "field": "ORDER",
          "type": "quantitative",
          "sort": "descending"
        },
        "color": {
          "field": "Name",
          "type": "nominal",
          "sort": {
            "field": "ORDER",
            "order": "descending"
          },
          "legend": null,
          "scale": {
            "scheme": "pbiColorNominal"
          }
        }
      },
      "layer": [
        {
          "name": "******RING BACKGROUND GEN COL******",
          "mark": {
            "type": "arc",
            "outerRadius": 100
          }
        },
        {
          "name": "******RING BACKGROUND OTHER COL******",
          "mark": {
            "type": "arc",
            "outerRadius": 100,
            "fill": {
              "expr": "datum.Name =='Other players'?'grey':''"
            }
          }
        },
        {
          "name": "******Name labels******",
          "mark": {
            "type": "text",
            "radius": 115,
            "dy": 4,
            "fill": "black",
            "align": {
              "expr": "scale('theta', datum.Points_calculation_end - datum.Points_calculation_start + datum.Points_calculation_start ) > PI  ?'right':'left'"
            }
          },
          "encoding": {
            "text": {
              "field": "Name",
              "type": "nominal"
            }
          }
        },
        {
          "name": "******Values_Labels******",
          "mark": {
            "type": "text",
            "radius": 80,
            "fill":{"expr":"datum.Name =='Other players'?'black':'white'"}
          },
          "encoding": {
            "text": {
              "field": "Points_calculation",
              "type": "nominal"
            }
          }
        }
      ]
    }