scale icon indicating copy to clipboard operation
scale copied to clipboard

Add Diverging Scale

Open pepper-nice opened this issue 3 years ago • 0 comments

Diverging Scale

Diverging scale is like sequential scale, which expects it has three elements to describe visual domain by interpolator. Their domain includes three values: two extremes and a central point. They also do not expose the invert method and interpolate options.

For every value to be scaled, there are two steps in short:

  • Normalize: Map x from [min, center, max] to [0, 0.5, 1].
  • Interpolate: Apply interpolator to the normalized value.

Basic Usage

import { Diverging } from '@antv/scale';

const scale = new Sequential({
  domain: [-10, 0, 10],
  interpolator: t => 1-t
});

scale.map(5); // 0.25
scale.map(-5); // 0.75
scale.map(2); // 0.4
scale.getOptions().range; // [1, 0.5, 0]

API Design

  • scale.map(x)
  • scale.getTicks()
  • scale.update(options)
  • scale.getOptions()
  • scale.nice()
  • scale.clone()

Implement

Like Sequential Scale, use decoration instead of inheritance to enhance linear scale.

import { Linear } from './Linear';

function Diverginglish(Scale){
  return class Diverging extends Scale {
    map(){}
    rescale(){}
    invert = undefined;
  }
};

export const Diverging = Diverginglish(Scale);

Reference

pepper-nice avatar Aug 29 '22 02:08 pepper-nice