如何在不同颜色的饼图中显示聚合总和值?

How to display the aggregated sum value inside a pie chart with different color?

我可以通过调整半径得到饼图中每个品牌的价值,但我无法改变颜色而不导致整个饼图变黑。我认为我可能需要为每个品牌的总和进行转换,但在正确的位置获得正确的值似乎是我的主要问题。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A simple pie chart with labels.",
  "height": 500,
  "width": 500,
  "data": {
    "values": [
      {"date": "4/15/2020", "brand": "Body Care", "mentions": 8599},
      {"date": "4/7/2020", "brand": "Makeup", "mentions": 9090},
      {"date": "6/29/2020", "brand": "Body Care", "mentions": 4563},
      {"date": "11/7/2020", "brand": "Skincare", "mentions": 3369},
      {"date": "10/10/2020", "brand": "Fragrance", "mentions": 7025},
      {"date": "10/25/2020", "brand": "Skincare", "mentions": 3523},
      {"date": "2/21/2020", "brand": "Fragrance", "mentions": 7976},
      {"date": "3/2/2020", "brand": "Hair", "mentions": 1463},
      {"date": "8/19/2020", "brand": "Fragrance", "mentions": 1912},
      {"date": "10/14/2020", "brand": "Fragrance", "mentions": 6804},
      {"date": "5/7/2020", "brand": "Makeup", "mentions": 4264},
      {"date": "8/4/2020", "brand": "Fragrance", "mentions": 9007},
      {"date": "9/17/2020", "brand": "Fragrance", "mentions": 5876},
      {"date": "1/12/2020", "brand": "Fragrance", "mentions": 5675},
      {"date": "7/17/2020", "brand": "Skincare", "mentions": 2555},
      {"date": "1/29/2020", "brand": "Body Care", "mentions": 6824},
      {"date": "9/19/2020", "brand": "Hair", "mentions": 3109},
      {"date": "2/4/2020", "brand": "Body Care", "mentions": 5807},
      {"date": "4/15/2020", "brand": "Fragrance", "mentions": 4999}
    ]
  },
  "encoding": {
    "theta": {
      "field": "mentions",
      "type": "quantitative",
      "stack": true,
      "aggregate": "sum"
    },
    "color": {
      "field": "brand",
      "type": "nominal",
      "legend": null,
      "scale": {"range": "category"}
    }
  },
  "layer": [
    {"mark": {"type": "arc", "outerRadius": 200}},
    {
      "mark": {"type": "text", "radius": 250, "fontSize": 16},
      "encoding": {"text": {"field": "brand", "type": "nominal"}, "color": {}}
    },
    {
      "mark": {"type": "text", "radius": 300, "color": "blue"},
      "encoding": {
        "text": {
          "field": "mentions",
          "type": "quantitative",
          "aggregate": "sum"
        },
        "color": {"field": "brand", "type": "nominal"}
      }
    }
  ],
  "view": {"stroke": null}
}

您可以将 "color" 编码移动到您想要应用它的图层中,然后使用 "detail" 编码对文本进行分组而不添加色阶 (view in editor ):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A simple pie chart with labels.",
  "height": 500,
  "width": 500,
  "data": {
    "values": [
      {"date": "4/15/2020", "brand": "Body Care", "mentions": 8599},
      {"date": "4/7/2020", "brand": "Makeup", "mentions": 9090},
      {"date": "6/29/2020", "brand": "Body Care", "mentions": 4563},
      {"date": "11/7/2020", "brand": "Skincare", "mentions": 3369},
      {"date": "10/10/2020", "brand": "Fragrance", "mentions": 7025},
      {"date": "10/25/2020", "brand": "Skincare", "mentions": 3523},
      {"date": "2/21/2020", "brand": "Fragrance", "mentions": 7976},
      {"date": "3/2/2020", "brand": "Hair", "mentions": 1463},
      {"date": "8/19/2020", "brand": "Fragrance", "mentions": 1912},
      {"date": "10/14/2020", "brand": "Fragrance", "mentions": 6804},
      {"date": "5/7/2020", "brand": "Makeup", "mentions": 4264},
      {"date": "8/4/2020", "brand": "Fragrance", "mentions": 9007},
      {"date": "9/17/2020", "brand": "Fragrance", "mentions": 5876},
      {"date": "1/12/2020", "brand": "Fragrance", "mentions": 5675},
      {"date": "7/17/2020", "brand": "Skincare", "mentions": 2555},
      {"date": "1/29/2020", "brand": "Body Care", "mentions": 6824},
      {"date": "9/19/2020", "brand": "Hair", "mentions": 3109},
      {"date": "2/4/2020", "brand": "Body Care", "mentions": 5807},
      {"date": "4/15/2020", "brand": "Fragrance", "mentions": 4999}
    ]
  },
  "encoding": {
    "theta": {
      "field": "mentions",
      "type": "quantitative",
      "stack": true,
      "aggregate": "sum"
    }
  },
  "layer": [
    {
      "mark": {"type": "arc", "outerRadius": 200},
      "encoding": {
        "color": {
          "field": "brand",
          "type": "nominal",
          "legend": null,
          "scale": {"range": "category"}
        }
      }
    },
    {
      "mark": {"type": "text", "radius": 250, "fontSize": 16},
      "encoding": {"text": {"field": "brand", "type": "nominal"}}
    },
    {
      "mark": {"type": "text", "radius": 300, "color": "blue"},
      "encoding": {
        "text": {
          "field": "mentions",
          "type": "quantitative",
          "aggregate": "sum"
        },
        "detail": {"field": "brand", "type": "nominal"}
      }
    }
  ],
  "view": {"stroke": null}
}