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

Stacked Bar Chart

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✓ Selection

Live preview

Rendered with sample data · Vega/Vega-Lite

🖱️ Click a category in the legend to highlight its series; the others dim. It works live here and in Power BI (Deneb selection).

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__ Date dateTime
  2. __1__ Category text

Drag your fields into Values in this order.

🎯 Technique

Engine
Vega-Lite 5.16.3
Marks
bar, point, text
Parameters
  • legendSel: highlights the series you click in the legend click on the legend
Power BI
  • pbiColor: takes a colour from the report theme
  • 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: Date , Category .
  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. 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/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": "Stacked Bar Chart",
      "description": "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": "68fcd6b9-6d3f-45a9-b0db-88f453cbc2df",
      "generated": "2024-07-11T02:41:35.472Z"
    },
    "dataset": [
      {
        "key": "__0__",
        "name": "Date",
        "description": "",
        "type": "dateTime",
        "kind": "column"
      },
      {
        "key": "__1__",
        "name": "Category",
        "description": "",
        "type": "text",
        "kind": "column"
      }
    ]
  },
  "config": {
    "axisY": {
      "labelFontSize": 14,
      "tickCount": 6,
      "gridDash": [
        4,
        8
      ]
    },
    "axisX": {
      "labelFontSize": 14,
      "offset": 1,
      "labelExpr": "[pbiFormat(datum.value,'MMM'),month(datum.value)===0? pbiFormat(datum.value,'yyyy'):'']"
    },
    "legend": {
      "orient": "top",
      "labelFontSize": 18
    }
  },
  "data": {
    "name": "dataset"
  },
  "title": {
    "text": "Weather Distribution by Month",
    "fontSize": 30
  },
  "name": "Mark_BarChart",
  "params": [
    {
      "name": "legendSel",
      "select": {
        "type": "point",
        "fields": [
          "__1__"
        ]
      },
      "bind": "legend"
    }
  ],
  "mark": "bar",
  "encoding": {
    "x": {
      "timeUnit": "month",
      "field": "__0__",
      "type": "ordinal",
      "title": null
    },
    "y": {
      "aggregate": "count",
      "type": "quantitative",
      "title": null
    },
    "color": {
      "field": "__1__",
      "type": "nominal",
      "scale": {
        "domain": [
          "sun",
          "fog",
          "drizzle",
          "rain",
          "snow"
        ],
        "range": [
          {
            "expr": "pbiColor(0,0.3)"
          },
          {
            "expr": "pbiColor(1,0.1)"
          },
          {
            "expr": "pbiColor(2,0.1)"
          },
          {
            "expr": "pbiColor(3,0.1)"
          },
          {
            "expr": "pbiColor(4,0.1)"
          }
        ]
      },
      "title": null
    },
    "opacity": {
      "condition": {
        "param": "legendSel",
        "value": 1
      },
      "value": 0.25
    }
  }
}
Get in touch!