How to useChart.js library in JavaScript
Chart.js provides a straightforward way to create radar charts using its radar chart type. Let’s see how to create a radar chart using Chart.js.
Chart.js CDN link:
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
Example: The below code implements the Chart.js library to create radar chart on the web app.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>
The Radar Chart
</title>
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
</head>
<body>
<canvas id="radarChart"></canvas>
<script>
let ctx = document.
getElementById('radarChart').
getContext('2d');
let myChart = new Chart(ctx, {
type: 'radar',
data: {
labels:
['A', 'B', 'C', 'D', 'E'],
datasets: [{
label: 'Data Set 1',
data: [12, 19, 3, 5, 2],
backgroundColor:
'rgba(255, 99, 132, 0.2)',
borderColor:
'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
},
options: {
scales: {
r: {
angleLines: {
display: false
},
suggestedMin: 0,
suggestedMax: 20
}
}
}
});
</script>
</body>
</html>
Output:
Example 2: The below code creates an advanced radar chart on the web page using chart.js.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>
The Radar Chart Example
</title>
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
</head>
<body>
<canvas id="radarChart" width="400"
height="400">
</canvas>
<script>
const ctx = document.
getElementById('radarChart').
getContext('2d');
const myRadarChart = new Chart(ctx, {
type: 'radar',
data: {
labels:
['Category 1', 'Category 2', 'Category 3',
'Category 4', 'Category 5'],
datasets: [{
label: 'Dataset 1',
data:
[10, 20, 15, 25, 30],
backgroundColor:
'rgba(255, 99, 132, 0.2)',
borderColor:
'rgba(255, 99, 132, 1)',
borderWidth: 1
}, {
label: 'Dataset 2',
data:
[15, 25, 20, 30, 35],
backgroundColor:
'rgba(54, 162, 235, 0.2)',
borderColor:
'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
scale: {
ticks: {
beginAtZero: true
}
}
}
});
</script>
</body>
</html>
Output:
How To Draw Radar Charts In Web?
The Radar charts consist of a series of data points that are plotted on radial axes emanating from the central point. Each axis represents a different variable and data points that are connected to form a polygon providing the visual representation of the data distribution.