Fix legend colours

53 Views Asked by At

For my vega bar chart i have data which is color coded. Example: 1 Datapoint1 USA red 2 Datapoint2 France yellow 3 Datapoint3 Germany red 4 Datapoint4 China white

For this reason i have the following vega code to assing the correct color in the bar char:

    {
  "name": "legend",
  "type": "ordinal",
  "domain": {"data": "dataset", "field": "colour"},
  "range": ["#FACC00", "#D92121", "#FFFFFF"]
}

However if i now filter the data for country since the color is assingned in the order from top then the assigned color hex code doesn't match anymore. Can somebody help how to fix the legend color to values in the data?

1

There are 1 best solutions below

0
davidebacci On BEST ANSWER

You can set a defined domain like this and the same colours will be used each time.

{
    "name": "legend",
    "type": "ordinal",
    "domain": ["France", "Germany", "China"],
    "range": ["#FACC00", "#D92121", "#FFFFFF"]
}