Deneb sort order not working correctly

Hi community,

I have this legend that I want to operate based on user selection, I am struggling in getting my Deneb code to correctly color fill my visual based on Hex color code.

Below is an example of what I want:

And below is what I currently get in my code:

Attached is my PBIX file and sample data

data test deneb.csv (215 Bytes)
Deneb legend sample.pbix (1.5 MB)

Before I dive in, it looks like you want the same colour for new capacity and planned capacity? Can you send me the HEX codes for the 5 colours? Also, took a quick look and this is Vega, not Vega-Lite; I have minimal experience with Vega.
Greg

Hi @Greg ,

That is correct - they have the same HEX codes. They are already in the dataset attached and also in the PBIX model if you want to have a look.
And you are right, I could not create this using Vega-Lite, only using Vega.

MAX AVAILABLE CAPACITY = #001965
CAPACITY AMBITION = #FFC000
NEW CAPACITY = #D4D7DC
PLANNED CAPACITY = #D4D7DC
CONSTRAINED CAPACITY = #E6553F

Br,
Casper

:astonished:

Hi @CasperSeve.

Given that I’ve little experience with Vega, I used Vega-Lite first to see how I’d do it: here’s what I came up with:

Here’s the Vega-Lite code:

{
  "data": {"name": "dataset"},
  "mark": {"type": "arc", "size": 0},
  "encoding": {
    "theta": {
      "field": "Capacity_type",
      "type": "nominal"
    },
    "color": {
      "field": "Capacity_type_Hex_color_code",
      "type": "nominal",
      "scale": {
        "domain": [
          "MAX AVAILABLE CAPACITY",
          "CAPACITY AMBITION",
          "NEW CAPACITY",
          "PLANNED CAPACITY",
          "CONSTRAINED CAPACITY"
        ],
        "range": [
          "#001965",
          "#FFC000",
          "#D4D7DC",
          "#D4D7DC",
          "#E6553F"
        ]
      },
      "legend": {
        "title": null,
        "orient": "none",
        "legendX": 0,
        "legendY": 0,
        "columns": 2,
        "symbolType": "square",
        "symbolSize": 200,
        "symbolStrokeColor": "black",
        "symbolStrokeWidth": 0.5
      }
    }
  }
}

I’ll give Vega a try next and see if I can come up with anything, but my Vega knowledge is minimal, so I’m not optimistic. I’d post to stack overflow as well, as one of the world’s best in Vega, Davide Bacci, regularly investigates and provides solutions there and may be able to help as well.

Greg

1 Like

Thanks a million @Greg !

The code you provided is with hardcoding, in my model I have this column called Hex_color_code, where pending on the capacity_type value a different color code might appear from time to time. Can I make this more dynamic?

Hi @CasperSeve.

OK, before I even got started with Vega, I found this: if you ensure that all the colours are unique (i.e., new and planned capacity are not the same hex code), then Vega works. Here’s what I got by simply changing the hex code for “planned capacity” to red:

The constrained capacity now shows the desired colour. I think that D3/Vega/Vege-Lite will aggregate identical values (in this case, colours) by default, and you may need to create a custom visual to present as you like. For me, the easy way forward with Vega is to ensure unique values in your dataset.

Hope that helps.
Greg
eDNA Forum - Deneb Legend (Vega).pbix (1.5 MB)

Hi @CasperSteve

To answer your question, yes, you can use dataset colours instead of hard-coded colours. Here’s a Vega-Lite solution:

The scale-null key:value pair in the color block of the encoding block is what makes this work.

Here’s the code:

{
  "data": {"name": "dataset"},
  "columns": 2,
  "hconcat": [
    {
      "name": "COLUMN_1",
      "transform": [
        {
          "filter": "datum['Capacity_type_Rank'] < 10"
        }
      ],
      "mark": {
        "type": "bar",
        "height": {"band": 0.9},
        "x": 20,
        "stroke": "black",
        "strokeWidth": 0.5
      },
      "encoding": {
        "y": {
          "field": "Capacity_type",
          "type": "nominal",
          "sort": {
            "op": "sum",
            "field": "Capacity_type_Rank",
            "order": "ascending"
          },
          "axis": {
            "title": null,
            "orient": "right",
            "labelAlign": "left",
            "domain": false,
            "ticks": false
          }
        },
        "color": {
          "field": "Capacity_type_Hex_color_code",
          "type": "nominal",
          "scale": null
        }
      }
    },
    {
      "name": "COLUMN_2",
      "transform": [
        {
          "filter": "datum['Capacity_type_Rank'] >= 10"
        }
      ],
      "mark": {
        "type": "bar",
        "height": {"band": 0.9},
        "x": 20,
        "stroke": "black",
        "strokeWidth": 0.5
      },
      "encoding": {
        "y": {
          "field": "Capacity_type",
          "type": "nominal",
          "sort": {
            "op": "sum",
            "field": "Capacity_type_Rank",
            "order": "ascending"
          },
          "axis": {
            "title": null,
            "orient": "right",
            "labelAlign": "left",
            "domain": false,
            "ticks": false
          }
        },
        "color": {
          "field": "Capacity_type_Hex_color_code",
          "type": "nominal",
          "scale": null
        }
      }
    }
  ]
}

Hope it helps.
Greg
eDNA Forum - Deneb Legend (Vega-Lite).pbix (1.5 MB)