📊
Gráfico de barras agregado con filtrado cruzado
Gráfico de barras agregado con filtrado cruzado. Muestra la distribución de la población de EE. UU. por grupos de edad en el año 2000. Ejemplo de Vega-Lite adaptado a Deneb en Power BI, con cross-filter, etiquetas mejoradas con la función pbiFormat y alineadas a derecha e izquierda para una mejor lectura, con las barras en color blanco y negro.
Vega-Lite aggregate-bar intermedio
✓ Cross-filter
✓ Tooltip
✓ Context menu
✓ Selección
Vista previa en vivo
Render con datos de muestra · Vega/Vega-Lite🖱️ Cross-filter en vivo — haz clic en una marca y las demás se filtran/atenúan aquí mismo (runtime de Deneb reutilizado, MIT). Igual que en Power BI.
Cargando vista previa…
Vista previa renderizada con el runtime de Deneb · MIT © Daniel Marsh-Patrick
🧩 Campos que espera
-
__0__year texto -
__1__age número -
__2__people número -
__3__sex número
Arrastra tus campos a Values en este orden.
🎯 Técnica
- Motor
- Vega-Lite 5.16.3
- Marks
- bar, text
- Power BI
- pbiColor, pbiFormat
Cómo usarlo en Power BI
- Agrega el visual Deneb a tu reporte e ingresa a su editor.
- Arrastra tus campos a Values en este orden: year , age , people , sex .
- Pega el spec (botón «Copiar spec» arriba) en el editor JSON de Deneb.
- 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": true,
"highlight": false,
"dataPointLimit": 50
},
"information": {
"name": "Aggregate Bar Chart With Cross Filtering",
"description": "Aggregate Bar Chart With Cross Filtering",
"author": "Cristobal Salcedo Beltran",
"uuid": "42a2a19a-f7a8-47bb-8beb-5aad66093fc7",
"generated": "2024-06-15T05:34:29.859Z"
},
"dataset": [
{
"key": "__0__",
"name": "year",
"description": "",
"type": "text",
"kind": "column"
},
{
"key": "__1__",
"name": "age",
"description": "",
"type": "numeric",
"kind": "column"
},
{
"key": "__2__",
"name": "people",
"description": "",
"type": "numeric",
"kind": "column"
},
{
"key": "__3__",
"name": "sex",
"description": "",
"type": "numeric",
"kind": "column"
}
]
},
"config": {
"bar": {
"color": {
"expr": "pbiColor(0)"
}
},
"axisY": {
"labelFontSize": 16,
"titleFontSize": 15,
"grid": false,
"tickCount": 6
}
},
"description": "Aggregate Bar Chart With Cross Filtering. A bar chart showing the US population distribution by age groups in the year 2000. Example of Vega-Lite adapted to Deneb in Power BI, with cross-filtering, labels improved with the pbiFormat function, aligned to the right and left for better visualization, in the bars with white and black color. Author: Cristobal Salcedo Beltran, Email: contacto@csalcedodatabi.com",
"title": {
"text": "US Population Distribution by Age in 2000",
"anchor": "middle",
"fontSize": 20
},
"data": {
"name": "dataset"
},
"transform": [
{
"filter": "datum['__0__'] == '2000'"
},
{
"calculate": "datum.__selected__=='off'? 0: datum['__2__']",
"as": "Selected_People"
}
],
"height": {
"step": 35
},
"layer": [
{
"description": "Dimmed Bar; width = sum of people",
"mark": {
"type": "bar",
"opacity": 0.3,
"tooltip": true
},
"encoding": {
"x": {
"aggregate": "sum",
"field": "__2__",
"title": "People",
"axis": {
"titleFontSize": 15,
"labels": false,
"domain": false,
"ticks": false,
"orient": "top"
}
}
}
},
{
"description": "Fully Opaque Bar; width = Selected_People",
"mark": {
"type": "bar"
},
"encoding": {
"x": {
"aggregate": "sum",
"field": "Selected_People"
}
}
},
{
"transform": [
{
"aggregate": [
{
"op": "sum",
"field": "__2__",
"as": "sumPeople"
}
],
"groupby": [
"__1__"
]
},
{
"window": [
{
"op": "average",
"field": "sumPeople",
"as": "averagePeople"
}
],
"frame": [
null,
null
]
}
],
"name": "labels",
"description": "Labels",
"mark": {
"type": "text",
"align": {
"expr": "datum.sumPeople>datum.averagePeople?'right':'left'"
},
"dx": {
"expr": "datum.sumPeople>datum.averagePeople?-3:3"
},
"color": {
"expr": "datum.sumPeople>datum.averagePeople?'white':'black'"
},
"fontSize": 16
},
"encoding": {
"text": {
"value": {
"expr": "pbiFormat(datum.sumPeople, '#,0,,.0#M')"
}
},
"x": {
"field": "sumPeople",
"type": "quantitative",
"axis": {
"labels": false,
"domain": false,
"ticks": false,
"grid": false
}
}
}
}
],
"encoding": {
"y": {
"field": "__1__",
"title": "Age"
}
}
}