分组标记的轴位置不正确

时间:2019-03-08 08:07:26

标签: vega

我想将axesmarks分组在一个group标记内,因为我希望条形图成为数据中的几个。但是,当我这样做时,x轴从条形图的底部移动到顶部。这是一个示例:

{
  "$schema": "https://vega.github.io/schema/vega/v4.json",
  "width": 400,
  "height": 200,
  "padding": 5,
  "data": [
    {
      "name": "cars",
      "format": {
        "type": "json",
        "parse": {
          "year": "date"
        }
      },
      "url": "https://vega.github.io/vega-datasets/data/cars.json",
      "transform": [
        {
          "type": "aggregate",
          "groupby": [
            "Origin"
          ],
          "as": [
            "num_records"
          ]
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {
        "data": "cars",
        "field": "Origin"
      },
      "range": "width",
      "padding": 0.05
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {
        "data": "cars",
        "field": "num_records"
      },
      "range": "height",
      "nice": true
    }
  ],
  "marks": [
    {
      "type": "group",
      "axes": [
        {
          "orient": "bottom",
          "scale": "x"
        },
        {
          "orient": "left",
          "scale": "y"
        }
      ],
      "marks": [
        {
          "type": "rect",
          "from": {
            "data": "cars"
          },
          "encode": {
            "enter": {
              "x": {
                "scale": "x",
                "field": "Origin"
              },
              "width": {
                "scale": "x",
                "band": 1
              },
              "y": {
                "scale": "y",
                "field": "num_records"
              },
              "y2": {
                "scale": "y",
                "value": 0
              }
            }
          }
        }
      ]
    }
  ]
}

Group Mark文档建议组支持此类嵌套的可视化规范。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我做错了没有编码组标记的宽度和高度。这是我修改后的示例:

{
  "$schema": "https://vega.github.io/schema/vega/v4.json",
  "width": 400,
  "height": 200,
  "padding": 5,
  "data": [
    {
      "name": "cars",
      "format": {
        "type": "json",
        "parse": {
          "year": "date"
        }
      },
      "url": "https://vega.github.io/vega-datasets/data/cars.json",
      "transform": [
        {
          "type": "aggregate",
          "groupby": [
            "Origin"
          ],
          "as": [
            "num_records"
          ]
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {
        "data": "cars",
        "field": "Origin"
      },
      "range": "width",
      "padding": 0.05
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {
        "data": "cars",
        "field": "num_records"
      },
      "range": "height",
      "nice": true
    }
  ],
  "marks": [
    {
      "type": "group",
      "axes": [
        {
          "orient": "bottom",
          "scale": "x"
        },
        {
          "orient": "left",
          "scale": "y"
        }
      ],
      "encode": {
        "enter": {
          "width": {
            "signal": "width"
          },
          "height": {
            "signal": "height"
          }
        }
      },
      "marks": [
        {
          "type": "rect",
          "from": {
            "data": "cars"
          },
          "encode": {
            "enter": {
              "x": {
                "scale": "x",
                "field": "Origin"
              },
              "width": {
                "scale": "x",
                "band": 1
              },
              "y": {
                "scale": "y",
                "field": "num_records"
              },
              "y2": {
                "scale": "y",
                "value": 0
              }
            }
          }
        }
      ]
    }
  ]
}