简体   繁体   English

为什么在这个 vega-lite 图中编码是这样拆分的?

[英]Why are the encodings split up like this in this vega-lite graph?

In this multi-line graph with a ruler style tooltip they split up the encoding between three layers & nest a layer inside an outer layer.在这个带有标尺样式工具提示的多线图中,他们将编码拆分为三层并在外层内嵌套一层。

https://vega.github.io/vega-lite/examples/interactive_multi_line_pivot_tooltip.html https://vega.github.io/vega-lite/examples/interactive_multi_line_pivot_tooltip.html

Specifically:具体来说:

 "encoding": {"x": {"field": "date", "type": "temporal"}},
  "layer": [
    {
      "encoding": {
        "color": {"field": "symbol", "type": "nominal"},
        "y": {"field": "price", "type": "quantitative"}
      },
      "layer": [
        {"mark": "line"},
        {"transform": [{"filter": {"selection": "hover"}}], "mark": "point"}
      ]
    },
    {
      "transform": [{"pivot": "symbol", "value": "price", "groupby": ["date"]}],
      "mark": "rule",
      "encoding": {
        "opacity": {
          "condition": {"value": 0.3, "selection": "hover"},
          "value": 0
        },
        "tooltip": [ ... ],
        "selection": { ... }
      }
    }

There's an encoding first outside the layers defining the x channel.在定义x通道的层之外首先有一个编码。 Then they add an encoding inside the first layer, defining the y & color channels.然后他们在第一层添加一个编码,定义ycolor通道。 Then they seem to nest a layer inside this outer layer and define the points that show up?然后他们似乎在这个外层嵌套了一层并定义了显示的点? Finally they add a second layer to define the tooltip.最后他们添加了第二层来定义工具提示。

What i'm not understanding however is然而我不明白的是

  1. What the point of this encoding block that is outside of the layers array.这个encoding块在layers组之外的意义是什么。 Whats the effect of this?这有什么作用? Why split up the encoding like this?为什么要这样拆分encoding

  2. There's a layer inside an outer layer, why?外层里面有一层,为什么?

The docs don't seem to explain any of this.文档似乎没有解释任何这些。

1) What the point of this encoding block that is outside of the layers array. 1)这个编码块在层数组之外的点是什么。 Whats the effect of this?这有什么作用?

Encodings above the layer are inherited by every subchart in the layer.层之上的编码被层中的每个子图继承。

2) Why split up the encoding like this. 2)为什么要像这样拆分编码。 There's a layer inside an outer layer, why?外层里面有一层,为什么?

The main reason you might use a multi-layer structure like this is to avoid repetition of encoding specifications.您可能会使用这样的多层结构的主要原因是避免重复编码规范。 You could create an equivalent result by moving all encodings into each layer, and using a single layer statement, like this ( view in editor ):您可以通过将所有编码移动到每一层并使用单层语句来创建等效结果,如下所示( 在编辑器中查看):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "data/stocks.csv"},
  "width": 400,
  "height": 300,
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "y": {"field": "price", "type": "quantitative"},
        "color": {"field": "symbol", "type": "nominal"}
      }
    },
    {
      "transform": [{"filter": {"selection": "hover"}}],
      "mark": "point",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "y": {"field": "price", "type": "quantitative"},
        "color": {"field": "symbol", "type": "nominal"}
      }
    },
    {
      "transform": [{"pivot": "symbol", "value": "price", "groupby": ["date"]}],
      "mark": "rule",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "opacity": {
          "condition": {"value": 0.3, "selection": "hover"},
          "value": 0
        },
        "tooltip": [
          {"field": "AAPL", "type": "quantitative"},
          {"field": "AMZN", "type": "quantitative"},
          {"field": "GOOG", "type": "quantitative"},
          {"field": "IBM", "type": "quantitative"},
          {"field": "MSFT", "type": "quantitative"}
        ]
      },
      "selection": {
        "hover": {
          "type": "single",
          "fields": ["date"],
          "nearest": true,
          "on": "mouseover",
          "empty": "none",
          "clear": "mouseout"
        }
      }
    }
  ]
}

It just involves a lot of repetition of equivalent encodings.它只是涉及大量重复等效编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM