What is @antv/scale?
@antv/scale is a JavaScript library for creating and managing scales, which are functions that map a domain of data values to a range of visual values. It is commonly used in data visualization to ensure that data is represented accurately and proportionally.
What are @antv/scale's main functionalities?
Linear Scale
Linear scales map a continuous domain to a continuous range. This is useful for data that is evenly distributed.
const { Linear } = require('@antv/scale');
const scale = new Linear({
domain: [0, 100],
range: [0, 1]
});
console.log(scale.map(50)); // 0.5
Ordinal Scale
Ordinal scales map a discrete domain to a discrete range. This is useful for categorical data.
const { Ordinal } = require('@antv/scale');
const scale = new Ordinal({
domain: ['A', 'B', 'C'],
range: [0, 1, 2]
});
console.log(scale.map('B')); // 1
Time Scale
Time scales map a time domain to a continuous range. This is useful for time-series data.
const { Time } = require('@antv/scale');
const scale = new Time({
domain: [new Date(2020, 0, 1), new Date(2020, 11, 31)],
range: [0, 1]
});
console.log(scale.map(new Date(2020, 5, 15))); // 0.5
Log Scale
Log scales map a logarithmic domain to a continuous range. This is useful for data that spans several orders of magnitude.
const { Log } = require('@antv/scale');
const scale = new Log({
domain: [1, 100],
range: [0, 1]
});
console.log(scale.map(10)); // 0.5
Other packages similar to @antv/scale
d3-scale
d3-scale is a part of the D3.js library and provides similar functionality for creating scales. It supports a wide range of scale types including linear, logarithmic, ordinal, and time scales. Compared to @antv/scale, d3-scale is more widely used and has a larger community, but @antv/scale may offer better integration with other AntV libraries.
vega-scale
vega-scale is part of the Vega visualization grammar and provides a variety of scale types for mapping data to visual values. It is similar to @antv/scale in terms of functionality but is designed to work within the Vega ecosystem. It offers robust support for different scale types and is well-documented.
@antv/scale
✨ Features
- Powerful: Ability to customize tickMethod are offered with abundant kinds of scales.
- High performance: Use different methods to cache some state of scales to improve performance.
- Fully embrace TypeScript: All code are written in TypeScript and complete type definition files are provided.
📦 Installation
$ npm install @antv/scale
🔨 Getting Started
import { Linear, LinearOptions } from '@antv/scale';
const options: LinearOptions = {
domain: [0, 10],
range: [0, 100],
};
const x = new Linear(options);
x.map(2);
x.invert(20);
x.getTicks();
import { Linear } from '@antv/scale';
const x = new Linear({
domain: [0, 10],
range: [0, 100],
tickCount: 3,
tickMethod: () => [0, 5, 10],
});
x.getTicks();
📎 Links
📮 Contribution
$ git clone git@github.com:antvis/scale.git
$ cd scale
$ npm i
$ npm t
Then send a pull request after coding.
📄 License
MIT@AntV.