Creating Pie Charts Programmatically

How can I programmatically generate pie charts from java? I have some data that is processed by the program, then I want to create an image file (PNG, GIF, etc.), which has a pie chart. Is there a library that does this, or at least what I can use for this?

Alternatively, would it be better to use a library that will draw pie charts in a JFrame and then somehow automatically take them off the screen?

+6
source share
3 answers

JFreeChart can generate pie charts and save the saved chart in JPEG format. The following is an example. Below is an example of 3D .

The developer guide has more examples and documentation .

Other graphic libraries that generate piecharts:

+5
source

You can use the XChart library, a very lightweight and direct graphics library for Java. The following code will create a pie chart. You can also right-click a chart and save as different types of bitmap images, including PNG, JPG, BMP, SVG, EPS and PDF. Disclaimer, I am the main developer of the XChart library.

public class PieChartDemo { public static void main(String[] args) throws IOException { // Create Chart PieChart chart = new PieChartBuilder().width(800).height(600).title("My Pie Chart").theme(ChartTheme.GGPlot2).build(); // Customize Chart chart.getStyler().setLegendVisible(false); chart.getStyler().setAnnotationType(AnnotationType.LabelAndPercentage); chart.getStyler().setAnnotationDistance(1.15); chart.getStyler().setPlotContentSize(.7); chart.getStyler().setStartAngleInDegrees(90); // Series chart.addSeries("Prague", 2); chart.addSeries("Dresden", 4); chart.addSeries("Munich", 34); chart.addSeries("Hamburg", 22); chart.addSeries("Berlin", 29); // Show it new SwingWrapper(chart).displayChart(); // Save it BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG); // or save it in high-res BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.PNG, 300); } } 

enter image description here

+4
source

this one is good, no need to use a third-party library

===============================

 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; import javax.swing.JFrame; class Slice { double value; Color color; public Slice(double value, Color color) { this.value = value; this.color = color; } } class PieChart3 extends JComponent { Slice[] slices = { new Slice(5, Color.black), new Slice(33, Color.green), new Slice(20, Color.yellow), new Slice(15, Color.red) }; PieChart3() {} public void paint(Graphics g) { drawPie((Graphics2D) g, getBounds(), slices); } void drawPie(Graphics2D g, Rectangle area, Slice[] slices) { double total = 0.0D; for (int i = 0; i < slices.length; i++) { total += slices[i].value; } double curValue = 0.0D; int startAngle = 0; for (int i = 0; i < slices.length; i++) { startAngle = (int) (curValue * 360 / total); int arcAngle = (int) (slices[i].value * 360 / total); g.setColor(slices[i].color); g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle); curValue += slices[i].value; } } 
+2
source

All Articles