Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating pie charts programmatically [closed]

Tags:

java

pie-chart

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?

like image 238
Jakob Weisblat Avatar asked Dec 01 '12 19:12

Jakob Weisblat


1 Answers

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

like image 91
herrtim Avatar answered Oct 02 '22 13:10

herrtim