Javascript Vega如何创建动态调整大小的图表&;如何更改x轴?

Javascript Vega如何创建动态调整大小的图表&;如何更改x轴?,javascript,angularjs,vega,Javascript,Angularjs,Vega,我想知道如何在web应用程序中使用vega 从我所看到的,它看起来像是可以调整图表大小,但我不完全知道如何去做呢?我是否需要手动更改vega正在处理的json对象,还是有更简单的方法 另外,我在尝试自定义x轴时遇到了麻烦。我的日期从0到数千不等。希望能够动态设置x轴的刻度数据(刻度数据指的是x轴的增量。我肯定的是,我不知道vega的刻度是什么意思,等等) 顺便说一句,我使用angular作为ng vega的前端,将vega的“规格”放在范围内 { "signals":[ {

我想知道如何在web应用程序中使用vega

从我所看到的,它看起来像是可以调整图表大小,但我不完全知道如何去做呢?我是否需要手动更改vega正在处理的json对象,还是有更简单的方法

另外,我在尝试自定义x轴时遇到了麻烦。我的日期从0到数千不等。希望能够动态设置x轴的刻度数据(刻度数据指的是x轴的增量。我肯定的是,我不知道vega的刻度是什么意思,等等)

顺便说一句,我使用angular作为ng vega的前端,将vega的“规格”放在范围内

{ 
   "signals":[ 
      { 
         "name":"width",
         "init":"isFinite(containerSize()[0]) ? containerSize()[0] : 200",
         "on":[ 
            { 
               "update":"isFinite(containerSize()[0]) ? containerSize()[0] : 200",
               "events":"window:resize"
            }
         ]
      }
   ]
}
请查看样本

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "autosize": {"type": "fit", "contains": "padding"},
  "background": "white",
  "padding": 5,
  "height": 200,
  "style": "cell",
  "data": [
    {
      "name": "source_0",
      "url": "data/seattle-weather.csv",
      "format": {"type": "csv", "parse": {"date": "date"}, "delimiter": ","},
      "transform": [
        {
          "type": "formula",
          "as": "month_date",
          "expr": "datetime(0, month(datum[\"date\"]), 1, 0, 0, 0, 0)"
        },
        {
          "type": "formula",
          "as": "month_date_end",
          "expr": "datetime(0, month(datum[\"date\"])+1, 1, 0, 0, 0, 0)"
        },
        {
          "type": "aggregate",
          "groupby": ["month_date", "month_date_end"],
          "ops": ["mean"],
          "fields": ["precipitation"],
          "as": ["mean_precipitation"]
        },
        {
          "type": "filter",
          "expr": "(isDate(datum[\"month_date\"]) || (isValid(datum[\"month_date\"]) && isFinite(+datum[\"month_date\"])))"
        }
      ]
    }
  ],
  "signals": [
    {
      "name": "width",
      "init": "isFinite(containerSize()[0]) ? containerSize()[0] : 200",
      "on": [
        {
          "update": "isFinite(containerSize()[0]) ? containerSize()[0] : 200",
          "events": "window:resize"
        }
      ]
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "rect",
      "style": ["bar"],
      "from": {"data": "source_0"},
      "encode": {
        "update": {
          "fill": {"value": "#4c78a8"},
          "x2": [
            {
              "test": "!isValid(datum[\"month_date\"]) || !isFinite(+datum[\"month_date\"])",
              "value": 0
            },
            {"scale": "x", "field": "month_date", "offset": 1}
          ],
          "x": [
            {
              "test": "!isValid(datum[\"month_date\"]) || !isFinite(+datum[\"month_date\"])",
              "value": 0
            },
            {"scale": "x", "field": "month_date_end"}
          ],
          "y": {"scale": "y", "field": "mean_precipitation"},
          "y2": {"scale": "y", "value": 0}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "time",
      "domain": {
        "data": "source_0",
        "fields": ["month_date", "month_date_end"]
      },
      "range": [0, {"signal": "width"}]
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {"data": "source_0", "field": "mean_precipitation"},
      "range": [{"signal": "height"}, 0],
      "nice": true,
      "zero": true
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "gridScale": "y",
      "grid": true,
      "domain": false,
      "labels": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "gridScale": "x",
      "grid": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "domain": false,
      "labels": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "date (month)",
      "labelFlush": true,
      "labelOverlap": true,
      "encode": {
        "labels": {
          "update": {"text": {"signal": "timeFormat(datum.value, '%b')"}}
        }
      },
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "Mean of precipitation",
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "zindex": 0
    }
  ],
  "config": {"background": "white"}
}