jfreechart的用法解说.doc_第1页
jfreechart的用法解说.doc_第2页
jfreechart的用法解说.doc_第3页
jfreechart的用法解说.doc_第4页
jfreechart的用法解说.doc_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Jfreechart的基础图表用法与在web项目中调用下面是我自己学习时测试的例子,一天就可以搞定,写的比较简单,基本的功能都有,具体的属性百度,一般的都有解答,希望能够对你们有所帮助jar包环境到官网/jfreechart/下载资源包文件将包文件放到你的项目里面,直接写java类,可以在java类的main方法里面直接生成图片如例子public class BarChartDemo /* * 获取一个演示用的组合数据集对象 * * return */private static CategoryDataset getDataSet() DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(100, 北京, 苹果);dataset.addValue(120, 上海, 苹果);dataset.addValue(160, 广州, 苹果);dataset.addValue(210, 北京, 梨子);dataset.addValue(220, 上海, 梨子);dataset.addValue(230, 广州, 梨子);dataset.addValue(330, 北京, 葡萄);dataset.addValue(340, 上海, 葡萄);dataset.addValue(340, 广州, 葡萄);dataset.addValue(420, 北京, 香蕉);dataset.addValue(430, 上海, 香蕉);dataset.addValue(400, 广州, 香蕉);dataset.addValue(510, 北京, 荔枝);dataset.addValue(530, 上海, 荔枝);dataset.addValue(510, 广州, 荔枝);return dataset;/* * 解决图表汉字显示问题 * * param chart */private static void processChart(JFreeChart chart) CategoryPlot plot = chart.getCategoryPlot();CategoryAxis domainAxis = plot.getDomainAxis();ValueAxis rAxis = plot.getRangeAxis();chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font(宋体, Font.PLAIN, 20);domainAxis.setTickLabelFont(new Font(sans-serif, Font.PLAIN, 11);domainAxis.setLabelFont(new Font(宋体, Font.PLAIN, 12);rAxis.setTickLabelFont(new Font(sans-serif, Font.PLAIN, 12);rAxis.setLabelFont(new Font(宋体, Font.PLAIN, 12);chart.getLegend().setItemFont(new Font(宋体, Font.PLAIN, 12);/3d时设置/* BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer(); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator();/设置柱子上显示文字 renderer.setItemLabelFont(new Font(宋体, Font.PLAIN, 12); renderer.setItemLabelsVisible(true);*/2dBarRenderer renderer = (BarRenderer) plot.getRenderer();renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator();/设置柱子上显示文字 renderer.setItemLabelFont(new Font(宋体, Font.PLAIN, 12); renderer.setItemLabelsVisible(true);/* * 输出图片 * * param chart */private static void writeChartAsImage(JFreeChart chart) FileOutputStream fos_jpg = null;try fos_jpg = new FileOutputStream(D:testfruit.jpg);ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null); catch (Exception e) e.printStackTrace(); finally try fos_jpg.close(); catch (Exception e) public void createZhu()/ 1. 得到数据CategoryDataset dataset = getDataSet();/ 2. 构造chartJFreeChart chart = ChartFactory.createBarChart3D(水果产量图, / 图表标题水果, / 目录轴的显示标签-横轴产量, / 数值轴的显示标签-纵轴dataset, / 数据集PlotOrientation.VERTICAL, / 图表方向:水平、true, / 是否显示图例(对于简单的柱状图必须false, / 是否生成工具false / 是否生成URL链接);/ 3. 处理chart中文显示问题processChart(chart);/ 4. chart输出图片writeChartAsImage(chart);/ 5. chart 以swing形式输出ChartFrame pieFrame = new ChartFrame(XXX, chart);pieFrame.pack();pieFrame.setVisible(true);public static void main(String args) throws IOException BarChartDemo demo = new BarChartDemo();/demo.createZhu();/3d条形图效果图如下饼状图代码/3D饼状图public void createPieChart3D() DefaultPieDataset dataset = new DefaultPieDataset();dataset.setValue(苹果, 250);dataset.setValue(桔子, 350);dataset.setValue(梨子, 200);dataset.setValue(香蕉, 50);dataset.setValue(荔枝, 150);JFreeChart chart = ChartFactory.createPieChart3D(水果产量比率图, dataset,true, true, true);TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font(黑体,Font.BOLD,15);/标题设置字体PiePlot3D plot = (PiePlot3D) chart.getPlot();plot.setLabelFont(new Font(黑体, Font.TRUETYPE_FONT, 15); plot.setLabelGenerator(new StandardPieSectionLabelGenerator(0,1,(2), NumberFormat.getNumberInstance(), new DecimalFormat(0.00%); chart.getLegend().setItemFont(new Font(宋体,Font.BOLD, 15);/ 设置开始角度plot.setStartAngle(150D);/ 设置方向为顺时针方向plot.setDirection(Rotation.CLOCKWISE);/ 设置透明度,0.5F为半透明,1为不透明,0为全透明plot.setForegroundAlpha(1F);/ 没有数据的时候显示的内容plot.setNoDataMessage(无数据显示);/ 背景色设置plot.setBackgroundPaint(ChartColor.WHITE);try ChartUtilities.saveChartAsPNG(new File(D:testPieChart3D.png),chart, 800, 500); catch (IOException e) e.printStackTrace();/2d饼状图代码/柱状2d柱状public void createColumnChart() DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(100, 北京, 苹果);dataset.addValue(100, 上海, 苹果);dataset.addValue(100, 广州, 苹果);dataset.addValue(200, 北京, 梨子);dataset.addValue(200, 上海, 梨子);dataset.addValue(200, 广州, 梨子);dataset.addValue(300, 北京, 葡萄);dataset.addValue(300, 上海, 葡萄);dataset.addValue(300, 广州, 葡萄);dataset.addValue(400, 北京, 香蕉);dataset.addValue(400, 上海, 香蕉);dataset.addValue(400, 广州, 香蕉);dataset.addValue(500, 北京, 荔枝);dataset.addValue(500, 上海, 荔枝);dataset.addValue(500, 广州, 荔枝);JFreeChart chart = ChartFactory.createBarChart(水果产量图, 水量, 产量,dataset, PlotOrientation.VERTICAL, true, true, true);processChart(chart);try ChartUtilities.saveChartAsPNG(new File(D:testColumnChart.png),chart, 800, 500); catch (IOException e) e.printStackTrace();2d折线图代码/2d折线图public void createLineChart() DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(100, 北京, 苹果);dataset.addValue(200, 上海, 苹果);dataset.addValue(300, 广州, 苹果);dataset.addValue(400, 北京, 梨子);dataset.addValue(500, 上海, 梨子);dataset.addValue(600, 广州, 梨子);dataset.addValue(700, 北京, 葡萄);dataset.addValue(800, 上海, 葡萄);dataset.addValue(900, 广州, 葡萄);dataset.addValue(800, 北京, 香蕉);dataset.addValue(700, 上海, 香蕉);dataset.addValue(600, 广州, 香蕉);dataset.addValue(500, 北京, 荔枝);dataset.addValue(400, 上海, 荔枝);dataset.addValue(300, 广州, 荔枝);JFreeChart chart = ChartFactory.createLineChart(水果产量图 , 水果, 产量,dataset, PlotOrientation.VERTICAL, true, true, true); CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(ChartColor.WHITE); / 背景色设置plot.setRangeGridlinePaint(ChartColor.GRAY); / 网格线色设置CategoryAxis domainAxis = plot.getDomainAxis();ValueAxis rAxis = plot.getRangeAxis();chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font(宋体, Font.PLAIN, 20);domainAxis.setTickLabelFont(new Font(sans-serif, Font.PLAIN, 11);domainAxis.setLabelFont(new Font(宋体, Font.PLAIN, 12);rAxis.setTickLabelFont(new Font(sans-serif, Font.PLAIN, 12);rAxis.setLabelFont(new Font(宋体, Font.PLAIN, 12);chart.getLegend().setItemFont(new Font(宋体, Font.PLAIN, 12);LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator();/设置柱子上显示文字 renderer.setItemLabelFont(new Font(宋体, Font.PLAIN, 12); renderer.setItemLabelsVisible(true);try ChartUtilities.saveChartAsPNG(new File(D:testLineChart.png),chart, 800, 500); catch (IOException e) e.printStackTrace();3d折线图代码public void createLineChart3D() DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(300, 北京, 苹果);dataset.addValue(100, 上海, 苹果);dataset.addValue(900, 广州, 苹果);dataset.addValue(200, 北京, 梨子);dataset.addValue(200, 上海, 梨子);dataset.addValue(700, 广州, 梨子);dataset.addValue(300, 北京, 葡萄);dataset.addValue(300, 上海, 葡萄);dataset.addValue(300, 广州, 葡萄);dataset.addValue(400, 北京, 香蕉);dataset.addValue(800, 上海, 香蕉);dataset.addValue(400, 广州, 香蕉);dataset.addValue(100, 北京, 荔枝);dataset.addValue(500, 上海, 荔枝);dataset.addValue(500, 广州, 荔枝);JFreeChart chart = ChartFactory.createLineChart3D(水果产量图 , 水果, 产量,dataset, PlotOrientation.VERTICAL, true, true, true); CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(ChartColor.WHITE); / 背景色设置plot.setRangeGridlinePaint(ChartColor.GRAY); / 网格线色设置CategoryAxis domainAxis = plot.getDomainAxis();ValueAxis rAxis = plot.getRangeAxis();chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font(宋体, Font.PLAIN, 20);domainAxis.setTickLabelFont(new Font(sans-serif, Font.PLAIN, 11);domainAxis.setLabelFont(new Font(宋体, Font.PLAIN, 12);rAxis.setTickLabelFont(new Font(sans-serif, Font.PLAIN, 12);rAxis.setLabelFont(new Font(宋体, Font.PLAIN, 12);chart.getLegend().setItemFont(new Font(宋体, Font.PLAIN, 12);LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator();/设置柱子上显示文字 renderer.setItemLabelFont(new Font(宋体, Font.PLAIN, 12); renderer.setItemLabelsVisible(true);try ChartUtilities.saveChartAsPNG(new File(D:testLineChart3D.png),chart, 800, 500); catch (IOException e) e.printStackTrace();点与折线结合的图代码如下package chart.demo02;import java.awt.Font;import java.io.File;import java.io.IOException;import java.text.DecimalFormat;import java.text.NumberFormat;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.labels.StandardXYToolTipGenerator;import org.jfree.chart.plot.PiePlot3D;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;import org.jfree.chart.title.TextTitle;import org.jfree.data.xy.XYDataset;import org.jfree.data.xy.XYSeries;import org.jfree.data.xy.XYSeriesCollection;/点状图与折线一起显示 public class pointAndLine public static void main(String args) JFreeChart chart = createChart(createDataset(); try TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font(黑体,Font.BOLD,15);/标题设置字体 ChartUtilities.saveChartAsPNG(new File(D:testpointAndLine.png),chart, 800, 500); catch (IOException e) e.printStackTrace();private static XYDataset createDataset() XYSeries xyseries = new XYSeries(点状图); xyseries.add(1.0D, 1.0D); xyseries.add(2D, 4D); xyseries.add(3D, 3D); xyseries.add(4D, 5D); xyseries.add(5D, 5D); xyseries.add(6D, 7D); xyseries.add(7D, 7D); xyseries.add(8D, 8D); XYSeries xyseries1 = new XYSeries(折线图); xyseries1.add(1.0D, 5D); xyseries1.add(2D, 7D); xyseries1.add(3D, 6D); xyseries1.add(4D, 8D); xyseries1.add(5D, 4D); xyseries1.add(6D, 4D); xyseries1.add(7D, 2D); xyseries1.add(8D, 1.0D); XYSeriesCollection xyseriescollection = new XYSeriesCollection(); xyseriescollection.addSeries(xyseries); xyseriescollection.addSeries(xyseries1); return xyseriescollection; private static JFreeChart createChart(XYDataset xydataset) JFreeChart jfreechart = ChartFactory.createXYLineChart(点图与折线图, X轴, Y轴, xydataset, PlotOrientation.VERTICAL, true, true, false); XYPlot xyplot = (XYPlot)jfreechart.getPlot(); ValueAxis valueAxis=xyplot.getDomainAxis(); /x轴 valueAxis.setLabelFont( new Font(黑体, Font.BOLD, 12); /x轴字体 XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(); xylineandshaperenderer.setSeriesLinesVisible(0, false); xylineandshaperenderer.setSeriesShapesVisible(1, false); xylineandshaperenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(); xyplot.setRenderer(xylineandshaperenderer); NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();/y轴 / 设置y轴上的字体 numberaxis.setTickLabelFont(new Font(宋体, Font.PLAIN, 13); / 设置y轴上的标题字体 numberaxis.setLabelFont(new Font(宋体, Font.PLAIN, 13); jfreechart.getLegend().setItemFont(new Font(宋体, Font.PLAIN, 13); /x副标题的字体 numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits(); return jfreechart; 结果Web项目中jsp调用在web.xml中配置信息 DisplayChart org.jfree.chart.servlet.DisplayChart DisplayChart /DisplayChart1. jsp例子 基础调用 jfreeChart报表显示 !- img src= width=500 height=300 border=0 Jsp显示效果注意 ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);默认生成的图片是放到了tomcat的temp临时文件里面如果你要自己指定文件录入,可以定义一个类继承ServletUtilities类 在saveChartAsPNG方法中执行设置你的路径如的例子package chart.demo02;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpSession;import org.jfree.chart.ChartRenderingInfo;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.servlet.ServletUtilities;public class JFChartUtils extends ServletUtilities private static

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论