我如何阻止 Vegalite 对 y 轴进行舍入 勾选标签

问题描述 投票:0回答:1

我正在创建一个图表,其中我希望轴值应与数据中的小数点后的数字相同,我希望此数据驱动。

我知道轴中的格式,但我想避免这种情况,因为它会创建类似轴上重复的视图。 有没有不使用格式的方法。

编辑器链接

我希望数据驱动与数据中的小数点相同,因为有时数据达到 2 个小数点值,有时达到 4 个小数点值

预期产出

enter image description here

我使用格式进行此操作,但我想要与数据中相同的位数(动态数据驱动)

另一个用例 欲望输出 enter image description here

第二个案例的编辑链接

visualization vega-lite vega vega-embed vega-lite-api
1个回答
0
投票

这并不简单,您仍然必须使用格式表达式才能使其正常工作。在下面的示例中,我采用数据集中的第一个值(以字符串形式提供)并使用小数位数动态指定格式字符串。例如,尝试将第一行从 1.000 更改为 1.0000。

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A simple bar chart with embedded data.",
  "background": "white",
  "padding": 5,
  "height": 200,
  "style": "cell",
  "data": [
    {
      "name": "source_0",
      "values": [
        {"a": "A", "b": "1.000"},
        {"a": "B", "b": 2},
        {"a": "C", "b": 3},
        {"a": "D", "b": 4},
        {"a": "E", "b": 5},
        {"a": "F", "b": 6},
        {"a": "G", "b": 7},
        {"a": "H", "b": 8},
        {"a": "I", "b": 9}
      ]
    },
    {
      "name": "data_0",
      "source": "source_0",
      "transform": [
        {
          "type": "stack",
          "groupby": ["a"],
          "field": "b",
          "sort": {"field": [], "order": []},
          "as": ["b_start", "b_end"],
          "offset": "zero"
        },
        {
          "type": "filter",
          "expr": "isValid(datum[\"b\"]) && isFinite(+datum[\"b\"])"
        }
      ]
    }
  ],
  "signals": [
    {"name": "test", "update": "length(split(toString(data('source_0')[0]['b']),'.')[1])"},
    {"name": "x_step", "value": 20},
    {
      "name": "width",
      "update": "bandspace(domain('x').length, 0.1, 0.05) * x_step"
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "rect",
      "style": ["bar"],
      "from": {"data": "data_0"},
      "encode": {
        "update": {
          "fill": {"value": "#4c78a8"},
          "ariaRoleDescription": {"value": "bar"},
          "description": {
            "signal": "\"a: \" + (isValid(datum[\"a\"]) ? datum[\"a\"] : \"\"+datum[\"a\"]) + \"; b: \" + (format(datum[\"b\"], \".3f\"))"
          },
          "x": {"scale": "x", "field": "a"},
          "width": {"signal": "max(0.25, bandwidth('x'))"},
          "y": {"scale": "y", "field": "b_end"},
          "y2": {"scale": "y", "field": "b_start"}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {"data": "data_0", "field": "a", "sort": true},
      "range": {"step": {"signal": "x_step"}},
      "paddingInner": 0.1,
      "paddingOuter": 0.05
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {"data": "data_0", "fields": ["b_start", "b_end"]},
      "range": [{"signal": "height"}, 0],
      "nice": true,
      "zero": true
    }
  ],
  "axes": [
    {
      "scale": "y",
      "orient": "left",
      "tickRound": false,
      "gridScale": "x",
      "grid": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "domain": false,
      "labels": false,
      "aria": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "a",
      "labelAngle": 90,
      "labelAlign": "left",
      "labelBaseline": "middle",
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "b",
      "format": {"signal": "'.'+test+'f'"},
      "tickRound": false,
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "zindex": 0
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.