Recharts — Treemap and Custom Tooltip

Image for post
Image for post
Photo by Mika Baumeister on Unsplash

We can add charts easily to a React app with Recharts.

In this article, we’ll look at how to use it.

Treemap

We can add a treemap with the Treemap component.

For instance, we can write:

import React from "react";
import { Treemap } from "recharts";
const data = [
{
name: "axis",
children: [
{ name: "Axes", size: 1302 },
{ name: "Axis", size: 24593 }
]
},
{
name: "controls",
children: [
{ name: "AnchorControl", size: 2138 },
{ name: "ClickControl", size: 3824 }
]
},
{
name: "data",
children: [
{ name: "Data", size: 20544 },
{ name: "DataList", size: 19788 },
{
name: "render",
children: [
{ name: "ArrowType", size: 698 },
{ name: "EdgeRenderer", size: 5569 }
]
},
{ name: "ScaleBinding", size: 11275 },
{ name: "Tree", size: 7147 },
{ name: "TreeBuilder", size: 9930 }
]
},
{
name: "operator",
children: [
{
name: "distortion",
children: [
{ name: "BifocalDistortion", size: 4461 },
{ name: "Distortion", size: 6314 }
]
},
{ name: "IOperator", size: 1286 },
{ name: "Operator", size: 2490 },
{ name: "OperatorList", size: 5248 }
]
}
];
export default function App() {
return (
<div className="App">
<Treemap
width={400}
height={200}
data={data}
dataKey="size"
ratio={4 / 3}
stroke="#fff"
fill="#8884d8"
/>
</div>
);
}

to add the data for the treemap and render it.

data has an array with objects that have the name and children properties.

The children objects have the size s we want to display.

width and height sets the dimensions of the treemap.

ratio has the aspect radio for the treemap.

stroke has the line color.

fill has the background color.

dataKey has the property name with the values we want to render.

Tooltip

We can customize tooltips to display the data we want.

For example, we can write:

import React from "react";
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend
} from "recharts";
const data = [
{
name: "Page A",
uv: 4000,
pv: 2400,
amt: 2400
},
{
name: "Page B",
uv: 3000,
pv: 1398,
amt: 2210
},
{
name: "Page C",
uv: 2000,
pv: 9800,
amt: 2290
}
];
const getIntroOfPage = (label) => {
if (label === "Page A") {
return "Page A is about men's clothing";
}
if (label === "Page B") {
return "Page B is about women's dress";
}
if (label === "Page C") {
return "Page C is about women's bag";
}
};
const CustomTooltip = ({ active, payload, label }) => {
if (active) {
return (
<div className="custom-tooltip">
<p className="label">{`${label} : ${payload[0].value}`}</p>
<p className="intro">{getIntroOfPage(label)}</p>
</div>
);
}
return null;
};
export default function App() {
return (
<div className="App">
<BarChart
width={500}
height={300}
data={data}
margin={{
top: 5,
right: 30,
left: 20,
bottom: 5
}}
>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="name" />
<YAxis />
<Tooltip content={<CustomTooltip />} />
<Legend />
<Bar dataKey="pv" barSize={20} fill="#8884d8" />
</BarChart>
</div>
);
}

We have the CustomTooltip component to render the tooltip the way we want.

We pass that into the content prop so that we can render what we want.

Conclusion

We can add a treemap and customize labels with the Recharts.

Written by

Web developer. Subscribe to my email list now at http://jauyeung.net/subscribe/. Email me at hohanga@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store