2012-03-30 18:31:20|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:Java, 界面设计, Swing|来源:唯设编程网
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。
JFreeChart截止2011年2月22日为止的相当不错的java图形解决方案,基本能够解决目前的图形方面的需求。本文着重对JFreeChart在Swing客户端界面实现柱形图进行介绍,其它统计图表的使用方法类似,读者可以参考JFreeChart提供的API。程序实现的效果如图:
Java代码:
package Test.JFreeChar; import java.awt.Color; import java.awt.Dimension; import java.awt.GradientPaint; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; /** * JFreeChar柱形图 * */ public class BarChartDemo extends ApplicationFrame { private static final long serialVersionUID = 1L; public BarChartDemo(String paramString) { super(paramString); JPanel localJPanel = createDemoPanel(); localJPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(localJPanel); } /** * 准备绘制柱形图使用的数据集 */ private static CategoryDataset createDataset() { String str1 = "First"; String str2 = "Second"; String str3 = "Third"; String str4 = "Category 1"; String str5 = "Category 2"; String str6 = "Category 3"; String str7 = "Category 4"; String str8 = "Category 5"; DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset(); localDefaultCategoryDataset.addValue(1.0D, str1, str4); localDefaultCategoryDataset.addValue(4.0D, str1, str5); localDefaultCategoryDataset.addValue(3.0D, str1, str6); localDefaultCategoryDataset.addValue(5.0D, str1, str7); localDefaultCategoryDataset.addValue(5.0D, str1, str8); localDefaultCategoryDataset.addValue(5.0D, str2, str4); localDefaultCategoryDataset.addValue(7.0D, str2, str5); localDefaultCategoryDataset.addValue(6.0D, str2, str6); localDefaultCategoryDataset.addValue(8.0D, str2, str7); localDefaultCategoryDataset.addValue(4.0D, str2, str8); localDefaultCategoryDataset.addValue(4.0D, str3, str4); localDefaultCategoryDataset.addValue(3.0D, str3, str5); localDefaultCategoryDataset.addValue(2.0D, str3, str6); localDefaultCategoryDataset.addValue(3.0D, str3, str7); localDefaultCategoryDataset.addValue(6.0D, str3, str8); return localDefaultCategoryDataset; } /** * 创建JFreeChart * @param paramCategoryDataset 数据集 * @return */ private static JFreeChart createChart(CategoryDataset paramCategoryDataset) { JFreeChart localJFreeChart = ChartFactory.createBarChart( "Bar Chart Demo 1", "Category", "Value", paramCategoryDataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot localCategoryPlot = (CategoryPlot) localJFreeChart .getPlot(); localCategoryPlot.setDomainGridlinesVisible(true); localCategoryPlot.setRangeCrosshairVisible(true); localCategoryPlot.setRangeCrosshairPaint(Color.blue); NumberAxis localNumberAxis = (NumberAxis) localCategoryPlot .getRangeAxis(); localNumberAxis.setStandardTickUnits(NumberAxis .createIntegerTickUnits()); BarRenderer localBarRenderer = (BarRenderer) localCategoryPlot .getRenderer(); localBarRenderer.setDrawBarOutline(false); GradientPaint localGradientPaint1 = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0, 0, 64)); GradientPaint localGradientPaint2 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0)); GradientPaint localGradientPaint3 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0)); localBarRenderer.setSeriesPaint(0, localGradientPaint1); localBarRenderer.setSeriesPaint(1, localGradientPaint2); localBarRenderer.setSeriesPaint(2, localGradientPaint3); localBarRenderer .setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator( "Tooltip: {0}")); CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis(); localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions .createUpRotationLabelPositions(0.5235987755982988D)); return localJFreeChart; } /** * 创建显示柱形图的面板 * @return */ public static JPanel createDemoPanel() { JFreeChart localJFreeChart = createChart(createDataset()); return new ChartPanel(localJFreeChart); } public static void main(String[] paramArrayOfString) { BarChartDemo localBarChartDemo1 = new BarChartDemo( "JFreeChart: BarChartDemo1.java"); localBarChartDemo1.pack(); RefineryUtilities.centerFrameOnScreen(localBarChartDemo1); localBarChartDemo1.setVisible(true); } }
至此,使用JFreeChart实现的统计柱形图便完成了,JFreeChart为统计图的显示提供了方便的接口,后续文章将会介绍使用JFreeChart实现其它统计图。