Vega Lite - 在分组级别创建总计

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

我正在尝试创建一个按“产品”列分组的矩阵。作为行标题,我使用“column”列,作为列标题,我使用“YearMonthText”列。我的矩阵中显示的值来自“_Value”列。

我想根据行标题“列”创建总计,以便对每个产品的预测进行求和,如果可能的话,对每个产品的百分比进行平均(不知道是否可以使用不同的聚合)

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.90},
      {"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": [
          {
            "aggregate": [
              {
                "op": "sum",
                "field": "_Value",
                "as": "_New_Value"
              }
            ],
            "groupby": [
              "Product",
              "Column"
            ]
          }
        ],
        "mark": {
          "type": "text",
          "align": "right",
          "fontSize": 16,
          "fontWeight": "bold",
          "x": 200,
          "color": "black"
        },
        "encoding": {
          "x": {
            "field": "Column",
            "type": "nominal",
            "axis": {"title": null}
          },
          "text": {
            "field": "_New_Value_Formatted",
            "type": "nominal"
          }
        }
      }
    ]
  },
  "resolve": {
    "axis": {
      "x": "independent",
      "y": "independent"
    }
  }
}

一旦我在“转换”中添加“计算”以进行格式设置,我就会收到一条错误消息:

         {
        "calculate": "if( datum['Column'] == 'Percentage', format( datum['_New_Value'], '.0%'), if( datum['Column'] == 'Forecast', format( datum['_New_Value'], ',.0f'), format( datum['_New_Value'], ',.0f')",
        "as": "_New_Value_Formatted"
      }

enter image description here

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

谢谢!

vega-lite deneb
1个回答
1
投票

试试这个:

{
  "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": {
    "width": {"step": 60},
    "layer": [
      {
        "mark": {
          "type": "rect",
          "color": "#2A6AC8",
          "cornerRadius": 5,
          "stroke": "#ffffff",
          "strokeWidth": 0
        }
      },
      {
        "mark": {"type": "text", "fontSize": 12, "color": "#ffffff"},
        "encoding": {
          "y": {
            "field": "Column",
            "title": null,
            "axis": {
              "grid": false,
              "domain": false,
              "ticks": false,
              "offset": 5
            }
          },
          "x": {
            "field": "YearMonthText",
            "title": null,
            "axis": {
              "orient": "top",
              "grid": false,
              "domain": false,
              "ticks": false,
              "offset": 3,
              "labelAngle": 0
            }
          },
          "text": {"field": "_Value", "type": "quantitative"}
        }
      },
      {
        "name": "Totals",
        "transform": [
          {
            "aggregate": [{"op": "sum", "field": "_Value", "as": "_New_Value"}],
            "groupby": ["Product", "Column"]
          },
          {
            "calculate": "datum.Column === 'Percentage' ? format( datum._New_Value, '.0%') : datum.Column === 'Forecast' ? format( datum._New_Value, ',.0f') : format( datum._New_Value, ',.0f')",
            "as": "_New_Value_Formatted"
          }
        ],
        "mark": {
          "type": "text",
          "align": "left",
          "fontSize": 14,
          "fontWeight": "bold",
          "dx": 70,
          "color": "black"
        },
        "encoding": {
          "y": {"field": "Column", "title": null},
          "text": {"field": "_New_Value_Formatted", "type": "nominal"}
        }
      }
    ]
  },
  "resolve": {"axis": {"x": "independent", "y": "independent"}},
  "config": {"view": {"stroke": "transparent"}}
}
© www.soinside.com 2019 - 2024. All rights reserved.