【问题标题】:Force vega-lite to show label when number is 0Force vega-lite to show label when number is 0
【发布时间】:2022-12-02 06:23:41
【问题描述】:

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

【问题讨论】:

  • You should be using a dimension and a measure to show zero values.

标签: vega-lite deneb


【解决方案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.

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.

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

{
  "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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 2022-12-01
    • 1970-01-01
    • 2022-12-27
    • 2019-06-02
    • 2020-04-22
    • 1970-01-01
    • 2020-08-13
    相关资源
    最近更新 更多