【问题标题】:vega lite grouping for pie chart用于饼图的 vega lite 分组
【发布时间】:2021-10-22 17:36:06
【问题描述】:

我如何知道 vega-lite 会产生多少分组?示例用例是:说..我想派生该分组信息以显示一条消息,说“您有太多分组”或根据分组数量找出更好的配色方案。 此处为 vega-lite 规范示例:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple pie chart with labels.",
  "data": {
    "values": [
      {"category": "a", "value": 4},
      {"category": "b", "value": 6},
      {"category": "c", "value": 10},
      {"category": "d", "value": 3},
      {"category": "e", "value": 7},
      {"category": "f", "value": 8},{"category": "f", "value": 8}
    ]
  },
  "encoding": {
    "theta": {"field": "value", "type": "quantitative", "stack": true},
    "color": {"field": "category", "type": "nominal"}

  },
  "layer": [{
    "mark": {"type": "arc", "outerRadius": 80}
  }, {
    "mark": {"type": "text", "radius": 90},
    "encoding": {
      "text": {"field": "category", "type": "nominal"}
    }
  }]
}

【问题讨论】:

    标签: vega-lite


    【解决方案1】:

    假设您的问题是“我如何计算 category 字段中有多少唯一项目,您可以使用 joinaggregate 转换和 distinct 运算符来计算。

    // ... the rest of your spec
    "transform": [
        {
          "joinaggregate": [{
           "op": "distinct",
           "field": "categories",
           "as": "number_of_categories"
          }]
        }
    

    在您的示例中,结果将是向表中的每一行添加一个值为 6 的列 number_of_categories

    您可以阅读有关joinaggregate here 的更多信息,并找到所有可用的运算符on the aggregate 页面。

    【讨论】:

    • 我想知道是否可以从代码中的规范派生number_of_categories。有没有办法获得这些信息?我可能在考虑使用 vega View API,但不太确定
    • 嗨,我认为它可以从 view.dataset() API 中检索到。检索数据集后,number_of_categories 将成为行中的列之一。请参阅vega.github.io/vega/docs/api/view/#view_data 了解更多详情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2019-06-02
    • 2021-05-15
    • 2017-08-17
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多