json 如何在图例中添加不同的标记类型

3zwjbxry  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(52)

我想通过某个标记的属性(哪一个)为标记规则提供一个名称,然后将其添加到图例中?可以吗?
我需要在图表上提供一些这样的规则,所以在图例上看到它们会很有用。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"X": 1, "Y": 2, "Series": "A"}, 
      {"X": 1, "Y": 3, "Series": "B"}, 
      {"X": 2, "Y": 3, "Series": "A"}, 
      {"X": 2, "Y": 4, "Series": "B"}, 
      {"X": 3, "Y": 4, "Series": "A"}, 
      {"X": 3, "Y": 5, "Series": "B"},
      {"X": 4, "Y": 3, "Series": "A"}, 
      {"X": 4, "Y": 4, "Series": "B"},
      {"X": 5, "Y": 2, "Series": "A"}, 
      {"X": 5, "Y": 3, "Series": "B"}
    ]
  },
  "encoding": {
    "x": {"field": "X", "type": "nominal"},
    "y": {"field": "Y", "aggregate": "sum", "type": "quantitative"},
    "color": {"field": "Series"}
  },
  "layer":[
    {
      "mark":"bar"
    },
    {
      "mark": {
        "type": "rule",
        "stroke": "red",
        "strokeWidth": 1,
        "strokeDash": [8,4]
      },
      "encoding":{
        "x": {},
        "y": {"datum": 6}
      }     
    }    
  ]
}

字符串
Vega Lite编辑器


的数据

aemubtdh

aemubtdh1#

给你。一旦你的可视化变得更复杂,你可能最好使用Vega而不是VL。


的数据

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "encoding": {
    "x": {"field": "X", "type": "nominal"},
    "y": {"field": "Y", "aggregate": "sum", "type": "quantitative"}
  },
  "layer": [
    {
      "data": {
        "name": "a",
        "values": [
          {"X": 1, "Y": 2, "Series": "A"},
          {"X": 1, "Y": 3, "Series": "B"},
          {"X": 2, "Y": 3, "Series": "A"},
          {"X": 2, "Y": 4, "Series": "B"},
          {"X": 3, "Y": 4, "Series": "A"},
          {"X": 3, "Y": 5, "Series": "B"},
          {"X": 4, "Y": 3, "Series": "A"},
          {"X": 4, "Y": 4, "Series": "B"},
          {"X": 5, "Y": 2, "Series": "A"},
          {"X": 5, "Y": 3, "Series": "B"}
        ]
      },
      "mark": "bar",
      "encoding": {
        "fill": {"field": "Series", "legend": {"symbolType": "square"}}
      }
    },
    {
      "data": {"name": "b", "values": [{"Y": 6, "Series": "Goal", "C": "red"}]},
      "mark": {"type": "rule", "strokeWidth": 1, "strokeDash": [8, 4]},
      "encoding": {
        "x": {},
        "y": {"field": "Y"},
        "color": {
          "field": "Series",
          "legend": {"symbolType": "stroke"},
          "scale": {"range": {"field": "C"}}
        }
      }
    }
  ]
}

字符串

相关问题