Vega 堆叠条形图 - 如何添加总计标签

Vega stacked barchart - how to add totals labels

我正在尝试制作在列上方带有总计标签的堆积条形图。它是一个图表,应该显示一个班次(smena)中的件数(rozdilKusu),根据不同的程序(pocitadlo)划分。我尝试使用“转换”和“计算”,但我无法正确编写代码。也许我走错了路。有人可以帮助我吗? 我的代码见下文。非常感谢。

{
  "$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}
        }
      }
    }
  ]
}

这里是您的示例 Vega 规范,已修改以显示每个类别的总数。添加了具有 聚合 转换的数据集以计算总数:

 {
  "name": "table_category_total",
  "source": "table",
  "transform": [
    {
      "type": "aggregate",
      "groupby": ["smena"],
      "fields": ["rozdilKusu"],
      "ops": ["sum"],
      "as": ["category_total"]
    }
  ]
}

text标记以显示总数:

{
  "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"}
    }
  }

参考文献:

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/

Open in Vega online editor