How to use vega-scale - 10 common examples

To help you get started, we’ve selected a few vega-scale 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 / packages / vega-encode / src / Scale.js View on Github external
function configureRangeStep(type, _, count) {
  if (type !== Band && type !== Point) {
    error('Only band and point scales support rangeStep.');
  }

  // calculate full range based on requested step size and padding
  var outer = (_.paddingOuter != null ? _.paddingOuter : _.padding) || 0,
      inner = type === Point ? 1
            : ((_.paddingInner != null ? _.paddingInner : _.padding) || 0);
  return [0, _.rangeStep * bandSpace(count, inner, outer)];
}
github vega / vega / packages / vega-encode / src / Scale.js View on Github external
// else if a range scheme is defined, use that
  else if (_.scheme) {
    range = configureScheme(type, _, count);
    if (isFunction(range)) {
      if (scale.interpolator) {
        return scale.interpolator(range);
      } else {
        error(`Scale type ${type} does not support interpolating color schemes.`);
      }
    }
  }

  // given a range array for an interpolating scale, convert to interpolator
  if (range && isInterpolating(type)) {
    return scale.interpolator(
      interpolateColors(flip(range, _.reverse), _.interpolate, _.interpolateGamma)
    );
  }

  // configure rounding / interpolation
  if (range && _.interpolate && scale.interpolate) {
    scale.interpolate(getInterpolate(_.interpolate, _.interpolateGamma));
  } else if (isFunction(scale.round)) {
    scale.round(round);
  } else if (isFunction(scale.rangeRound)) {
    scale.interpolate(round ? interpolateRound : interpolate);
  }

  if (range) scale.range(flip(range, _.reverse));
}
github vega / vega / packages / vega-encode / src / Scale.js View on Github external
function configureScheme(type, _, count) {
  var extent = _.schemeExtent,
      name, scheme;

  if (isArray(_.scheme)) {
    scheme = interpolateColors(_.scheme, _.interpolate, _.interpolateGamma);
  } else {
    name = _.scheme.toLowerCase();
    scheme = getScheme(name);
    if (!scheme) error(`Unrecognized scheme name: ${_.scheme}`);
  }

  // determine size for potential discrete range
  count = (type === Threshold) ? count + 1
    : (type === BinOrdinal) ? count - 1
    : (type === Quantile || type === Quantize) ? (+_.schemeCount || DEFAULT_COUNT)
    : count;

  // adjust and/or quantize scheme as appropriate
  return isInterpolating(type) ? adjustScheme(scheme, extent, _.reverse)
    : isFunction(scheme) ? quantizeInterpolator(adjustScheme(scheme, extent), count)
    : type === Ordinal ? scheme : scheme.slice(0, count);
github vega / vega / packages / vega-encode / src / Scale.js View on Github external
if (isArray(_.scheme)) {
    scheme = interpolateColors(_.scheme, _.interpolate, _.interpolateGamma);
  } else {
    name = _.scheme.toLowerCase();
    scheme = getScheme(name);
    if (!scheme) error(`Unrecognized scheme name: ${_.scheme}`);
  }

  // determine size for potential discrete range
  count = (type === Threshold) ? count + 1
    : (type === BinOrdinal) ? count - 1
    : (type === Quantile || type === Quantize) ? (+_.schemeCount || DEFAULT_COUNT)
    : count;

  // adjust and/or quantize scheme as appropriate
  return isInterpolating(type) ? adjustScheme(scheme, extent, _.reverse)
    : isFunction(scheme) ? quantizeInterpolator(adjustScheme(scheme, extent), count)
    : type === Ordinal ? scheme : scheme.slice(0, count);
}
github vega / vega / packages / vega-encode / src / Scale.js View on Github external
}

  // else if a range scheme is defined, use that
  else if (_.scheme) {
    range = configureScheme(type, _, count);
    if (isFunction(range)) {
      if (scale.interpolator) {
        return scale.interpolator(range);
      } else {
        error(`Scale type ${type} does not support interpolating color schemes.`);
      }
    }
  }

  // given a range array for an interpolating scale, convert to interpolator
  if (range && isInterpolating(type)) {
    return scale.interpolator(
      interpolateColors(flip(range, _.reverse), _.interpolate, _.interpolateGamma)
    );
  }

  // configure rounding / interpolation
  if (range && _.interpolate && scale.interpolate) {
    scale.interpolate(getInterpolate(_.interpolate, _.interpolateGamma));
  } else if (isFunction(scale.round)) {
    scale.round(round);
  } else if (isFunction(scale.rangeRound)) {
    scale.interpolate(round ? interpolateRound : interpolate);
  }

  if (range) scale.range(flip(range, _.reverse));
}
github vega / vega / packages / vega-encode / src / Scale.js View on Github external
function domainCheck(type, domain, df) {
  if (isLogarithmic(type)) {
    // sum signs of domain values
    // if all pos or all neg, abs(sum) === domain.length
    var s = Math.abs(domain.reduce(function(s, v) {
      return s + (v < 0 ? -1 : v > 0 ? 1 : 0);
    }, 0));

    if (s !== domain.length) {
      df.warn('Log scale domain includes zero: ' + stringValue(domain));
    }
  }
  return domain;
}
github vega / vega / packages / vega-functions / src / scale-gradient.js View on Github external
export default function(scale, p0, p1, count, group) {
  scale = getScale(scale, (group || this).context);

  const gradient = Gradient(p0, p1);

  let stops = scale.domain(),
      min = stops[0],
      max = peek(stops),
      fraction = identity

  if (!(max - min)) {
    // expand scale if domain has zero span, fix #1479
    scale = (scale.interpolator
      ? get('sequential')().interpolator(scale.interpolator())
      : get('linear')().interpolate(scale.interpolate()).range(scale.range())
    ).domain([min=0, max=1]);
  } else {
    fraction = scaleFraction(scale, min, max);
  }

  if (scale.ticks) {
    stops = scale.ticks(+count || 15);
    if (min !== stops[0]) stops.unshift(min);
    if (max !== peek(stops)) stops.push(max);
  }

  stops.forEach(_ => gradient.stop(fraction(_), scale(_)));

  return gradient;
}
github vega / vega / packages / vega-functions / src / scale-gradient.js View on Github external
export default function(scale, p0, p1, count, group) {
  scale = getScale(scale, (group || this).context);

  const gradient = Gradient(p0, p1);

  let stops = scale.domain(),
      min = stops[0],
      max = peek(stops),
      fraction = identity

  if (!(max - min)) {
    // expand scale if domain has zero span, fix #1479
    scale = (scale.interpolator
      ? get('sequential')().interpolator(scale.interpolator())
      : get('linear')().interpolate(scale.interpolate()).range(scale.range())
    ).domain([min=0, max=1]);
  } else {
    fraction = scaleFraction(scale, min, max);
  }

  if (scale.ticks) {
    stops = scale.ticks(+count || 15);
    if (min !== stops[0]) stops.unshift(min);
    if (max !== peek(stops)) stops.push(max);
  }

  stops.forEach(_ => gradient.stop(fraction(_), scale(_)));

  return gradient;
}
github vega / vega / packages / vega-wordcloud / src / Wordcloud.js View on Github external
if (!(mod || pulse.changed(pulse.ADD_REM) || Params.some(modp))) return;

  var data = pulse.materialize(pulse.SOURCE).source,
      layout = this.value,
      as = _.as || Output,
      fontSize = _.fontSize || 14,
      range;

  isFunction(fontSize)
    ? (range = _.fontSizeRange)
    : (fontSize = constant(fontSize));

  // create font size scaling function as needed
  if (range) {
    var fsize = fontSize,
        sizeScale = scale('sqrt')()
          .domain(extent(fsize, data))
          .range(range);
    fontSize = function(x) { return sizeScale(fsize(x)); };
  }

  data.forEach(function(t) {
    t[as[0]] = NaN;
    t[as[1]] = NaN;
    t[as[3]] = 0;
  });

  // configure layout
  var words = layout
    .words(data)
    .text(_.text)
    .size(_.size || [500, 500])
github vega / vega / packages / vega-encode / src / Scale.js View on Github external
prototype.transform = function(_, pulse) {
  var df = pulse.dataflow,
      scale = this.value,
      key = scaleKey(_);

  if (!scale || key !== scale.type) {
    this.value = scale = getScale(key)();
  }

  for (key in _) if (!SKIP[key]) {
    // padding is a scale property for band/point but not others
    if (key === 'padding' && includePad(scale.type)) continue;
    // invoke scale property setter, raise warning if not found
    isFunction(scale[key])
      ? scale[key](_[key])
      : df.warn('Unsupported scale property: ' + key);
  }

  configureRange(scale, _,
    configureBins(scale, _, configureDomain(scale, _, df))
  );

  return pulse.fork(pulse.NO_SOURCE | pulse.NO_FIELDS);