VictoryAxis
VictoryAxis draws an SVG chart axis with React. Styles and data can be customized by passing in your own values as properties to the component. Data changes are animated with victory-animation.
Features
Props are Optional
VictoryAxis is written to be highly configurable, but it also includes a set of sensible defaults and fallbacks. If no properties are provided, the following default axis is rendered:
<VictoryAxis/>
To make a vertical axis, set the dependentAxis prop to true. Dependent axes are automatically oriented on the left. You can also manually specify an orientation prop i.e. orientation="right". The default dependent axis is shown below.
<VictoryAxis dependentAxis/>
Declarative Composition
Axes are meant to be composable.
<svg width={450} height={450}>
<VictoryAxis
padding={75}
label="x-axis"
standalone={false}/>
<VictoryAxis dependentAxis
padding={75}
label="y-axis"
standalone={false}/>
</svg>
And can be made to cross each other by setting offsetX, offsetY, and crossAxis props as shown below. Getting crossed axes to look correct requires calculating the appropriate offsets. This is handled automatically in VictoryChart.
<svg width={400} height={400}>
<VictoryAxis
width={400}
height={400}
domain={[-10, 10]}
crossAxis={true}
offsetY={200}
standalone={false}/>
<VictoryAxis dependentAxis
width={400}
height={400}
domain={[-10, 10]}
crossAxis={true}
offsetX={200}
standalone={false}/>
</svg>
Flexible and Configurable
The sensible defaults VictoryAxis provides make it easy to get started, but everything can be overridden, and configured to suit your needs:
<VictoryAxis
style={{
axis: {stroke: "black"},
grid: {strokeWidth: 2},
ticks: {stroke: "red"},
tickLabels: {fontSize: 12},
axisLabels: {fontsize: 16}
}}
label="Planets"
tickValues={[
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter"
]}/>
Non-linear scales
With a little more code, you can make a time scale axis with custom tick values and formatting.
<VictoryAxis
scale="time"
tickValues={[
new Date(1980, 1, 1),
new Date(1990, 1, 1),
new Date(2000, 1, 1),
new Date(2010, 1, 1),
new Date(2020, 1, 1)]}
tickFormat={(x) => x.getFullYear()}/>
Here's how you make a log scale:
<VictoryAxis
dependentAxis
padding={{left: 50, top: 20, bottom: 20}}
scale="log"
domain={[1, 5]}
/>
Animating
VictoryAxis animates with VictoryAnimation as props change when an animate prop is provided.
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
tickValues: [5, 10, 25, 31, 42],
style: this.getStyles()
};
}
getTickValues() {
return _.map(_.range(5), (i) => {
return 10 * i + _.random(5);
});
}
getStyles() {
const n = _.random(0, 1);
const tickColors = ["black", "red"];
return {
axis: {stroke: "black"},
grid: {strokeWidth: n},
ticks: {stroke: tickColors[n]},
tickLabels: {fill: tickColors[n]}
};
}
componentDidMount() {
setInterval(() => {
this.setState({
tickValues: this.getTickValues(),
style: this.getStyles()
});
}, 3000);
}
render() {
return (
<VictoryAxis
animate={{velocity: 0.01}}
style={this.state.style}
tickValues={this.state.tickValues}
tickFormat={[
"Mets\nNY",
"Giants\nSF",
"Yankees\nNY",
"Nationals\nDC",
"Mariners\nSEA"
]}/>
);
}
}
ReactDOM.render(<App/>, mountNode);
Props
| Props | Description |
|---|---|
| animateobject | The animate prop specifies props for victory-animation to use. It this prop is not given, the axis will not tween between changing data / style props. Large datasets might animate slowly due to the inherent limits of svg rendering. Examples: {velocity: 0.02, onEnd: () => alert("done!")} |
| crossAxisbool | This prop specifies whether a given axis is intended to cross another axis. |
| dependentAxisbool | The dependentAxis prop specifies whether the axis corresponds to the dependent variable (usually y). This prop is useful when composing axis with other components to form a chart. |
| domaincustom | The domain prop describes the range of values your axis will include. This prop should be given as a array of the minimum and maximum expected values for your axis. If this value is not given it will be calculated based on the scale or tickValues. Examples: [-1, 1] |
| heightcustom | The height prop specifies the height of the chart container element in pixels.Default Value: 300 |
| labelany | The label prop specifies the label for your axis. This prop can be a string or a label component. |
| labelPaddingnumber | The labelPadding prop specifies the padding in pixels for your axis label. |
| offsetXnumber | This value describes how far from the "edge" of its permitted area each axis will be set back in the x-direction. If this prop is not given, the offset is calculated based on font size, axis orientation, and label padding. |
| offsetYnumber | This value describes how far from the "edge" of its permitted area each axis will be set back in the y-direction. If this prop is not given, the offset is calculated based on font size, axis orientation, and label padding. |
| orientation"top", "bottom", "left", "right" | The orientation prop specifies the position and orientation of your axis. |
| paddingnumber, shape | The padding props specifies the amount of padding in number of pixels between the edge of the chart and any rendered child components. This prop can be given as a number or as an object with padding specified for top, bottom, left and right.Default Value: 50 |
| scalecustom | The scale prop determines which scales your axis should use. This prop can be given as a `d3-scale@0.3.0` function or as a string corresponding to a supported d3-string function. Examples: d3Scale.time(), "linear", "time", "log", "sqrt"Default Value: "linear" |
| standalonebool | The standalone prop determines whether the component will render a standalone svg or a <g> tag that will be included in an external svg. Set standalone to false to compose VictoryAxis with other components within an enclosing <svg> tag.Default Value: true |
| style{parent: object, axis: object, axisLabel: object, grid: object, ticks: object, tickLabels: object} | The style prop specifies styles for your chart. Victory Axis relies on Radium, so valid Radium style objects should work for this prop, however height, width, and margin are used to calculate range, and need to be expressed as a number of pixels. Styles for axis lines, gridlines, and ticks are scoped to separate props. Examples: {axis: {stroke: "#756f6a"}, grid: {stroke: "grey"}, ticks: {stroke: "grey"}, tickLabels: {fontSize: 10, padding: 5}, axisLabel: {fontSize: 16, padding: 20}} |
| tickCountcustom | The tickCount prop specifies how many ticks should be drawn on the axis if tickValues are not explicitly provided.Default Value: 5 |
| tickFormatfunc, custom | The tickFormat prop specifies how tick values should be expressed visually. tickFormat can be given as a function to be applied to every tickValue, or as an array of display values for each tickValue. Examples: d3.time.format("%Y"), (x) => x.toPrecision(2), ["first", "second", "third"] |
| tickValuescustom | The tickValues prop explicitly specifies which tick values to draw on the axis. Examples: ["apples", "bananas", "oranges"], [2, 4, 6, 8] |
| widthcustom | The width props specifies the width of the chart container element in pixelsDefault Value: 450 |