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