🧱

Gráfico de barras apiladas horizontales

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’.

Vega-Lite stacked-bar intermedio ✓ Tooltip ✓ Context menu

Vista previa en vivo

Render con datos de muestra · Vega/Vega-Lite
Cargando vista previa…

Vista previa renderizada con el runtime de Deneb · MIT © Daniel Marsh-Patrick

🔽 Descargar .pbix 🔽 Descargar spec .json

🧩 Campos que espera

  1. __0__ Category texto
  2. __1__ Detail Category texto
  3. __2__ Measure número medida

Arrastra tus campos a Values en este orden.

🎯 Técnica

Motor
Vega-Lite 5.16.3
Marks
bar, text
Power BI
pbiFormat

Cómo usarlo en Power BI

  1. Agrega el visual Deneb a tu reporte e ingresa a su editor.
  2. Arrastra tus campos a Values en este orden: Category , Detail Category , Measure .
  3. Pega el spec (botón «Copiar spec» arriba) en el editor JSON de Deneb.
  4. Mapea tus columnas y ajusta a tu gusto. La interactividad nativa ya viene configurada.
Ver el spec Vega-Lite completo
spec.vega-lite.json
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "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'. Author: Cristobal Salcedo Beltran. Contact: contacto@csalcedodatabi.com",
      "author": "Cristobal Salcedo Beltran",
      "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": ""
    }
  },
  "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'. Author: Cristobal Salcedo Beltran. Contact: contacto@csalcedodatabi.com.",
  "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"
  }
}
¡Contáctame!