2

我试图弄清楚如何在网络应用程序中使用 vega。

从我所看到的看起来可以调整图表大小,但我不完全确定如何去做?我是否必须手动更改 vega 正在工作的 json 对象,还是有更简单的方法?

另外,我在尝试自定义 x 轴时遇到了麻烦。我的日期范围从 0 到可能数千。希望能够动态设置 x 轴的刻度数据(通过刻度数据,我的意思是 x 轴如何递增.. 我的困惑的一部分我敢肯定,我不知道 vega 的刻度是什么意思, ETC。)。

顺便说一句,我在前端使用 Angular 和 ng-vega 将 vega 的“规范”放到示波器上。

4

1 回答 1

0
{ 
   "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"}
}
于 2019-10-30T06:59:14.440 回答