# Getting Started With Chart.js: Radar and Polar Area Charts

This post is part of a series called Getting Started With Chart.js.
Getting Started With Chart.js: Line and Bar Charts
Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts

The previous tutorial of this series focused on creating line and bar charts using Chart.js. Both these charts have their own uses and configuration options that were covered in detail in the last tutorial.

In this tutorial, you will learn about two new chart types that can be created using Chart.js: radar and polar area charts. Just like the previous tutorial, we will start with a brief overview of the chart types and then move to a more detailed discussion.

Line and bar charts are useful when you want to compare only one or two properties of a large number of objects—for example, the population of all the countries in Asia or the number of different pollutants in the atmosphere.

Let's say you want to compare the density, opacity, viscosity, refractive index, and boiling point of only three different liquids. Creating a bar chart for this data will be problematic as you will need to create five different columns for each liquid. It will also be hard to compare the corresponding properties of the liquids.

In situations where you have to compare a lot of properties of only a few objects, creating a radar chart is the most efficient method of visualizing and comparing data. These charts are also known as spider charts.

From Wikipedia, a radar 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. The relative positions and angles of the axes are typically uninformative.

Let's create our first radar chart now. Radar charts are created by setting the type key in Chart.js to radar. Here is a very basic example.

 1 var radarChart = new Chart(marksCanvas, {  2  type: 'radar',  3  data: marksData,  4  options: chartOptions  5 }); 

Let's plot the marks of two students of a class in five different subjects. Here is the code to provide the data for creating the chart.

 1 var marksData = {  2  labels: ["English", "Maths", "Physics", "Chemistry", "Biology", "History"],  3  datasets: [  4  {  5  label: "Martha",  6  backgroundColor: "#FFF17644",  7  borderColor: "black",  8  borderWidth: 1,  9  data: [65, 75, 70, 80, 60, 80]  10  },  11  {  12  label: "Steven",  13  backgroundColor: "#8E24AA44",  14  borderColor: "black",  15  borderWidth: 1,  16  data: [54, 65, 60, 70, 90, 75]  17  }  18  ]  19 };  20 21 var chartOptions = {  22  plugins: {  23  title: {  24  display: true,  25  align: "start",  26  text: "Comparing Student Performance"  27  },  28  legend: {  29  align: "start"  30  }  31  },  32  scales: {  33  r: {  34  pointLabels: {  35  font: {  36  size: 20  37  }  38  }  39  }  40  }  41 };  42 43 var radarChart = new Chart(marksCanvas, {  44  type: "radar",  45  data: marksData,  46  options: chartOptions  47 }); 

Radar charts can have a lot of overlap, making it difficult to correctly identify the data points without any color coding. For this reason, we have assigned a color to each dataset using the backgroundColor key. Besides the background color, you can also change the border color and border width for the chart using the borderColor and borderWidth keys.

You might have also noticed that we have used r as our scale key inside the chartOptions object instead of x or y as we do in line or bar charts. This is because these types of charts have a radial axis. The following demo shows the final result of our code. As you can see, it is now very easy to compare the performance of both students in different subjects.

It is also possible for you to create dashed borders instead of continuous lines using the borderDash key. This key accepts an array as its value. The first element of the array determines the length of the dashes, and the second element determines the space between them. You can also provide an offset value for drawing the dashes using the borderDashOffset key.

You can also control the border color and width for plotted points using the pointBorderColor and pointBorderWidth keys. Similarly, you can also set a background color for different points using the pointBackgroundColor key. The size of the plotted points can be specified using the pointRadius key. You can control the distance at which the points should start interacting with the mouse using the pointHitRadius key.

You can change the appearance of the plotted points on hover using the pointHoverBackgroundColor, pointHoverBorderColor, and pointHoverBorderWidth keys. One thing that you need to remember is that these hover keys will not wait for you to actually hover over the element to get triggered. The changes will take effect as soon as you are inside the hit radius set above.

There are a lot of shapes available for plotted points. They are circular by default. However, you can change the shape to triangle, rect, rectRounded, rectRot, cross, crossRot, star, line, and dash.

Let's use all these keys to redraw the previous radar chart. Here is the code to provide configuration options for the datasets and the scales.

 1 var marksData = {  2  labels: ["English", "Maths", "Physics", "Chemistry", "Biology", "History"],  3  datasets: [{  4  label: "Martha",  5  backgroundColor: "transparent",  6  borderColor: "red",  7  fill: false,  8  pointRadius: 6,  9  pointHitRadius: 12,  10  pointBorderWidth: 3,  11  pointBackgroundColor: "pink",  12  pointBorderColor: "black",  13  pointHoverRadius: 10,  14  pointStyle: "triangle",  15  data: [65, 75, 70, 80, 60, 80]  16  }, {  17  label: "Steven",  18  backgroundColor: "transparent",  19  borderColor: "blue",  20  fill: false,  21  pointRadius: 6,  22  pointHitRadius: 12,  23  pointBorderWidth: 3,  24  pointBackgroundColor: "lightblue",  25  pointBorderColor: "black",  26  pointHoverRadius: 10,  27  pointStyle: "rect",  28  data: [54, 65, 60, 70, 70, 75]  29  },  30  {  31  label: "Harry",  32  backgroundColor: "transparent",  33  borderColor: "green",  34  fill: false,  35  pointRadius: 6,  36  pointHitRadius: 12,  37  pointBorderWidth: 3,  38  pointBackgroundColor: "lightgreen",  39  pointBorderColor: "black",  40  pointHoverRadius: 10,  41  pointStyle: "circle",  42  data: [84, 56, 75, 40, 90, 85]  43  }]  44 };  45 46 var chartOptions = {  47  plugins: {  48  title: {  49  display: true,  50  text: "Comparing Student Performance",  51  position: "bottom"  52  },  53  legend: {  54  position: "bottom",  55  labels: {  56  padding: 30  57  }  58  }  59  },  60  scales: {  61  r: {  62  max: 100,  63  min: 30,  64  ticks: {  65  stepSize: 15,  66  backdropColor: "orange",  67  color: "white"  68  },  69  grid: {  70  color: "black"  71  },  72  angleLines: {  73  color: "gray"  74  },  75  pointLabels: {  76  font: {  77  size: 20  78  }  79  }  80  }  81  }  82 }; 

