JFreeChart一个免费的Java图表库.JFreeChart支持饼图(2D和3D),条形图(水平和垂直,整齐堆叠),线图,散点图,时序图,
高低的开闭图,烛台图,甘特图,结合地块,温度计,刻度盘和很多其它。JFreeChart能够在应用程序中使用,小程序,servlet和JSP。
由于JFreeChart能够生成的图像非常多,在此简要列举三个(饼图,柱状图,曲线图)作为入门,学习怎样整合Struts怎样显示在JSP中
首先要有一个搭建好的Struts框架,在此基础上我们就能够开发使用JFreeChart了,
详细Struts2的搭建參考http://blog.csdn.net/itmyhome1990/article/details/36186059
注:当然也全然能够不用Struts,在JSP中写java脚本一样能够执行JFreeChart,但为了规范性还是建议使用Struts
JFreeChart是开放源代码的免费软件,下载地址(含本演示样例源代码) http://download.csdn.net/detail/itmyhome/7598033
本文使用的JFreeChart版本号为jfreechart-1.0.17
MyEclipse中新建一Webproject,将下载的jfreechart-1.0.17下lib文件夹下的jcommon-1.0.21.jar,jfreechart-1.0.17.jar
导入到project下。另在web.xml文件里添加例如以下配置:
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping>
DefaultPieDataset dpd = new DefaultPieDataset(); dpd.setValue("JDK1.4", 60); dpd.setValue("JDK1.5", 15); dpd.setValue("JDK1.6", 25); dpd.setValue("JDK1.7", 25); dpd.setValue("JDK1.8", 25); JFreeChart chart = ChartFactory.createPieChart("开发人员眼下採用JDK版本号分布", dpd, true,false, false); String fileName = ""; try { fileName = ServletUtilities.saveChartAsPNG(chart, 600, 500, session); } catch (IOException e) { e.printStackTrace(); } //JSP页面显示的URL pieChartURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;在JSP中 <img src="${barChartURL }" width=700 height=500 border=0> 就可以显示该饼图(下同)
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(42, "语言", "JAVA"); dataset.addValue(17, "语言", "C#"); dataset.addValue(14, "语言", "C++"); dataset.addValue(12, "语言", "C"); dataset.addValue(3, "语言", "PHP"); dataset.addValue(2, "语言", "JavsScript"); dataset.addValue(1, "语言", "Objective-C"); dataset.addValue(3, "语言", "Python"); dataset.addValue(4, "语言", "其它"); JFreeChart chart = ChartFactory.createBarChart3D("开发人员第一编程语言分布情况", "开发语言", "百分比", dataset, PlotOrientation.VERTICAL, false, false,false); String fileName = ""; try { fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500,null, session); } catch (IOException e) { e.printStackTrace(); } barChartURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;
TimeSeries timeSeries = new TimeSeries("近期30天流量趋势", Day.class); TimeSeriesCollection lineDataset = new TimeSeriesCollection(); timeSeries.add(new Day(01, 06, 2014), 10200); timeSeries.add(new Day(05, 06, 2014), 11200); timeSeries.add(new Day(10, 06, 2014), 12000); timeSeries.add(new Day(15, 06, 2014), 13200); timeSeries.add(new Day(20, 06, 2014), 11600); timeSeries.add(new Day(25, 06, 2014), 13200); timeSeries.add(new Day(30, 06, 2014), 12500); lineDataset.addSeries(timeSeries); JFreeChart chart = ChartFactory.createTimeSeriesChart("訪问量统计时间线", "月份", "訪问量", lineDataset, true, true, true); XYPlot plot = chart.getXYPlot(); //设置网格背景颜色 plot.setBackgroundPaint(Color.white); //设置网格竖线颜色 plot.setDomainGridlinePaint(Color.LIGHT_GRAY); //设置网格横线颜色 plot.setRangeGridlinePaint(Color.LIGHT_GRAY); //设置主标题 chart.setTitle(new TextTitle("近期30天流量趋势")); chart.setAntiAlias(true); String fileName = ""; try { fileName = ServletUtilities.saveChartAsPNG(chart, 600, 500, null, session); } catch (IOException e) { e.printStackTrace(); } graphChartURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;
在图表生成时可能会发生乱码,解决方法例如以下:
StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15)); standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15)); ChartFactory.setChartTheme(standardChartTheme);在使用的过程中假设报下面错误:
org.apache.struts2.dispatcher.Dispatcher warn 警告: Could not find action or result: /jfreechart/DisplayChart?filename=jfreechart-4239939260100283845.png There is no Action mapped for namespace [/] and action name [DisplayChart] associated with context path [/jfreechart]. - [unknown location]而在jsp中显示不出来图片,则原因是Struts配置错误。
由于在后台生成图片的时候有这么一句话request.getContextPath() + "/DisplayChart?filename=" + fileName;
我们用chorme查看图片路径的时候也能够看到
<img src="/jfreechart/DisplayChart?filename=jfreechart-4239939260100283845.png" width="600" height="500" border="0">
走的是servlet 这个路径被Struts拦截了(由于struts配置的拦截为<url-pattern>/*</url-pattern>) 所以找不到相应的action or result
改动web.xml
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/admin/*</url-pattern> </filter-mapping> <!--或者 <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> -->
欢迎添加JAVA技术交流群:74955800
转载请注明出处:http://blog.csdn.net/itmyhome1990/article/details/36898497