首页 > 解决方案 > Vega 图例和每标记颜色

问题描述

正如您在示例中看到的,我想创建一个带有多个标记的图表。这些标记中的每一个都需要特定的颜色、相应的标签和图例。我知道我通过“域”定义颜色:{“数据”:“表”,“字段”:“区域”}。但是我的“小计”和“总计”行需要特定的颜色。图例应为 Neustadt - Totals 和 Neustadt - Subtotals。

我已经玩过可以在scales文档中找到的内容。但我似乎无法通过引用标记字段制作颜色和图例项目。

"data": [
    {
      "name": "table",
      "values": [
        {"District": "Neustadt", "total": "86", "id": 12, "subtotal": "600", "Year": "2017"},
        {"District": "Neustadt", "total": "398", "id": 13, "subtotal": "100", "Year": "2018"},
        {"District": "Neustadt", "total": "155", "id": 14, "subtotal": "10", "Year": "2019"}
      ],
      "transform": [
        {
          "type": "collect",
          "sort": {
            "field": "Year"
          }
        }
      ]
    }
  ],

  "scales": [
    {
      "name": "Year",
      "type": "point",
      "range": "width",
      "domain": {"data": "table", "field": "Year", "sort": true}
    },
    {
      "name": "subtotal",
      "type": "linear",
      "range": "height",
      "nice": true,
      "zero": true,
      "domain": {"data": "table", "field": "subtotal"}
    },
    {
      "name": "color",
      "type": "ordinal",
      "range": "category",
      "domain": {"data": "table", "field": "District"}
    }
  ]

如果我在数据中有另一个区域,我只能得到一个图例项目,当然也只能得到多个项目。分组标记是一种选择吗?

标签: legendvega

解决方案


推荐阅读