Creating donut charts

Donut charts are similar to pie charts, but they are missing the middle section (hence the name donut). Some analysts prefer donut charts to pie charts because they do not emphasize the size of each piece within the chart and are easier to compare to other donut charts. They also provide the added advantage of taking up less space, allowing for more formatting options in the display.

In this example, we will assume our data is already populated in a two-dimensional array called ageCount. The first row of the array contains the possible age values, ranging again from 19 to 30 (inclusive). The second row contains the number of data values equal to each age. For example, in our dataset, there are six data values equal to 19, so ageCount[0][1] contains the number six.

We create a DataTable and use the add method to add our values from the array. Notice we are testing to see if the value of a particular age is zero. In our test case, there will be zero data values equal to 23. We are opting to add a blank space in our donut chart if there are no data values for that point. This is accomplished by using a negative number as the first parameter in the add method. This will set an empty space of size 3:

DataTable donutData = new DataTable(Integer.class, Integer.class); 
for(int Y = 0; Y < ageCount[0].length; y++){ 
    if(ageCount[1][y] == 0){ 
        donutData.add(-3, ageCount[0][y]); 
    }else{ 
        donutData.add(ageCount[1][y], ageCount[0][y]); 
    } 
} 
 

Next, we create our donut plot using the PiePlot class. We set basic properties of the plot, including specifying the values for the legend. In this case, we want our legend to reflect our age possibilities, so we use the setLabelColumn method to change the default labels. We also set our insets as we did in the previous example:

PiePlot testPlot = new PiePlot(donutData); 
((ValueLegend) testPlot.getLegend()).setLabelColumn(1); 
testPlot.getTitle().setText("Donut Plot Example"); 
testPlot.setRadius(0.9); 
testPlot.setLegendVisible(true); 
testPlot.setInsets(new Insets2D.Double(20.0, 20.0, 20.0, 20.0)); 

Next, we create a PieSliceRenderer object to set more advanced properties. Because a donut plot is basically a pie plot in essence, we will render a donut plot by calling the setInnerRadius method. We also specify the gap between the pie slices, the colors used, and the style of the labels:

 
PieSliceRenderer renderPie = (PieSliceRenderer) 
testPlot.getPointRenderer(donutData); 
renderPie.setInnerRadius(0.4); 
renderPie.setGap(0.2); 
LinearGradient colors = new LinearGradient( 
    Color.blue, Color.green); 
renderPie.setColor(colors); 
renderPie.setValueVisible(true); 
renderPie.setValueColor(Color.WHITE); 
renderPie.setValueFont(Font.decode(null).deriveFont(Font.BOLD)); 
 

Finally, we create our panel and set its size:

add(new InteractivePanel(testPlot), BorderLayout.CENTER); 
setSize(1500, 700); 
setVisible(true); 
 

When the application is executed, the following graph is displayed:

Creating donut charts
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.246