12

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

Alternately, would it be better to use a library that will draw pie charts in a JFrame and then somehow automatically screenshot those?

Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65

3 Answers3

9

You can use the XChart library, a very light-weight and straight forward charting library for Java. The following code will create a pie chart. You can also right-click the chart and save as different bitmap types including PNG, JPG, BMP, SVG, EPS, and PDF. Disclaimer, I'm 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

herrtim
  • 2,697
  • 1
  • 26
  • 36
  • 1
    This looks beautiful, will definitely use your library thank you! How would you incorporate this into a Swing JPanel? – Enigmatical Jul 18 '16 at 20:04
  • See the section "Advanced Example" on the README at: https://github.com/timmolter/XChart to see how to embed it into a Swing JPanel. – herrtim Jul 19 '16 at 08:12
  • @herrtim is there anyway, i can add it to same frame. i used to save an image of it, then present the image in my frame. I don't want to make a seperate frame for it. – Sal-laS Mar 23 '17 at 05:26
  • @Salman See the `Advanced Example` here: https://github.com/timmolter/XChart. Just take the XChartPanel and add it to your `JFrame` like any other `JPanel`. – herrtim Mar 23 '17 at 11:45
6

JFreeChart can generate pie charts and can save the resultant chart to JPEG format. Here is an example of how to do so. Here is a 3D example.

There are more examples and documentation available in the developers guide.

Other charting libraries which generate piecharts:

Reimeus
  • 158,255
  • 15
  • 216
  • 276
5

this is one is good, no need of use any 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;
      }
   }
nraj
  • 51
  • 1
  • 1