Raghav Mittal
Raghav Mittal

Reputation: 69

How to place text at the center for a funnel chart?

I am working on creating a funnel chart using vega lite but facing an issue while placing text at the center of each bar.

Below is the vega lite spec.


{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "encoding": {
    "y": {"field": "department", "type": "nominal", "sort": "-x"},
    "x": {
      "field": "daily_total_login",
      "type": "quantitative",
      "stack": "center"
    }
  },
  "layer": [
    {
      "mark": {"tooltip": true, "type": "bar"},
      "encoding": {
        "x": {
          "field": "daily_total_login",
          "type": "quantitative",
          "stack": "center"
        },
        "color": {"field": "department", "type": "nominal", "legend": null}
      }
    },
    {
      "mark": {"type": "text"},
      "encoding": {
        "text": {"field": "daily_total_login", "type": "quantitative"}
      },
    }
  ],
  "data": {
    "values": [
      {
        "date": "2024-08-09",
        "department": "A",
        "daily_total_login": 277,
        "daily_active_user": 87
      },
      {
        "date": "2024-08-10",
        "department": "B",
        "daily_total_login": 36,
        "daily_active_user": 9
      },
      {
        "date": "2024-08-11",
        "department": "C",
        "daily_total_login": 39,
        "daily_active_user": 14
      },
      {
        "date": "2024-08-12",
        "department": "D",
        "daily_total_login": 124,
        "daily_active_user": 51
      },

    ]
  },
  "height": "container",
  "width": "container"
}

With the output as below: enter image description here

Could anyone please suggest what changes I could make in the above vega lite spec, so that the text could appear at the center of each bar?

Upvotes: 1

Views: 31

Answers (1)

davidebacci
davidebacci

Reputation: 30304

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "encoding": {
    "y": {"field": "department", "type": "nominal", "sort": "-x"},
    "x": {
      "field": "daily_total_login",
      "type": "quantitative",
      "stack": "center"
    }
  },
  "layer": [
    {
      "mark": {"tooltip": true, "type": "bar"},
      "encoding": {
        "color": {"field": "department", "type": "nominal", "legend": null}
      }
    },
    {
      "mark": {"type": "text"},
      "encoding": {
        "text": {"field": "daily_total_login", "type": "quantitative"},
        "x": {
          "datum":{"expr":"invert('x',width/2)"}
          
        }
      }
    }
  ],
  "data": {
    "values": [
      {
        "date": "2024-08-09",
        "department": "A",
        "daily_total_login": 277,
        "daily_active_user": 87
      },
      {
        "date": "2024-08-10",
        "department": "B",
        "daily_total_login": 36,
        "daily_active_user": 9
      },
      {
        "date": "2024-08-11",
        "department": "C",
        "daily_total_login": 39,
        "daily_active_user": 14
      },
      {
        "date": "2024-08-12",
        "department": "D",
        "daily_total_login": 124,
        "daily_active_user": 51
      }
    ]
  },
  "height": "container",
  "width": "container",
}

Upvotes: 2

Related Questions