visualizationvega-litevegavega-embedvega-lite-api

How i stop Vegalite to rounding of y axis Tick label


I am creating a chart in which i want axis values should be same digit after decimal point as in data , i want this data driven .

I know format in axis but i want to avoid this because it create view like repetative on axis . Is there any way without use of format.

Editor link

I want data driven same upto decimal point as in data because sometime data come upto 2 decimal value and sometime four decimal value

Expected Output

enter image description here

I make this using format but i want this same number of digit as in data (dynamic data driven )

for another use case desire output enter image description here

Editor Link for Second case senerio


Solution

  • This is non trivial and you still have to use the format expression to get it working. In the example below, I take the first value in your dataset(provided as a string) and use the number of decimal places to specify the format string dynamically. Try changing the first row from 1.000 to 1.0000 for instance.

    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
        }
      ]
    }