How to use vega-util - 10 common examples

To help you get started, we’ve selected a few vega-util 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-lite / src / compile / compile.ts View on Github external
function getTopLevelProperties(
  inputSpec: TopLevel,
  autosize: AutoSizeType | AutoSizeParams,
  config: Config,
  model: Model
) {
  const width = model.component.layoutSize.get('width');
  const height = model.component.layoutSize.get('height');
  if (autosize === undefined) {
    autosize = {type: 'pad'};
  } else if (isString(autosize)) {
    autosize = {type: autosize};
  }
  if (width && height && isFitType(autosize.type)) {
    if (width === 'step' && height === 'step') {
      log.warn(log.message.droppingFit());
      autosize.type = 'pad';
    } else if (width === 'step' || height === 'step') {
      // effectively XOR, because else if

      // get step dimension
      const sizeType = width === 'step' ? 'width' : 'height';
      // log that we're dropping fit for respective channel
      log.warn(log.message.droppingFit(getPositionScaleChannel(sizeType)));

      // setting type to inverse fit (so if we dropped fit-x, type is now fit-y)
      const inverseSizeType = sizeType === 'width' ? 'height' : 'width';
github vega / vega-lite / src / compile / scale / domain.ts View on Github external
// we can omit the domain as it is inferred from the `bins` property
        return makeImplicit([]);
      }

      // ordinal bin scale takes domain from bin_range, ordered by bin start
      // This is useful for both axis-based scale (x/y) and legend-based scale (other channels).
      return makeImplicit([
        {
          // If sort by aggregation of a specified sort field, we need to use RAW table,
          // so we can aggregate values for the scale independently from the main aggregation.
          data: util.isBoolean(sort) ? model.requestDataName(MAIN) : model.requestDataName(RAW),
          // Use range if we added it and the scale does not support computing a range as a signal.
          field: model.vgField(channel, binRequiresRange(fieldDef, channel) ? {binSuffix: 'range'} : {}),
          // we have to use a sort object if sort = true to make the sort correct by bin start
          sort:
            sort === true || !isObject(sort)
              ? {
                  field: model.vgField(channel, {}),
                  op: 'min' // min or max doesn't matter since we sort by the start of the bin range
                }
              : sort
        }
      ]);
    } else {
      // continuous scales
      const {bin} = fieldDef;
      if (isBinning(bin)) {
        const binSignal = getBinSignalName(model, fieldDef.field, bin);
        return makeImplicit([
          new SignalRefWrapper(() => {
            const signal = model.getSignalName(binSignal);
            return `[${signal}.start, ${signal}.stop]`;
github vega / vega-dataflow / src / transforms / aggregate / Aggregate.js View on Github external
outputs = (this._outputs = []),
      inputMap = {};

  function inputVisit(get) {
    var fields = array(accessorFields(get)),
        i = 0, n = fields.length, f;
    for (; i
github vega / vega-lite / src / datetime.ts View on Github external
function normalizeMonth(m: string | number) {
  if (isNumber(m)) {
    // We accept 1-based month, so need to readjust to 0-based month
    return (m - 1).toString();
  } else {
    const lowerM = m.toLowerCase();
    const monthIndex = MONTHS.indexOf(lowerM);
    if (monthIndex !== -1) {
      return monthIndex + ''; // 0 for january, ...
    }
    const shortM = lowerM.substr(0, 3);
    const shortMonthIndex = SHORT_MONTHS.indexOf(shortM);
    if (shortMonthIndex !== -1) {
      return shortMonthIndex + '';
    }
    // Invalid month
    throw new Error(log.message.invalidTimeUnit('month', m));
  }
github vega / vega / packages / vega-transforms / src / Window.js View on Github external
function processPartition(list, state, cmp, _) {
  var sort = _.sort,
      range = sort && !_.ignorePeers,
      frame = _.frame || [null, 0],
      data = list.data(cmp), // use cmp for stable sort
      n = data.length,
      i = 0,
      b = range ? bisector(sort) : null,
      w = {
        i0: 0, i1: 0, p0: 0, p1: 0, index: 0,
        data: data, compare: sort || constant(-1)
      };

  for (state.init(); i
github vega / vega / packages / vega-geo / src / GeoShape.js View on Github external
prototype.transform = function(_, pulse) {
  var out = pulse.fork(pulse.ALL),
      shape = this.value,
      as = _.as || 'shape',
      flag = out.ADD;

  if (!shape || _.modified()) {
    // parameters updated, reset and reflow
    this.value = shape = shapeGenerator(
      getProjectionPath(_.projection),
      _.field || field('datum'),
      _.pointRadius
    );
    out.materialize().reflow();
    flag = out.SOURCE;
  }

  out.visit(flag, function(t) { t[as] = shape; });

  return out.modifies(as);
};
github vega / vega-lite / src / compile / repeater.ts View on Github external
function replaceRepeater(mapping: EncodingOrFacet, repeater: RepeaterValue): EncodingOrFacet {
  const out: EncodingOrFacet = {};
  for (const channel in mapping) {
    if (hasOwnProperty(mapping, channel)) {
      const channelDef: ChannelDef> | ChannelDef>[] = mapping[channel];

      if (isArray(channelDef)) {
        // array cannot have condition
        out[channel] = channelDef.map(cd => replaceRepeaterInChannelDef(cd, repeater)).filter(cd => cd);
      } else {
        const cd = replaceRepeaterInChannelDef(channelDef, repeater);
        if (cd !== undefined) {
          out[channel] = cd;
        }
      }
    }
  }
  return out;
}
github vega / vega-lite / build / src / compile / facet.js View on Github external
const outputName = facetSortFieldName(fieldDef, sort);
                    if (row && column) {
                        // For crossed facet, use pre-calculate field as it requires a different groupby
                        // For each calculated field, apply max and assign them to the same name as
                        // all values of the same group should be the same anyway.
                        fields.push(outputName);
                        ops.push('max');
                        as.push(outputName);
                    }
                    else {
                        fields.push(field);
                        ops.push(op);
                        as.push(outputName);
                    }
                }
                else if (isArray(sort)) {
                    const outputName = sortArrayIndexField(fieldDef, channel);
                    fields.push(outputName);
                    ops.push('max');
                    as.push(outputName);
                }
            }
        }
        const cross = !!row && !!column;
        return Object.assign({ name,
            data,
            groupby }, (cross || fields.length
            ? {
                aggregate: Object.assign({}, (cross ? { cross } : {}), (fields.length ? { fields, ops, as } : {}))
            }
            : {}));
    }
github vega / vega-lite / src / compile / repeat.ts View on Github external
private _initChildren(
    spec: NormalizedRepeatSpec,
    repeat: RepeatMapping | string[],
    repeater: RepeaterValue,
    config: Config
  ): Model[] {
    const children: Model[] = [];

    const row = (!isArray(repeat) && repeat.row) || [repeater ? repeater.row : null];
    const column = (!isArray(repeat) && repeat.column) || [repeater ? repeater.column : null];
    const repeatValues = (isArray(repeat) && repeat) || [repeater ? repeater.repeat : null];

    // cross product
    for (const repeatValue of repeatValues) {
      for (const rowValue of row) {
        for (const columnValue of column) {
          const name =
            (repeatValue ? `__repeat_repeat_${repeatValue}` : '') +
            (rowValue ? `__repeat_row_${rowValue}` : '') +
            (columnValue ? `__repeat_column_${columnValue}` : '');

          const childRepeat = {
            repeat: repeatValue,
            row: rowValue,
            column: columnValue
github vega / vega-lite / build / src / compile / mark / mark.js View on Github external
case 'latitude':
            case 'longitude':
            case 'latitude2':
            case 'longitude2':
            // TODO: case 'cursor':
            // text, shape, shouldn't be a part of line/trail/area
            case 'text':
            case 'shape':
            // tooltip fields should not be added to group by
            case 'tooltip':
                return details;
            case 'detail':
            case 'key':
                const channelDef = encoding[channel];
                if (isArray(channelDef) || isFieldDef(channelDef)) {
                    (isArray(channelDef) ? channelDef : [channelDef]).forEach(fieldDef => {
                        if (!fieldDef.aggregate) {
                            details.push(vgField(fieldDef, {}));
                        }
                    });
                }
                return details;
            case 'size':
                if (mark === 'trail') {
                    // For trail, size should not group trail lines.
                    return details;
                }
            // For line, it should group lines.
            /* tslint:disable */
            // intentional fall through
            case 'color':
            case 'fill':