Inside the chartOptions object, the min and max values are used to set the minimum and maximum values for the scale. The stepSize key can be used to tell Chart.js the number of steps that it should take to go from min to max. As you can see in the chart below, the values go from 90 to 100 with an increase of just 10. Here is the final result of the above code.

## Creating Polar Area Charts

Polar area charts are similar to pie charts. Two major differences between these charts are that in polar area charts all the sectors have equal angles, and the radius of each sector depends on the plotted value. These charts are used to plot cyclic phenomena. For example, you can use them to plot the number of migratory birds of a given species in your area in each season of the year.

The radius of each sector in these charts is proportional to the square root of the number of corresponding objects. In the case of migratory birds, the radius will be proportional to the square root of the number of birds in your area.

You can create polar area charts in Chart.js by setting the type key to polarArea. Here is the basic code that you need to create a polar chart.

 1 var polarAreaChart = new Chart(birdsCanvas, {  2  type: 'polarArea',  3  data: birdsData,  4  options: chartOptions  5 }); 

Here is the code to plot the number of migratory birds on a polar area chart. The only data provided at this point is the number of birds and the background color for different seasons.

 1 var birdsData = {  2  labels: ["Spring","Summer","Fall","Winter"],  3  datasets: [{  4  data: [1200, 1700, 800, 200],  5  backgroundColor: [  6  "rgba(255, 0, 0, 0.5)",  7  "rgba(100, 255, 0, 0.5)",  8  "rgba(200, 50, 255, 0.5)",  9  "rgba(0, 100, 255, 0.5)"  10  ]  11  }]  12 };  13 14 var chartOptions = {  15  plugins: {  16  title: {  17  display: true,  18  align: "start",  19  text: "Migratory Birds in Different Seasons"  20  },  21  legend: {  22  align: "start"  23  }  24  }  25 };  26 27 var polarAreaChart = new Chart(birdsCanvas, {  28  type: 'polarArea',  29  data: birdsData,  30  options: chartOptions  31 }); 

The above code will create the following polar area chart.

The polar area chart provides the usual options to set the backgroundColor, borderWidth, borderColor, hoverBackgroundColor, hoverBorderWidth, and hoverBorderColor. There are also some keys specific to polar area charts that you can use to customize their appearance.

For example, you can set the starting angle for the first value in the dataset using the startAngle key. Similarly, the angle subtended by each arc can be specified using the angle key inside arc. This angle is calculated automatically in such a way that it covers the whole 360° area. However, you can set it to any value you like.

The sectors drawn in the polar area chart are both rotated and scaled by default. You can prevent the scaling animation by setting the value of the animateScale key to false. Similarly, the rotating animation can be turned off by setting the value of the animateRotate key to false. Both these keys are available under animation.

The code below changes the value of a few keys to make the chart more visually appealing.

 1 var chartOptions = {  2  plugins: {  3  title: {  4  display: true,  5  position: "bottom",  6  text: "Migratory Birds in Different Seasons"  7  },  8  legend: {  9  align: "center",  10  position: "left",  11  labels: {  12  font: {  13  size: 16  14  }  15  }  16  }  17  },  18  animation: {  19  animateRotate: false  20  },  21  scales: {  22  r: {  23  ticks: {  24  font: {  25  size: 16,  26  },  27  color:"white",  28  backdropColor: "black"  29  }  30  },  31  },  32  elements: {  33  arc: {  34  angle: 180,  35  borderColor: "black"  36  }  37  }  38 }; 

Besides rotating the chart and disabling the rotation animation, we have also moved the legend to the left of the chart by setting the value of position to left. This leaves enough space at the top of the chart to display it properly.

## Final Thoughts

In this tutorial, you learned about the applications of radar and polar area charts. After that, you learned how to create basic charts and customize them with various configuration options available in Chart.js. You will learn about pie, donut, and bubble charts in the next part of the series.

If you're working with the web, especially on the front-end, JavaScript is important to know. Of course, it's not without its learning curves, and there are plenty of frameworks and libraries to keep you busy, as well. If you're looking for additional resources to study or to use in your work, check out what we have available on Envato Market.