fieldType method

String fieldType(
  1. String field
)

What kind of thing a column holds, in Vega-Lite's vocabulary.

Read off the rows rather than declared: numbers are a quantity, ISO dates are an instant, anything else is a name. A column the data does not have answers the empty string, so a caller can tell "no such column" from "a column of names" — which is the difference between a mistake and a chart.

field The column name.

Returns quantitative, temporal, nominal, or the empty string when no row has the column.

Implementation

String fieldType(String field) {
  bool sawNumber = false;
  bool sawText = false;
  bool sawDate = false;
  bool sawAny = false;
  for ( final r in rows) {
    if ( r.has(field) ) {
      VlJson v = r._get(field);
      if ( false == v.isNull() ) {
        sawAny = true;
        if ( v.isNumber() ) {
          sawNumber = true;
        }
        if ( v.isString() ) {
          sawText = true;
          if ( VlDataset.looksLikeDate(v.str) ) {
            sawDate = true;
          }
        }
      }
    }
  }
  if ( false == sawAny ) {
    return "";
  }
  if ( sawText ) {
    if ( sawDate ) {
      return "temporal";
    }
    return "nominal";
  }
  if ( sawNumber ) {
    return "quantitative";
  }
  return "nominal";
}