How to use the vega-lite.fieldDef function in vega-lite

To help you get started, we’ve selected a few vega-lite examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vega / vega-tooltip / src / vg-tooltip.ts View on Github external
// handle corner case: if T is present in vlSpec, then we keep both T and (TIMEUNIT)T
    var fieldDefs = vl.spec.fieldDefs(vlSpec);
    for (var i = 0; i < fieldDefs.length; i++) {
      if (fieldDefs[i].field === originalTemporalField && !fieldDefs[i].timeUnit) {
        supplementedFieldOption.removeOriginalTemporalField = undefined;
        break;
      }
    }

  }

  // supplement title
  if (!config.countTitle) config.countTitle = vl.config.defaultConfig.countTitle; // use vl default countTitle
  supplementedFieldOption.title = fieldOption.title ?
    fieldOption.title : vl.fieldDef.title(fieldDef, config);

  // supplement formatType
  supplementedFieldOption.formatType = fieldOption.formatType ?
    fieldOption.formatType : formatTypeMap[fieldDef.type];

  // supplement format
  if (fieldOption.format) {
    supplementedFieldOption.format = fieldOption.format;
  }
  // when user doesn't provide format, supplement format using timeUnit, timeFormat, and numberFormat
  else {
    switch (supplementedFieldOption.formatType) {
      case "time":
        supplementedFieldOption.format = fieldDef.timeUnit ?
          // TODO(zening): use template for all time fields, to be consistent with Vega-Lite
          vl.timeUnit.template(fieldDef.timeUnit, "", false).match(/time:'[%-a-z]*'/i)[0].split("'")[1]
github vega / vega-tooltip / src / vg-tooltip.ts View on Github external
}

  // if either one of fieldOption and fieldDef is undefined, make it an empty object
  if (!fieldOption && fieldDef) fieldOption = {};
  if (fieldOption && !fieldDef) fieldDef = {};

  // the supplemented field option
  var supplementedFieldOption: supplementedFieldOption = {};

  // supplement a user-provided field name with underscore prefixes and suffixes to
  // match the field names in item.datum
  // for aggregation, this will add prefix "mean_" etc.
  // for timeUnit, this will add prefix "yearmonth_" etc.
  // for bin, this will add prefix "bin_" and suffix "_start". Later we will replace "_start" with "_range".
  supplementedFieldOption.field = fieldDef.field ?
    vl.fieldDef.field(fieldDef) : fieldOption.field;

  // If a fieldDef is a (TIMEUNIT)T, we check if the original T is present in the vlSpec.
  // If only (TIMEUNIT)T is present in vlSpec, we set `removeOriginalTemporalField` to T,
  // which will cause function removeDuplicateTimeFields() to remove T and only keep (TIMEUNIT)T
  // in item data.
  // If both (TIMEUNIT)T and T are in vlSpec, we set `removeOriginalTemporalField` to undefined,
  // which will leave both T and (TIMEUNIT)T in item data.
  // Note: user should never have to provide this boolean in options
  if (fieldDef.type === TEMPORAL && fieldDef.timeUnit) {
    // in most cases, if it's a (TIMEUNIT)T, we remove original T
    var originalTemporalField = fieldDef.field;
    supplementedFieldOption.removeOriginalTemporalField = originalTemporalField;

    // handle corner case: if T is present in vlSpec, then we keep both T and (TIMEUNIT)T
    var fieldDefs = vl.spec.fieldDefs(vlSpec);
    for (var i = 0; i < fieldDefs.length; i++) {
github vega / vega-tooltip / src / supplementField.ts View on Github external
}
  if (fieldOption && !fieldDef) {
    fieldDef = {} as any;  // type will be added later, TODO: refactor this
  }

  // the supplemented field option
  const supplementedFieldOption: SupplementedFieldOption = {};

  // supplement a user-provided field name with underscore prefixes and suffixes to
  // match the field names in item.datum
  // for aggregation, this will add prefix "mean_" etc.
  // for timeUnit, this will add prefix "yearmonth_" etc.
  // for bin, this will add prefix "bin_" and suffix "_start". Later we will replace "_start" with "_range".

  if (fieldDef.bin) {
    fieldDef.bin = vl.fieldDef.normalizeBin(fieldDef.bin, undefined);
  }
  supplementedFieldOption.field = vl.fieldDef.vgField(fieldDef) || fieldOption.field;

  // If a fieldDef is a (TIMEUNIT)T, we check if the original T is present in the vlSpec.
  // If only (TIMEUNIT)T is present in vlSpec, we set `removeOriginalTemporalField` to T,
  // which will cause function removeDuplicateTimeFields() to remove T and only keep (TIMEUNIT)T
  // in item data.
  // If both (TIMEUNIT)T and T are in vlSpec, we set `removeOriginalTemporalField` to undefined,
  // which will leave both T and (TIMEUNIT)T in item data.
  // Note: user should never have to provide this boolean in options
  if (fieldDef.type === TEMPORAL && fieldDef.timeUnit) {
    // in most cases, if it's a (TIMEUNIT)T, we remove original T
    const originalTemporalField = fieldDef.field;
    supplementedFieldOption.removeOriginalTemporalField = originalTemporalField;

    // handle corner case: if T is present in vlSpec, then we keep both T and (TIMEUNIT)T
github vega / vega-tooltip / src / supplementField.ts View on Github external
// handle corner case: if T is present in vlSpec, then we keep both T and (TIMEUNIT)T
    const fieldDefs = vl.spec.fieldDefs(vlSpec);
    for (const items of fieldDefs) {
      if (items.field === originalTemporalField && !items.timeUnit) {
        supplementedFieldOption.removeOriginalTemporalField = undefined;
        break;
      }
    }
  }

  // supplement title
  if (!config.countTitle) {
    config.countTitle = vl.config.defaultConfig.countTitle; // use vl default countTitle
  }
  let defaultTitle = vl.fieldDef.title(fieldDef, config);
  if (isPositionFieldDef(fieldDef)) {
    if (fieldDef.axis && fieldDef.axis.title) {
      defaultTitle = fieldDef.axis.title;
    }
  }
  if (isMarkPropFieldDef(fieldDef)) {
    if (fieldDef.legend && fieldDef.legend.title) {
      defaultTitle = fieldDef.legend.title;
    }
  }

  supplementedFieldOption.title = fieldOption.title || defaultTitle;

  // supplement formatType
  supplementedFieldOption.formatType = fieldOption.formatType || formatTypeMap[fieldDef.type];