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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

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
Super User
Super User

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.