Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Luz
Helper I
Helper I

Conditional with Deneb

Hi, 

 

I need to put in different colors the bars in a graph made with Deneb, I'm trying to use conditional but I don't know how because I have an error:

 

 

 

  "encoding": {
    "y": {
      "field": "detMaxConfidence",
      "type": "nominal",
      "axis": {"title": "Confianza"}},
      "color":{
        "condition": {
          "test":"datum['detMaxConfidence'] === 50",
        "value":"#3049AD" || "datum['detMaxConfidence'] === 60",
        "value":"#FF994E"   }, 
        "value": "red"
      },

 

 

 I need five diferent colors or five conditional, how can I do it?

 

@giammariam 

1 ACCEPTED SOLUTION
giammariam
Solution Sage
Solution Sage

Hey @Luz, without seeing your data or your entire vega-lite spec, I had to make a few assumptions using the screenshot that you sent me in the private message. There are actually multiple ways to do this. If the color condition values are static (which it looks like they are from your code snippet above), then you could actually forgo the conditional and just populate the scale domain values (input) and scale range values (output) like the following:

 

 

            "color": {
              "field": "detMaxConfidence",
              "type": "nominal",
              "scale": {
                "domain": [50, 60, 70, 80, 100],
                "range": ["#3049AE", "#FE994E", "#C83D95", "#FFBCEE", "#3CF8F7"]
              }
            }

 

 If this assumption is wrong, please clarify the logic needed to determine the colors.

 

Here is a screenshot of the output from my vega-lite spec:

giammariam_0-1678927656896.png


If this is enough to get you going please consider liking this reply and choosing it as the solution. Otherwise, I'm happy to help further.

 

Here is the gist where you can view the spec in the Vega editor.


Here is the spec (note that this contains dummy data so you'll want to remove that portion from the data property before bringing it into Deneb):

 

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "padding": 15,
  "title": "Detecciones per Confianza",
  "background": "#F0F0F0",
  "width": 800,
  "height": 400,
  "data": {
    "name": "dataset",
    "values": [
      {"detecciones": 10, "detMaxConfidence": 50},
      {"detecciones": 15, "detMaxConfidence": 60},
      {"detecciones": 10, "detMaxConfidence": 70},
      {"detecciones": 12, "detMaxConfidence": 80},
      {"detecciones": 45, "detMaxConfidence": 100}
    ]
  },
  "layer": [
    {
      "encoding": {
        "x": {
          "field": "detecciones",
          "type": "quantitative",
          "axis": {"title": "Detecciones"}
        },
        "y": {
          "field": "detMaxConfidence",
          "type": "nominal",
          "axis": {"title": "Confianza"}
        }
      },
      "layer": [
        {
          "mark": {"type": "bar"},
          "encoding": {
            "color": {
              "field": "detMaxConfidence",
              "type": "nominal",
              "scale": {
                "domain": [50, 60, 70, 80, 100],
                "range": ["#3049AE", "#FE994E", "#C83D95", "#FFBCEE", "#3CF8F7"]
              },
              "legend": {
                "title": "Confianza"
              }
            }
          }
        },
        {
          "mark": {"type": "text"},
          "encoding": {
            "text": {"field": "detecciones", "type": "quantitative"},
            "xOffset": {"value": 10}
          }
        }
      ]
    }
  ]
}

 

 



Madison Giammaria
Proud to be a Super User 😄
LinkedIn

Do you frequently use Deneb to provide insights to your stakeholders? Have you considered sponsoring this free and open source custom visual? More info here!

View solution in original post

2 REPLIES 2
giammariam
Solution Sage
Solution Sage

Hey @Luz, without seeing your data or your entire vega-lite spec, I had to make a few assumptions using the screenshot that you sent me in the private message. There are actually multiple ways to do this. If the color condition values are static (which it looks like they are from your code snippet above), then you could actually forgo the conditional and just populate the scale domain values (input) and scale range values (output) like the following:

 

 

            "color": {
              "field": "detMaxConfidence",
              "type": "nominal",
              "scale": {
                "domain": [50, 60, 70, 80, 100],
                "range": ["#3049AE", "#FE994E", "#C83D95", "#FFBCEE", "#3CF8F7"]
              }
            }

 

 If this assumption is wrong, please clarify the logic needed to determine the colors.

 

Here is a screenshot of the output from my vega-lite spec:

giammariam_0-1678927656896.png


If this is enough to get you going please consider liking this reply and choosing it as the solution. Otherwise, I'm happy to help further.

 

Here is the gist where you can view the spec in the Vega editor.


Here is the spec (note that this contains dummy data so you'll want to remove that portion from the data property before bringing it into Deneb):

 

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "padding": 15,
  "title": "Detecciones per Confianza",
  "background": "#F0F0F0",
  "width": 800,
  "height": 400,
  "data": {
    "name": "dataset",
    "values": [
      {"detecciones": 10, "detMaxConfidence": 50},
      {"detecciones": 15, "detMaxConfidence": 60},
      {"detecciones": 10, "detMaxConfidence": 70},
      {"detecciones": 12, "detMaxConfidence": 80},
      {"detecciones": 45, "detMaxConfidence": 100}
    ]
  },
  "layer": [
    {
      "encoding": {
        "x": {
          "field": "detecciones",
          "type": "quantitative",
          "axis": {"title": "Detecciones"}
        },
        "y": {
          "field": "detMaxConfidence",
          "type": "nominal",
          "axis": {"title": "Confianza"}
        }
      },
      "layer": [
        {
          "mark": {"type": "bar"},
          "encoding": {
            "color": {
              "field": "detMaxConfidence",
              "type": "nominal",
              "scale": {
                "domain": [50, 60, 70, 80, 100],
                "range": ["#3049AE", "#FE994E", "#C83D95", "#FFBCEE", "#3CF8F7"]
              },
              "legend": {
                "title": "Confianza"
              }
            }
          }
        },
        {
          "mark": {"type": "text"},
          "encoding": {
            "text": {"field": "detecciones", "type": "quantitative"},
            "xOffset": {"value": 10}
          }
        }
      ]
    }
  ]
}

 

 



Madison Giammaria
Proud to be a Super User 😄
LinkedIn

Do you frequently use Deneb to provide insights to your stakeholders? Have you considered sponsoring this free and open source custom visual? More info here!
ppm1
Solution Sage
Solution Sage

One way to do it is in your "mark" with a nested condition expression as shown below.

ppm1_0-1678925916237.png

 

Pat

Microsoft Employee

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.