The Victory lets us add charts and data visualization into our React app.
In this article, we’ll look at how to add charts into our React app with Victory.
Responsive VictoryContainer
We can set the responsive
prop of the VictoryContainer
component to render a responsive chart:
import React from "react";
import { VictoryChart, VictoryContainer, VictoryLine } from "victory";
export default function App() {
return (
<div>
<VictoryChart
height={200}
width={300}
containerComponent={<VictoryContainer responsive />}
>
<VictoryLine y={(data) => Math.sin(2 * Math.PI * data.x)} />
</VictoryChart>
</div>
);
}
Rendering Components in Custom Containers
We can render components in custom containers.
For instance, we can write:
import React from "react";
import { VictoryLabel, VictoryPie } from "victory";
export default function App() {
return (
<svg viewBox="0 0 400 400">
<VictoryPie
standalone={false}
width={400}
height={400}
data={[
{ x: "A", y: 10 },
{ x: "B", y: 20 },
{ x: "C", y: 80 }
]}
innerRadius={70}
labelRadius={100}
style={{ labels: { fontSize: 20, fill: "white" } }}
/>
<circle
cx="200"
cy="200"
r="65"
fill="none"
stroke="black"
strokeWidth={3}
/>
<circle
cx="200"
cy="200"
r="155"
fill="none"
stroke="black"
strokeWidth={3}
/>
<VictoryLabel
textAnchor="middle"
verticalAnchor="middle"
x={200}
y={200}
style={{ fontSize: 30 }}
text="Label"
/>
</svg>
);
}
to render the pie chart in the svg
element.
This is because the chart components are rendered as svgs.
Polar Charts
We can add polar charts into our React app.
For instance, we can write:
import React from "react";
import {
VictoryAxis,
VictoryBar,
VictoryChart,
VictoryPolarAxis,
VictoryTheme
} from "victory";
const data = [
{ x: 1, y: 3 },
{ x: 2, y: 4 },
{ x: 3, y: 2 }
];
export default function App() {
return (
<div>
<VictoryChart polar domain={{ x: [0, 10] }} theme={VictoryTheme.material}>
<VictoryPolarAxis tickCount={8} />
<VictoryBar
data={data}
style={{ data: { fill: "#c43a31", stroke: "black", strokeWidth: 2 } }}
/>
</VictoryChart>
</div>
);
}
to add a polar chart with the VictoryChart
component.
VictoryAxis
has the axes for the chart.
VictoryBar
are rendered in the chart as segments.
We can add the VictoryPolarAxis
component to add the tick lines to the polar chart:
import React from "react";
import {
VictoryBar,
VictoryChart,
VictoryPolarAxis,
VictoryTheme
} from "victory";
const data = [
{ x: 1, y: 3 },
{ x: 2, y: 4 },
{ x: 3, y: 2 }
];
export default function App() {
return (
<div>
<VictoryChart polar theme={VictoryTheme.material}>
<VictoryPolarAxis
dependentAxis
style={{
axis: { stroke: "none" },
tickLabels: { fill: "none" },
grid: { stroke: "grey", strokeDasharray: "4, 8" }
}}
/>
<VictoryPolarAxis tickValues={[0, 3, 6, 9]} />
<VictoryBar
style={{ data: { fill: "#c43a31", width: 50 } }}
data={data}
/>
</VictoryChart>
</div>
);
}
We can add multiple segments into the polar chart with the VictoryBar
components:
import React from "react";
import {
VictoryBar,
VictoryChart,
VictoryPolarAxis,
VictoryStack,
VictoryTheme
} from "victory";
const data = [
{ x: 1, y: 3 },
{ x: 2, y: 4 },
{ x: 3, y: 2 }
];
export default function App() {
return (
<div>
<VictoryChart
polar
maxDomain={{ x: 360 }}
height={250}
width={250}
padding={30}
>
<VictoryPolarAxis
dependentAxis
style={{
axis: { stroke: "none" },
tickLabels: { fill: "none" },
grid: { stroke: "grey", strokeDasharray: "4, 8" }
}}
/>
<VictoryPolarAxis tickValues={[0, 45, 90, 135, 180, 225, 270, 315]} />
<VictoryStack
colorScale={["#ad1b11", "#c43a31", "#dc7a6b"]}
style={{ data: { width: 50 } }}
>
<VictoryBar data={data} />
<VictoryBar data={data} />
<VictoryBar data={data} />
</VictoryStack>
</VictoryChart>
</div>
);
}
Conclusion
We can add various kinds of polar charts with React Victory.