Skip to main content

Radar Chart (Spider Chart)

The Radar Chart, also known as a Spider Chart, is a graphical method of displaying multivariate data in the form of a two-dimensional chart of three or more quantitative variables represented on axes starting from the same point.

Installation

The RadarChart component is part of the @rexora/charts package. You can install it using npm or yarn:

bash
npm install @rexora/charts
# or
yarn add @rexora/charts

Usage

Here's a basic example of how to use the RadarChart component:

tsx
import React from 'react';
import { RadarChart } from '@rexora/charts';

const data = [
{ skill: 'Strength', level: 80 },
{ skill: 'Dexterity', level: 60 },
{ skill: 'Constitution', level: 75 },
{ skill: 'Intelligence', level: 90 },
{ skill: 'Wisdom', level: 70 },
{ skill: 'Charisma', level: 85 },
];

const MyRadarChart = () => {
return (
<div>
<h2>Character Skills</h2>
<RadarChart
data={data}
width={400}
height={400}
options={{
margin: { top: 40, right: 40, bottom: 40, left: 40 },
levels: 5,
maxValue: 100,
labelFactor: 1.2,
wrapWidth: 60,
opacityArea: 0.35,
dotRadius: 4,
opacityCircles: 0.1,
strokeWidth: 2,
roundStrokes: true,
color: '#FF0000', // Example color
}}
/>
</div>
);
};

export default MyRadarChart;

Props and Options

The RadarChart component accepts the following props:

PropTypeDescriptionRequired
dataArrayThe data array for the chart. Each object should have properties corresponding to your skill/variable and level/value.Yes
widthnumberThe width of the chart SVG.Yes
heightnumberThe height of the chart SVG.Yes
optionsobjectConfiguration options for the chart.No

The options prop is an object that can contain the following properties:

OptionTypeDescriptionDefault
marginobjectMargins around the chart (top, right, bottom, left).{ top: 20, right: 20, bottom: 20, left: 20 }
levelsnumberThe number of levels to draw on the radar chart.5
maxValuenumberThe maximum value for the radar axes. Data values will be scaled to this.Determined from data if not provided
labelFactornumberHow much farther than the radius the labels should be.1.25
wrapWidthnumberThe width in pixels to wrap the axis labels.60
opacityAreanumberThe opacity of the colored area.0.35
dotRadiusnumberThe radius of the dots on the radar points.4
opacityCirclesnumberThe opacity of the circles on the grid.0.1
strokeWidthnumberThe width of the web lines.2
roundStrokesbooleanWhether to have round or flat line endings.false
colorstringThe color of the radar area and lines.Category20 D3 color scale

Notes

  • The data array should be structured such that each object represents a point on the radar chart axes. You'll need to adapt the accessors within the D3 rendering logic to match your data structure.
  • The maxValue option is crucial for scaling your data correctly on the radar axes.
  • Customization of colors, tooltips, and interactions will require adding more D3 logic within the component.