0

I'm still very much a beginner in vega-lite but I'm trying to create a stacked bar chart with different sales channels. Sometimes a sales channel has a 0 and doesn't show up, how can I still show the label?

{
  "layer": [
    {
      "mark": {
        "type": "bar",
        "cornerRadius": 50,
        "color": "#90C290",
        "tooltip": true
      },
      "encoding": {
        "x": {
          "field": "Number of customers"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "tooltip": true,
        "align": "left",
        "baseline": "middle",
        "x": 10,
        "color": "white"
      },
      "encoding": {
        "text": {
          "field": "Number of customers",
          "type": "text"
        }
      }
    }
  ],
  "encoding": {
    "y": {
      "field": "Sales channel",
      "type": "nominal",
      "sort": "descending",
      "title": null
    },
    "x": {
      "type": "quantitative",
      "title": null,
      "axis": null
    }
  }
}

I tried the code above and looked through documentation but couldn't exactly find what I was looking for

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34

1 Answers1

1

I added sample data to your spec, and there are a few changes I would make.

Around line 29 you have "type": "text" which should be "type": "quantitative".

I think your problem is that the text color is white and the background color is white, so the text is there but you can't see it. A simple fix would be to set the text color to black, or change the background color to something other than white (add "background": "lightgray", before "layer").

It's also possible you don't see the channel at all depending on how you are passing data from Power BI. Check the data tab in the Deneb window to make sure the Channel is there.

Deneb window showing channel "Zero" with 0 customers

If the channel is not there, you'll have to adjust something on the Power BI side. A good practice is to put the data in a table in Power BI first so you know what you are sending into Deneb. If you use an aggregation like SUM on the data field in Power BI, nulls will drop out, but zeros should stay. If you use "Don't summarize" then nulls or errors (text in a number field) will pass in as nulls to Deneb, but you may need to add an "aggregate": "sum" to your encoding.

Power BI tables showing which values will get passed to Deneb

In any case, here's the spec the way I would write it.

Deneb bar chart

{
  "data": {"name": "dataset"},
  "layer": [
    {
      "mark": {
        "type": "bar",
        "cornerRadius": 50,
        "color": "#90C290",
        "tooltip": true
      }
    },
    {
      "mark": {
        "type": "text",
        "tooltip": true,
        "align": "left",
        "baseline": "middle",
        "dx": 5,
        "color": "black"
      },
      "encoding": {
        "text": {
          "field": "Number of customers",
          "type": "quantitative"
        }
      }
    }
  ],
  "encoding": {
    "y": {
      "field": "Sales channel",
      "type": "nominal",
      "sort": "descending",
      "title": null
    },
    "x": {
      "field": "Number of customers",
      "type": "quantitative",
      "title": null,
      "axis": null
    }
  }
}

Link to sample data in Vega Editor

TheRizza
  • 1,577
  • 1
  • 10
  • 23