vega-lite: Cannot format row/col

{
  "data": {"values": [{"a": 1.01,"b": 1.01,"m_teps": 859}]},
  "encoding": {
    "column": {"axis": {"format": "d"},"field": "b","type": "O"},
    "row": {"axis": {"format": "d"},"field": "a","type": "O"},
    "text": {"field": "m_teps","type": "Q", "format": "d"}
  },
  "mark": "text"
}
screen shot 2016-12-05 at 20 40 35

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (16 by maintainers)

Commits related to this issue

Most upvoted comments

I’m working on it. Will probably be done with the PR tonight

Not pretty but here is one (note that this is vl 2)

{
  "data": {"values": [{"a": 1.01,"b": 1.01,"m_teps": 859}]},
  "transform": {
    "calculate": [
      {
        "as": "fa",
        "expr": "format(datum.a, ',.1f')"
      },
      {
        "as": "fb",
        "expr": "format(datum.b, ',.1f')"
      }
    ]
  },
  "encoding": {
    "column": {"field": "fb","type": "O"},
    "row": {"field": "fa","type": "O"},
    "text": {"field": "m_teps","type": "Q", "format": "d"}
  },
  "mark": "text"
}

@NeelMohapatra will add a warning to vega-lite when you try to format an ordinal guide label and link to an example like the one above.

@NeelMohapatra We can actually allow formatting for nominal and ordinal fields.

  • Add formatType: 'number' | 'time' | 'utc' to Axis, Legend, and TextFieldDef

  • For text, tooltips and row/column headers, check if format is explicitly specified in formatSignalRef in compile/common.ts. If so, apply the right format function (based on formatType) to the output signal. If formatType is not specified, use 'number'.

  • For axis and legend, in compile/axis/encode.ts and compile/legend/encode.ts, check if fieldDef.type is ordinal / nominal and if format is explicitly specified. If so, apply the right format to labels’s text.

Note: see links for specific lines.

Okay, the fact that axis labels cannot be formatted is by design (https://github.com/vega/vega/issues/745#issuecomment-283156390). Two ways forward are: format numbers before we group them, or file an issue with Vega to support. The problem with the first approach is that it doesn’t work well if the same field is used for different guides. However, I’m okay with not supporting that.

Good call, thanks @domoritz. This appears to be working fairly well (for posterity):

def roundSig(column, significant_figures=1):
    # http://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python
    def roundSigFn(x, sig):
        return round(x, sig - int(math.floor(math.log10(x))) - 1)

    def fn(df):
        df[column] = df[column].apply(
            lambda x: roundSigFn(x, significant_figures))
        return df
    return fn