Reputation: 23
I am trying make stacked bar chart with totals labels above the columns. it is a graph that should show the number of pieces(rozdilKusu) in one shift(smena), divided according to different programs (pocitadlo). I try it with "transform" and "calculate", but I am not able to write the code correctly. Maybe I'm on the wrong track. Can somebody, help me? My code see below. Thanks a lot.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A basic stacked bar chart example.",
"width": 500,
"height": 200,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"smena": 0, "rozdilKusu": 28, "pocitadlo": 0}, {"smena": 0, "rozdilKusu": 55, "pocitadlo": 1},
{"smena": 1, "rozdilKusu": 43, "pocitadlo": 0}, {"smena": 1, "rozdilKusu": 91, "pocitadlo": 1},
{"smena": 2, "rozdilKusu": 25, "pocitadlo": 0}, {"smena": 2, "rozdilKusu": 53, "pocitadlo": 1},
{"smena": 3, "rozdilKusu": 19, "pocitadlo": 0}, {"smena": 3, "rozdilKusu": 87, "pocitadlo": 1},
{"smena": 4, "rozdilKusu": 52, "pocitadlo": 0}, {"smena": 4, "rozdilKusu": 48, "pocitadlo": 1},
{"smena": 5, "rozdilKusu": 24, "pocitadlo": 0}, {"smena": 5, "rozdilKusu": 49, "pocitadlo": 1},
{"smena": 6, "rozdilKusu": 87, "pocitadlo": 0}, {"smena": 6, "rozdilKusu": 66, "pocitadlo": 1},
{"smena": 7, "rozdilKusu": 17, "pocitadlo": 0}, {"smena": 7, "rozdilKusu": 27, "pocitadlo": 1},
{"smena": 8, "rozdilKusu": 68, "pocitadlo": 0}, {"smena": 8, "rozdilKusu": 16, "pocitadlo": 1},
{"smena": 9, "rozdilKusu": 49, "pocitadlo": 0}, {"smena": 9, "rozdilKusu": 15, "pocitadlo": 1}
],
"transform": [
{
"type": "stack",
"groupby": ["smena"],
"sort": {"field": "pocitadlo"},
"field": "rozdilKusu"
}
]
}
],
"scales": [
{
"name": "xscale",
"type": "band",
"range": "width",
"domain": {"data": "table", "field": "smena"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true, "zero": true,
"domain": {"data": "table", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "table", "field": "pocitadlo"}
}
],
"axes": [
{"orient": "bottom", "scale": "xscale", "zindex": 1},
{"orient": "left", "scale": "y", "zindex": 1}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "xscale", "field": "smena"},
"width": {"scale": "xscale", "band": 1, "offset": -1},
"y": {"scale": "y", "field": "y0"},
"y2": {"scale": "y", "field": "y1"},
"fill": {"scale": "color", "field": "pocitadlo"},
"tooltip": [{"field": "rozdilKusu", "type": "ordinal"}]
},
"update": {
"fillOpacity": {"value": 1}
},
"hover": {
"fillOpacity": {"value": 0.5}
}
}
}
]
}
Upvotes: 2
Views: 708
Reputation: 797
Here is your example Vega spec modified to show totals for each category. Added dataset with aggregate transform to calculate totals:
{
"name": "table_category_total",
"source": "table",
"transform": [
{
"type": "aggregate",
"groupby": ["smena"],
"fields": ["rozdilKusu"],
"ops": ["sum"],
"as": ["category_total"]
}
]
}
and text mark to show totals:
{
"type": "text",
"from": {"data": "table_category_total"},
"encode": {
"enter": {
"x": {"scale": "xscale", "field": "smena", "band": 0.5},
"y": {"scale": "y", "field": "category_total", "offset": -2},
"text": {"field": "category_total"},
"align": {"value": "center"},
"baseline": {"value": "bottom"},
"fill": {"value": "#333"}
}
}
References:
https://vega.github.io/vega/examples/stacked-bar-chart/
https://vega.github.io/vega/examples/bar-chart/
https://vega.github.io/vega/docs/data/
https://vega.github.io/vega/docs/transforms/aggregate/
Upvotes: 1