Esta página también está en español. Leer en español →

Horizontal Stacked Bar Chart

Horizontal stacked bars for Power BI with Deneb, based on an official Vega-Lite example. Cross-filter is limited when aggregate is used.

Vega-Lite stacked-bar ✓ Tooltip✓ Context menu

Live preview

Rendered with sample data · Vega/Vega-Lite
Horizontal Stacked Bar Chart Loading preview…

Preview rendered with the runtime of Deneb · MIT © Daniel Marsh-Patrick

The sample data and the chart's own titles stay as they ship in the spec.

🔽 Download (.pbix, Spanish labels) 🔽 Download spec .json

🧩 Fields it expects

  1. __0__ Category text
  2. __1__ Detail Category text
  3. __2__ Measure number measure

Drag your fields into Values in this order.

🎯 Technique

Engine
Vega-Lite 5.16.3
Marks
bar, text
Power BI
  • pbiFormat: applies Power BI's number or date format to a value

How to use it in Power BI

  1. Add the Deneb visual to your report and open its editor.
  2. Drag your fields into Values in this order: Category , Detail Category , Measure .
  3. Paste the spec (the «Copy spec» button above) into Deneb's JSON editor.
  4. Map your columns and adjust it to taste. The native interactivity is already set up.
See the full spec: Vega-Lite
spec.vega-lite.json
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Gráfico de barras apiladas horizontales. Basado en un ejemplo oficial de Vega-Lite y adaptado para Deneb en Power BI; ten en cuenta que la interactividad de cross-filter es limitada cuando se usa ‘aggregate’. Por CSalcedoDataBI · https://csalcedodatabi.com/deneb/horizontal-stacked-bar-chart",
  "usermeta": {
    "deneb": {
      "build": "1.6.2.1",
      "metaVersion": 1,
      "provider": "vegaLite",
      "providerVersion": "5.16.3"
    },
    "interactivity": {
      "tooltip": true,
      "contextMenu": true,
      "selection": false,
      "highlight": false,
      "dataPointLimit": 50
    },
    "information": {
      "name": "Horizontal Stacked Bar Chart",
      "description": "Horizontal Stacked Bar Chart: Based on an official Vega-Lite example and adapted for use in Deneb within Power BI, it should be noted that cross-filtering interactivity is limited when using 'aggregate'. Autor: Cristobal Salcedo Beltrán.",
      "author": "Cristobal Salcedo Beltrán",
      "uuid": "290227de-ebef-42ff-977d-6015324bb0fd",
      "generated": "2024-07-24T04:59:06.947Z"
    },
    "dataset": [
      {
        "key": "__0__",
        "name": "Category",
        "description": "Represents the type or variety of the item. Example: \"Barley Variety\".",
        "type": "text",
        "kind": "column"
      },
      {
        "key": "__1__",
        "name": "Detail Category",
        "description": "Provides more specific details or sub-categories related to the main category. Example: \"Experimental Site\"",
        "type": "text",
        "kind": "column"
      },
      {
        "key": "__2__",
        "name": "Measure",
        "description": "Represents the quantitative measurement related to the categories. Example: \"Total Production Units",
        "type": "numeric",
        "kind": "measure"
      }
    ]
  },
  "config": {
    "view": {
      "stroke": "transparent"
    },
    "background": "#eee",
    "axisX": {
      "labels": false,
      "ticks": false,
      "domain": false,
      "grid": false,
      "title": ""
    },
    "axisY": {
      "labelFont": "arial",
      "offset": 5,
      "labelFontSize": 20,
      "title": ""
    }
  },
  "data": {
    "name": "dataset"
  },
  "encoding": {
    "x": {
      "aggregate": "sum",
      "field": "__2__",
      "type": "quantitative",
      "title": "Total Yield (units)",
      "stack": "zero"
    },
    "y": {
      "field": "__0__",
      "sort": {
        "op": "sum",
        "field": "__2__",
        "order": "descending"
      }
    },
    "color": {
      "field": "__1__",
      "type": "nominal",
      "scale": {
        "scheme": "category20"
      },
      "legend": {
        "titleFontSize": 20,
        "title": "Site",
        "labelFontStyle": "italic",
        "labelFont": "Arial",
        "orient": "right",
        "labelFontSize": 18
      }
    }
  },
  "layer": [
    {
      "mark": {
        "type": "bar",
        "opacity": 1,
        "stroke": "white",
        "strokeWidth": 2.5
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "center",
        "baseline": "middle",
        "fontSize": 16
      },
      "encoding": {
        "color": {
          "value": "white"
        },
        "text": {
          "aggregate": "sum",
          "field": "__2__",
          "type": "quantitative",
          "format": "#,0",
          "formatType": "pbiFormat"
        },
        "x": {
          "aggregate": "sum",
          "field": "__2__",
          "bandPosition": 0.5,
          "type": "quantitative",
          "title": ""
        },
        "detail": {
          "field": "__1__"
        }
      }
    }
  ],
  "title": {
    "text": "Total Yield of Different Barley Varieties by Experimental Site (1931-1932)",
    "subtitle": "Yield comparison across six experimental sites over two years",
    "subtitleFontSize": 18,
    "subtitleFontStyle": "italic",
    "font": "arial",
    "fontStyle": "normal",
    "fontSize": 20,
    "fontWeight": "bold"
  }
}
Get in touch!