iTranslated by AI
Creating Charts with Recharts in React
Prerequisites
We will create chart graphs in React.
The library we will use is Recharts.
In this article, we will focus on creating a pie chart.
Introducing the Library
Install recharts.
$ npm install recharts
Once installed, let's import the methods necessary for creating a pie chart.
import { PieChart, Pie, Text } from 'recharts'
Creating a Pie Chart
A graph can be created with minimal effort as long as you have data.
Prepare the data as an array of objects. We'll also include an index here.
const data = [
{
index: 0,
name: 'Data 1',
value: 300,
},
{
index: 1,
name: 'Data 2',
value: 200,
},
{
index: 2,
name: 'Data 3',
value: 380,
},
{
index: 3,
name: 'Data 3',
value: 80,
},
{
index: 4,
name: 'Data 4',
value: 40,
}
]
All that's left is to pass the data to the methods for the pie chart.
<PieChart width={730} height={250}>
<Pie data={data} dataKey="value" cx="50%" cy="50%" outerRadius={100} fill="#82ca9d" label/>
</PieChart>
The basic graph creation is now complete.
Since this is not very practical for actual operations, let's update it with label displays and other features.
About <Pie> Parameters
data
The graph data.
cx, cy
These refer to the position relative to the graph display area secured by <PieChart>.
If
cx="100", the graph is generated with the right edge of the display area as the center of the pie chart.
outerRadius
The outer radius of the selector.
By setting it along with innerRadius, you can also create a donut-shaped pie chart.
fill
Specify a color code to set the color of the graph.
Customizing Label Display
Use <PieChart> to secure the display size of the graph.
Adding Information to Labels
Since we already have name in the data, let's display name on the labels.
The label property can also accept element elements, so we will create a label element that displays both name and value.
const label = ({ name, value, cx, x, y }) => {
return (
<>
{/* You can receive auxiliary information via arguments */}
<Text x={x} y={y} fill="#82ca9d">{name}</Text>
<Text x={x} y={y} dominantBaseline="hanging" fill="#82ca9d">{value}</Text>
</>
)
}
...
<PieChart width={730} height={250}>
<Pie data={data} dataKey="value" cx="50%" cy="50%" outerRadius={100} fill="#82ca9d" label={label}/>
</PieChart>
Simply lining up the <Text> components for the label will cause the text to overlap, so we use the dominantBaseline="hanging" property to place one set of information at the bottom vertically.
However, with this alone, the label display will always be placed from left to right, so we need to display the text from right to left depending on the coordinates.
Changing the display based on position information
The cx received by the label function is the x-coordinate of the center of the graph.
In other words, if the x-coordinate of the label is greater than the x-coordinate of the center of the graph (x > cx), the label is positioned to the right of the graph.
We will prepare a textAnchor variable to specify the rendering position.
const textAnchor = x > cx ? "start" : "end";
Then, simply pass this value to the textAnchor property of the <Text> component.
return (
<>
<Text x={x} y={y} textAnchor={textAnchor} fill="#82ca9d">
{name}
</Text>
<Text
x={x}
y={y}
dominantBaseline="hanging"
textAnchor={textAnchor}
fill="#82ca9d"
>
{value}
</Text>
</>
);
};
Conclusion
Were you able to create a graph like the one below?

In fact, if adjacent data points are both small, the labels may overlap.
Solving this requires a rather mathematical approach, but I'll introduce it next time if there's interest.
(You can arrange them nicely using things like sine, cosine, and tangent.)
Recharts is compatible with React and is a very easy-to-use chart library.
Of course, it also supports other charts like bar charts and line charts, so it can be used for various purposes.
However, I feel that there are few cases where it can be used with no customization at all.
I hope there are easy solutions by coming up with some ideas, but there may be situations where you have to resolve issues through very precise calculations. (That's where you show your skills!)
If I find a chart library that is easy to use, including this kind of customizability, I would like to introduce it again.
I'm looking forward to hearing comments like "There's also this one!"
Discussion