浏览 3991 次
锁定老帖子 主题:jfreechart实现柱状图排序
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-27
最后修改:2009-03-27
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GradientPaint; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; 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; /** * jfreechart排序柱状图. */ public class MyBarPic extends ApplicationFrame { /** * * 构造函数,进行初始化操作 * * @param title the frame title. */ public MyBarPic(String title) { super(title); CategoryDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); ChartPanel chartPanel = new ChartPanel(chart, false); chartPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(chartPanel); saveAsFile(chart, "D:\\图\\第一.jpg", 430, 200); } /** * 生成数据集 * * @return */ private static CategoryDataset createDataset() { /** * * 数据集数据存放对象 * * @author newlife111 * */ class ThreeData implements Comparator{ String rowKey;//行标签 String colKey;//列标签 double value;//值 ThreeData(){ } ThreeData(double value, String rowKey, String colKey){ this.value = value; this.rowKey = rowKey; this.colKey = colKey; } /* * 比较的算法 */ public int compare(Object obj1, Object obj2) { double valueTmp = ((ThreeData)obj1).value - ((ThreeData)obj2).value; return valueTmp > 0 ? -1 : (valueTmp < 0 ? 1 : 0); } } /* * 存放数据 */ List list = new ArrayList(); list.add(new ThreeData(100.00d, "xx", "苹\n果")); list.add(new ThreeData(200.00d, "xx", "葡\n萄")); list.add(new ThreeData(50.00d, "xx", "雪\n梨")); list.add(new ThreeData(24.00d, "xx", "香\n蕉")); list.add(new ThreeData(65.00d, "xx", "橘\n子")); list.add(new ThreeData(125.00d, "xx", "柑\n橘")); list.add(new ThreeData(198.00d, "xx", "柚\n子")); list.add(new ThreeData(160.00d, "xx", "草\n莓")); //进行排序 Collections.sort(list,new ThreeData()); /* * 添加数据进数据集 */ DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Iterator iter = list.iterator(); while(iter.hasNext()) { ThreeData threeDataTmp = (ThreeData)iter.next(); dataset.addValue(threeDataTmp.value, threeDataTmp.rowKey, threeDataTmp.colKey); } return dataset; } /** * 生成图片. * * @param dataset 数据集. * * @return chart 图片对象 */ private static JFreeChart createChart(CategoryDataset dataset) { JFreeChart chart = ChartFactory.createBarChart( "xxx", // 图片标题 "xxx", // 横轴标题 "xxx", // 纵轴标题 dataset, // 数据集 PlotOrientation.VERTICAL, // 图形方向 true, // 是否legend true, // 图形提示 false // 图形url ); //设置图片背景色 chart.setBackgroundPaint(Color.white); //获得数据区对象 CategoryPlot plot = (CategoryPlot) chart.getPlot(); //设置图形背景色 plot.setBackgroundPaint(Color.white); //横轴网格显示 plot.setDomainGridlinesVisible(true); //横轴网格颜色 plot.setRangeGridlinePaint(Color.black); /* * 图形相关设置 */ BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); renderer.setBaseItemLabelFont(new Font("黑体", Font.BOLD, 12)); renderer.setBaseLegendTextFont(new Font("Simsun", 0, 10)); renderer.setBaseItemLabelsVisible(true); renderer.setMaximumBarWidth(0.05f); renderer.setDrawBarOutline(false); renderer.setShadowVisible(false); /* * 纵轴相关设置 */ final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setLabelFont(new Font("Simsun", 0, 12)); /* * 图形1颜色设置 */ GradientPaint gp0 = new GradientPaint(0.5f, 0.5f, new Color(100,100,255), 0.0f, 0.0f, new Color(0, 0, 64)); renderer.setSeriesPaint(0, gp0); /* * 横轴相关设置 */ CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(new Font("Simsun", Font.BOLD, 12)); //Font.BOLD字符加粗可使标签显示清楚些 domainAxis.setTickLabelFont(new Font("Simsun", Font.BOLD, 8)); domainAxis.setLowerMargin(0.03f); //这里设置横轴标签可显示行数,在数据集的横轴字符串上用\n可实现标签字符垂直 domainAxis.setMaximumCategoryLabelLines(255); domainAxis.setCategoryLabelPositionOffset(3); return chart; } /** * 程序入口 * * @param args */ public static void main(String[] args) { MyBarPic demo = new MyBarPic("FirstPic"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } /** * * 保存为文件 * * @param chart 图片对象 * @param outputPath 保存路径 * @param weight 宽 * @param height 高 */ public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) { FileOutputStream out = null; try { File outFile = new File(outputPath); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } out = new FileOutputStream(outputPath); //保存为JPEG ChartUtilities.writeChartAsJPEG(out,chart,800,600); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { } } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |