Creating pie charts programmatically

瘦欲@ 提交于 2019-12-22 03:49:20

问题


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?


回答1:


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:

  • Charts4J
  • EasyCharts (Commercial)



回答2:


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);
  }

}




回答3:


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;
      }
   }


来源:https://stackoverflow.com/questions/13662984/creating-pie-charts-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!