Vega-Lite - 基于行标题的总计不同聚合

问题描述 投票:0回答:1

在 Vega-Lite 中,我尝试创建一个总计列,其中行标题“预测”只是行值的总和,而“百分比”是行值的平均值。我设法让它部分地与 joinaggregate 一起工作,但似乎这些值被重复了几次?

enter image description here

这是我的代码:

{
  "data": {
    "values": [
      {
        "Customer": "Kevin N.V.",
        "Product": "PH114",
        "YearMonthText": "2024.01",
        "Column": "Forecast",
        "_Value": 24
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH114",
        "YearMonthText": "2024.01",
        "Column": "Percentage",
        "_Value": 0.9
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH114",
        "YearMonthText": "2024.02",
        "Column": "Forecast",
        "_Value": 74
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH114",
        "YearMonthText": "2024.02",
        "Column": "Percentage",
        "_Value": 0.75
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH878",
        "YearMonthText": "2024.01",
        "Column": "Forecast",
        "_Value": 744
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH878",
        "YearMonthText": "2024.01",
        "Column": "Percentage",
        "_Value": 0.05
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH878",
        "YearMonthText": "2024.02",
        "Column": "Forecast",
        "_Value": 4
      },
      {
        "Customer": "Kevin N.V.",
        "Product": "PH878",
        "YearMonthText": "2024.02",
        "Column": "Percentage",
        "_Value": 0.07
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH200",
        "YearMonthText": "2024.01",
        "Column": "Forecast",
        "_Value": 77
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH200",
        "YearMonthText": "2024.01",
        "Column": "Percentage",
        "_Value": 0.65
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH200",
        "YearMonthText": "2024.02",
        "Column": "Forecast",
        "_Value": 80
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH200",
        "YearMonthText": "2024.02",
        "Column": "Percentage",
        "_Value": 0.8
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH527",
        "YearMonthText": "2024.01",
        "Column": "Forecast",
        "_Value": 4
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH527",
        "YearMonthText": "2024.01",
        "Column": "Percentage",
        "_Value": 0.77
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH527",
        "YearMonthText": "2024.02",
        "Column": "Forecast",
        "_Value": 88
      },
      {
        "Customer": "Klaas N.V.",
        "Product": "PH527",
        "YearMonthText": "2024.02",
        "Column": "Percentage",
        "_Value": 0.9
      }
    ]
  },
  "facet": {
    "row": {"field": "Product", "title": null, "header": {"labelFontSize": 14}}
  },
  "spec": {
    "layer": [
      {"mark": {"type": "rect"}},
      {
        "mark": {"type": "text", "fontSize": 12},
        "encoding": {
          "y": {"field": "Column", "title": null},
          "x": {
            "field": "YearMonthText",
            "title": null,
            "axis": {"orient": "top"}
          },
          "text": {"field": "_Value", "type": "quantitative"}
        }
      },
      {
        "name": "Totals",
        "transform": [
          {
            "joinaggregate": [
              {
                "op": "sum",
                "field": "_Value",
                "as": "_New_Value_Sum"
              },
                            {
                "op": "mean",
                "field": "_Value",
                "as": "_New_Value_Mean"
              }
            ],
            "groupby": [
              "Product",
              "Column"
            ]
          },
          {
            "calculate": "datum.Column === 'Percentage' ? format( datum._New_Value_Mean, '.0%') : datum.Column === 'Forecast' ? format( datum._New_Value_Sum, ',.0f') : format( datum._New_Value_Sum, ',.0f')",
            "as": "_New_Value_Formatted"
          }
        ],
        "mark": {
          "type": "text",
          "align": "left",
          "fontSize": 14,
          "fontWeight": "bold",
          "dx": 80,
          "color": "black"
        },
        "encoding": {
          "y": {"field": "Column", "title": null},
          "x": {
            "field": "YearMonthText",
            "title": null,
            "axis": {"orient": "top"}
          },
          "text": {"field": "_New_Value_Formatted", "type": "nominal"}
        }
      }
    ]
  },
  "resolve": {"axis": {"x": "independent", "y": "independent"}}
}

有人可以帮我解决这个问题吗?

亲切的问候

vega-lite deneb
1个回答
0
投票

在最后一个 Total 层中:

1.) 将 joinaggregate 更改为聚合。

2.) 删除整个 X 轴。

3.) 例如将 dx 调整为 30。

© www.soinside.com 2019 - 2024. All rights reserved.