I need to create a donut pie chart for a 6 type of data. I can able to create chart using JFreechart library.
I need to customize this donut pie chart. For example I need to have little gap between slices of the chart. In my case my donut pie chart will have 6 slices with 6 different color in circle shape with this slices are connect and form the circle.
As per my requirement instead of connected slices I need to create a circle of slices with little gap (empty space) between each slices. I am struggle to create circle of slices with little gap.
Attached the sample image. Highlighted part with the gap between the slice is what I am looking.
Also instead of flat edge slices, need to create curvy edge slices.
This is the code I am trying which creates circular doughnut pie chart without gap.
setInteriorGap -> This property only increase or decreasing the inner circle space.
private String createCustomPieChart(Map<String, Float> pieData, String pieChart) {
AtomicInteger count = new AtomicInteger(0);
Integer size = pieData.size();
try {
DefaultPieDataset<String> dataset = new DefaultPieDataset<String>();
pieData.entrySet().stream().forEach(e -> dataset.setValue(e.getKey(), e.getValue()));
JFreeChart chart = ChartFactory.createRingChart("Chart Title", dataset, true, false, false);
chart.setBorderVisible(false);
chart.getLegend().setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(false);
chart.setBackgroundPaint(Color.WHITE);
//Pie Plot area customization
RingPlot rp = (RingPlot)chart.getPlot();
rp.setBackgroundPaint(Color.WHITE);
rp.setSectionDepth(0.10);
rp.setLabelGap(0.5);
rp.setOutlineVisible(false);
//Dynamically generate new color for each section
pieData.entrySet().stream().forEach(entry -> {rp.setSectionPaint(entry.getKey(), Color.getHSBColor((float)count.get()/size, 1, 1)); count.getAndIncrement();});
rp.setSeparatorsVisible(false);
rp.setCircular(true);
rp.setAutoPopulateSectionOutlineStroke(true);
rp.setInteriorGap(0.25); //To increase or decrease circular size of the chart
//rp.setLabelLinkMargin(1.00);
//rp.setOuterSeparatorExtension(2.0);
rp.setInnerSeparatorExtension(0.90);
rp.setLegendItemShape(new Rectangle(20,20)); // Customizing List of items on chart legend
rp.setLabelBackgroundPaint(Color.WHITE);
rp.setLabelOutlinePaint(Color.WHITE);
rp.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}")); // change this to display only key or value
rp.setSimpleLabelOffset(new RectangleInsets(UnitType.RELATIVE, 0.09, 0.09, 0.09, 0.09)); // Customizing Labels on the plot area
rp.setSimpleLabels(true); // To avoid lines for labels
//Display some text at centre
rp.setCenterTextMode(CenterTextMode.FIXED);
rp.setCenterText("Total Split 500");
//Write to an image
BufferedImage bufferedImage = chart.createBufferedImage(500, 500);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
return pieChart.replace("graph.jpg", "data:image/png;base64,"+DatatypeConverter.printBase64Binary(baos.toByteArray()));
}catch (Exception e) {
e.printStackTrace();
log.error("Exception from Pie Image generate: {}", e.getMessage());
}
return "";
}
