Vega-独特图表上的多个图例

时间:2019-07-17 09:13:48

标签: legend vega vega-lite

我最近开始与Kibana合作,更具体地说是与Vega合作-我对传奇有一些疑问。 我希望能够在一个图表中添加多个图例-如本示例中所示的堆积条形图:https://vega.github.io/editor/#/examples/vega-lite/stacked_bar_h(我想在下面添加特定数据-例如,百分比)

我将图例设为水平-方向:水平

我希望最终结果是这样的:

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "autosize": "pad",
  "padding": 5,
  "width": 200,
  "style": "cell",
  "data": [
    {
      "name": "source_0",
      "url": "data/barley.json",
      "format": {"type": "json"},
      "transform": [
        {
          "type": "aggregate",
          "groupby": ["variety", "site"],
          "ops": ["sum"],
          "fields": ["yield"],
          "as": ["sum_yield"]
        },
        {
          "type": "stack",
          "groupby": ["variety"],
          "field": "sum_yield",
          "sort": {"field": ["site"], "order": ["descending"]},
          "as": ["sum_yield_start", "sum_yield_end"],
          "offset": "zero"
        }
      ]
    }
  ],
  "signals": [
    {"name": "y_step", "value": 20},
    {
      "name": "height",
      "update": "bandspace(domain('y').length, 0.1, 0.05) * y_step"
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "rect",
      "style": ["bar"],
      "from": {"data": "source_0"},
      "encode": {
        "update": {
          "fill": {"scale": "color", "field": "site"},
          "tooltip": {
            "signal": "{\"Sum of yield\": format(datum[\"sum_yield\"], \"\"), \"variety\": ''+datum[\"variety\"], \"site\": ''+datum[\"site\"]}"
          },
          "x": {"scale": "x", "field": "sum_yield_end"},
          "x2": {"scale": "x", "field": "sum_yield_start"},
          "y": {"scale": "y", "field": "variety"},
          "height": {"scale": "y", "band": true}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "linear",
      "domain": {
        "data": "source_0",
        "fields": ["sum_yield_start", "sum_yield_end"]
      },
      "range": [0, {"signal": "width"}],
      "nice": true,
      "zero": true
    },
    {
      "name": "y",
      "type": "band",
      "domain": {"data": "source_0", "field": "variety", "sort": true},
      "range": {"step": {"signal": "y_step"}},
      "paddingInner": 0.1,
      "paddingOuter": 0.05
    },
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "source_0", "field": "site", "sort": true},
      "range": "category"
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "Sum of yield",
      "labelFlush": true,
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "zindex": 1
    },
    {
      "scale": "x",
      "orient": "bottom",
      "gridScale": "y",
      "grid": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "domain": false,
      "labels": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "variety",
      "zindex": 1
    }
  ],
  "legends": [
    {
      "direction": "horizontal",
      "fill": "color",
      "gradientLength": {"signal": "clamp(height, 64, 200)"},
      "symbolType": "circle",
      "encode": {"symbols": {"update": {"shape": {"value": "square"}}}}
    }
  ]
}`

有什么办法可以做到这一点? 任何帮助将不胜感激。

0 个答案:

没有答案