Sometimes you will have a Pie chart with a lot of slices. This tutorial will teach how to automatically pre-hide some of them using “data fields” and data.
Before we begin, let’s make sure we absolutely know how Pie series’ data fields work. To do so visit “Setting up series ” section in our Pie chart article.
As our test guinea pig, let’s use a very basic Pie chart with a Legend:
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
var chart = am4core . create ( " chartdiv " , am4charts . PieChart );
country: " Czech Republic " ,
country: " The Netherlands " ,
// Add and configure Series
var pieSeries = chart . series . push ( new am4charts . PieSeries ());
pieSeries . dataFields . value = " litres " ;
pieSeries . dataFields . category = " country " ;
// Let's cut a hole in our Pie chart the size of 40% the radius
chart . innerRadius = am4core . percent ( 40 );
// Disable ticks and labels
pieSeries . labels . template . disabled = true ;
pieSeries . ticks . template . disabled = true ;
pieSeries . slices . template . tooltipText = "" ;
chart . legend = new am4charts . Legend ();
chart . legend . position = " right " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Pre-hiding Pie Chart slices (1) pre-hiding-pie-chart-slices-1
Suppose we want to hide last 4 slices, since they are not very relative because of their size.
For that we have a data field "hidden" in Pie series. Just like we were setting data fields for value and category, we can set this field to specify which slice visibility in our data:
pieSeries.dataFields.hidden = "show";
pieSeries.dataFields.hidden = "show";
Now, our Pie series will know that it needs to hide slices that have show set to false in their data:
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
var chart = am4core . create ( " chartdiv " , am4charts . PieChart );
country: " Czech Republic " ,
country: " The Netherlands " ,
// Add and configure Series
var pieSeries = chart . series . push ( new am4charts . PieSeries ());
pieSeries . dataFields . value = " litres " ;
pieSeries . dataFields . category = " country " ;
pieSeries . dataFields . hidden = " hidden " ;
// Let's cut a hole in our Pie chart the size of 40% the radius
chart . innerRadius = am4core . percent ( 40 );
// Disable ticks and labels
pieSeries . labels . template . disabled = true ;
pieSeries . ticks . template . disabled = true ;
pieSeries . slices . template . tooltipText = "" ;
chart . legend = new am4charts . Legend ();
chart . legend . position = " right " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Pre-hiding Pie Chart slices (2) pre-hiding-pie-chart-slices-2