@vx/scale
npm install --save @vx/scale
Overview of Scaling
The @vx/scale
package aims to provide a wrapper around existing d3 scaling originally defined in the d3-scale package.
Scales are functions that help you map your data to the physical pixel size that your graph requires. For example, let's say you wanted to create a bar chart to show populations per country. If you were to use a 1-to-1 scale (IE: 1 pixel per y value) your bar for the USA would be about 321.4 million pixels high!
Instead, you can tell vx a function to use that takes a value (like your population per country) and spits out another value.
For example, we could create a linear scale like this:
const graphHeight = 500;
const maxPopulation = getMostPopulatedCountryInTheWorld();
const yScale = Scale.scaleLinear({
rangeRound: [graphHeight, 0],
domain: [0, maxPopulation],
});
const bars = data.map((d, i) => {
const barHeight = graphHeight - yScale(d.y);
return <Shape.Bar height={barHeight} y={graphHeight - barHeight} />
})
Note: This example represents how to use a yScale, but skipped a lot of details about how to make a bar chart. If you're trying to do that, you should check out this example.
Current Scaling Options
Band Scaling
Original d3 docs
Example:
const scale = Scale.scaleBand({
});
Linear Scaling
Original d3 docs
Example:
const scale = Scale.scaleLinear({
});
Log Scaling
Original d3 docs
Example:
const scale = Scale.scaleLog({
});
Ordinal Scaling
Original d3 docs
Example:
const scale = Scale.scaleOrdinal({
});
Point Scaling
Original d3 docs
Example:
const scale = Scale.scalePoint({
});
Power Scaling
Original d3 docs
Example:
const scale = Scale.scalePower({
});
Square Root Scaling
Original d3 docs
Example:
const scale = Scale.scaleSqrt({
});
Time Scaling
Original d3 docs
Example:
const scale = Scale.scaleTime({
});
You also can scale time with Coordinated Universal Time via scaleUtc
.
Example:
const scale = Scale.scaleUtc({
});
Color Scales
D3 scales offer the ability to map points to colors. You can use d3-scale-chromatic
in conjunction with vx's scaleOrdinal
to make color scales.
You can install d3-scale-chromatic
with npm:
npm install --save d3-scale-chromatic
You create a color scale like so:
import { scaleOrdinal } from '@vx/scale';
import { schemeSet1 } from 'd3-scale-chromatic';
const colorScale = scaleOrdinal({
domain: arrayOfThings,
range: schemeSet1
});
This generates a color scale with the following colors:
There are a number of other categorical color schemes available, along with other continuous color schemes.
v0.0.196
:trophy: Contributors
:rocket: Enhancements
- [brush] Add initialBrushPosition #618, fix in #667
💥 Breaking Changes
- [tooltip] Add ability to remove tooltip default styles #666. If styles were applied previously, you will also need to spread
defaultStyles
:
// before
import { Tooltip } from '@vx/tooltip';
...
<Tooltip style={{ color: myCustomColor }} />
// after
import { Tooltip, defaultStyles } from '@vx/tooltip';
...
<Tooltip style={{ ...defaultStyles, color: myCustomColor }} />
:memo: Documentation
- [demo] Several demos refactored to link out to codesandbox
Changes:
- @vx/annotation: 0.0.195 => 0.0.196
- @vx/axis: 0.0.195 => 0.0.196
- @vx/bounds: 0.0.195 => 0.0.196
- @vx/brush: 0.0.195 => 0.0.196
- @vx/chord: 0.0.195 => 0.0.196
- @vx/clip-path: 0.0.195 => 0.0.196
- @vx/curve: 0.0.195 => 0.0.196
- @vx/demo: 0.0.195 => 0.0.196
- @vx/drag: 0.0.195 => 0.0.196
- @vx/event: 0.0.195 => 0.0.196
- @vx/geo: 0.0.195 => 0.0.196
- @vx/glyph: 0.0.195 => 0.0.196
- @vx/gradient: 0.0.195 => 0.0.196
- @vx/grid: 0.0.195 => 0.0.196
- @vx/group: 0.0.195 => 0.0.196
- @vx/heatmap: 0.0.195 => 0.0.196
- @vx/hierarchy: 0.0.195 => 0.0.196
- @vx/legend: 0.0.195 => 0.0.196
- @vx/marker: 0.0.195 => 0.0.196
- @vx/mock-data: 0.0.195 => 0.0.196
- @vx/network: 0.0.195 => 0.0.196
- @vx/pattern: 0.0.195 => 0.0.196
- @vx/point: 0.0.195 => 0.0.196
- @vx/responsive: 0.0.195 => 0.0.196
- @vx/scale: 0.0.195 => 0.0.196
- @vx/shape: 0.0.195 => 0.0.196
- @vx/stats: 0.0.195 => 0.0.196
- @vx/text: 0.0.195 => 0.0.196
- @vx/threshold: 0.0.195 => 0.0.196
- @vx/tooltip: 0.0.195 => 0.0.196
- @vx/voronoi: 0.0.195 => 0.0.196
- @vx/vx: 0.0.195 => 0.0.196
- @vx/zoom: 0.0.195 => 0.0.196