首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

作图不同的条形图

2012-06-28 
绘制不同的条形图web.xml这里只贴出公共部分,后面的介绍需要修改此配置文件的,我会另外的说明给出。web.xml

绘制不同的条形图

web.xml

这里只贴出公共部分,后面的介绍需要修改此配置文件的,我会另外的说明给出。
web.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3???? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4???? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6
7???? <!-- 增加的jfreechart的处理类 -->
8???? <servlet>
9???????? <servlet-name>DisplayChart</servlet-name>
10???????? <servlet-class>
11???????????? org.jfree.chart.servlet.DisplayChart
12???????? </servlet-class>
13???????? <init-param>
14???????????? <!-- 解决可能存在的servlet中文乱码问题 -->
15???????????? <param-name>encoding</param-name>
16???????????? <param-value>GBK</param-value>
17???????? </init-param>
18???? </servlet>
19???? <servlet-mapping>
20???????? <servlet-name>DisplayChart</servlet-name>
21???????? <url-pattern>/DisplayChart</url-pattern>
22???? </servlet-mapping>
23
24 </web-app>



下面来介绍用java文件书写的条形图(我用的是servlet实现,为了演示效果,我条形图放了四个实现不同效果条形图的方法)
2.1 在配置文件中加servlet文件的配置

<!-- 为了把新建图的代码写在java代码中增加的一个servlet -->

<servlet>
??? <servlet-name>BarServlet</servlet-name>
??? <servlet-class>bar.BarServlet</servlet-class>
??? <init-param>
??????? <!-- 解决可能存在的servlet中文乱码问题 -->
??????? <param-name>encoding</param-name>
??????? <param-value>GBK</param-value>
??? </init-param>
</servlet>
<servlet-mapping>
??? <servlet-name>BarServlet</servlet-name>
??? <url-pattern>/BarServlet</url-pattern>
</servlet-mapping>
2.2 BarServlet.java
BarServlet.java

? 1 package bar;
? 2
? 3 import org.jfree.chart.ChartFactory;
? 4 import org.jfree.chart.ChartRenderingInfo;
? 5 import org.jfree.chart.JFreeChart;
? 6 import org.jfree.chart.labels.ItemLabelAnchor;
? 7 import org.jfree.chart.labels.ItemLabelPosition;
? 8 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
? 9 import org.jfree.chart.plot.PlotOrientation;
10 import org.jfree.chart.renderer.category.BarRenderer3D;
11 import org.jfree.chart.servlet.ServletUtilities;
12 import org.jfree.data.category.CategoryDataset;
13 import org.jfree.data.category.DefaultCategoryDataset;
14 import org.jfree.data.general.DatasetUtilities;
15 import org.jfree.ui.TextAnchor;
16 import org.jfree.chart.ChartUtilities;
17 import java.awt.BasicStroke;
18 import java.awt.Color;
19 import java.awt.Font;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import org.jfree.chart.title.TextTitle;
23 import org.jfree.chart.axis.AxisLocation;
24 import org.jfree.chart.axis.CategoryLabelPositions;
25 import org.jfree.chart.axis.NumberAxis;
26 import org.jfree.chart.axis.CategoryAxis;
27 import org.jfree.chart.axis.ValueAxis;
28 import org.jfree.chart.entity.StandardEntityCollection;
29 import org.jfree.chart.plot.CategoryPlot;
30 import javax.servlet.ServletOutputStream;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.servlet.http.HttpSession;
35
36 /**
37 *
38 * Module: BarServlet.java
39 * Description: 条形图用java代码来书写
40 * Company:
41 * Asiainfo Author: pan**eng
42 * Date: Dec 15, 2011
43 */
44
45 public class BarServlet extends HttpServlet {
46
47???? private static final long serialVersionUID = 1L;
48???? // 访问此servlet的url为:http://localhost:8080/JFreeChart/BarServlet?num=2
49???? public void doGet(HttpServletRequest request, HttpServletResponse response)
50???????????? throws IOException {
51???????? //获得回话的session对象,ServletUtilities类的saveChartAsPNG方法需要用时把session放进去
52 //HttpSession session = request.getSession();
53???????? /**解决servlet输出的中文乱码问题,这里的乱码和条形图中的乱码可是两种不同的乱码
54????????? * 如果使用response.getWriter()方法获得对象,则setContentType方法必须放在获得out对象之前
55????????? * 如果使用response.getOutputStream()方法获得对象,则setContentType方法放在获得out对象之前或之后都可以
56????????? * */
57???????? response.setContentType("text/html; charset=GBK");
58???????? PrintWriter out = response.getWriter();
59???????? //ServletOutputStream out = response.getOutputStream();
60
61 //response.setCharacterEncoding("GBK");
62
63???????? String num = request.getParameter("num");//获得请求url的参数,用来判断是显示第几个条形图示例
64
65???????? /*
66????????? * 为了少新建文件,也为了少配置web.xml文件,把条形图的介绍都放在一个servlet中了,
67????????? * 每个if分支都是独立的,调用四个不同的方法,实现四个不同效果的条形图,从1到4,功能依次增强
68 */
69????????? String filename ="";//定义一个公共变量,保存生成的名称
70???????? if (num.equals("1")) {
71???????????? out.print("<b>第一种情况,一个简单的条形图</b><br><h1></h1>");
72???????????? filename = bar1();
73???????? } else if (num.equals("2")) {
74???????????? out.print("<b>第二种情况,各种不同颜色的条形图</b><br><h1></h1>");
75???????????? filename = bar2();
76???????? } else if (num.equals("3")) {
77???????????? out.print("<b>第三种情况,多个条形图对比</b><br><h1></h1>");
78???????????? filename = bar3();
79???????? } else if (num.equals("4")) {
80???????????? out.print("<b>第四种情况,多个条形图对比并且每个条形图上面加上数字</b><br><h1></h1>");
81???????????? filename = bar4();
82???????? } else {
83???????????? out.println("<b>输入的url不对,请输入1、2、3、4 来查看不同的条形图</b><br><h1></h1>");
84???????? }
85???????? String graphURL = request.getContextPath() + "/DisplayChart?filename="
86???????????????? + filename;// 调用jfreechart的处理类
87 // 条形图的输出
88 //---------------start---------------
89???????? out.println("<html>");
90???????? out.println("<body>");
91???????? out
92???????????????? .println("<img src=http://www.cnblogs.com/java-pan/archive/2011/12/17//""
93???????????????????????? + graphURL
94???????????????????????? + "" width=600 height=400 border=0 usemap="#"
95???????????????????????? + filename + ""/>");
96???????? out.println("</body>");
97???????? out.println("</html>");
98???????? //---------------end---------------
99???????? out.flush();//刷新该流的缓冲
100???????? out.close();//关闭该流并释放与之关联的所有系统资源
101???? }
102???? //加上接受post请求的处理方法,实质也还是调用doGet方法来处理
103???? public void doPost(HttpServletRequest request, HttpServletResponse response)
104???????????? throws IOException {
105???????? this.doGet(request, response);
106???? }
107???? //条形图示例1 一个简单的条形图
108???? public String bar1() throws IOException{
109???????? DefaultCategoryDataset dataset = new DefaultCategoryDataset();
110???????? // 设置填充数据
111???????? dataset.addValue(25, "襄阳", "襄城");
112???????? dataset.addValue(20, "襄阳", "樊城");
113???????? dataset.addValue(15, "襄阳", "襄州");
114???????? dataset.addValue(10, "襄阳", "东津");
115???????? dataset.addValue(5, "襄阳", "鱼梁州");
116
117???????? /**
118????????? * ChartFactory类的createBarChart3D方法参数介绍:
119????????? * createBarChart3D
120????????? * (
121????????? * String title, 图表标题
122????????? * String categoryAxisLabel, 统计种类轴标题,可以理解为X轴标题
123????????? * String valueAxisLabel,统计值轴标题,可以理解为y轴标题
124????????? * CategoryDataset dataset, 绘图数据集
125????????? * PlotOrientation orientation, 用于设置柱形图的绘制方向,PlotOrientation.VERTICAL(垂直),PlotOrientation.HORIZONTAL(水平)
126????????? * boolean legend, 用于设定是否显示图例
127????????? * boolean tooltips, 用于设定是否采用标准生成器
128????????? * boolean urls 用于设置定否包生成链接
129????????? * )
130 */
131???????? JFreeChart chart = ChartFactory.createBarChart3D("襄阳城区人口统计", "地区",
132???????????????? "人口数(单位:万)", dataset, PlotOrientation.VERTICAL, true,
133???????????????? false, false);
134
135???????? // 设置主标题指定字体,解决中文乱码
136???????? Font font = new Font("宋体", Font.BOLD, 20);
137???????? TextTitle title = new TextTitle("襄阳城区人口统计(主标题)", font);
138???????? chart.setTitle(title);
139???????? /* 以上三句和下面的一句效果是一样的 */
140???????? // chart.setTitle(new TextTitle("襄阳城区人口统计(主标题)",new
141 // Font("宋体",Font.BOLD,20)));
142???????? CategoryPlot plot = chart.getCategoryPlot();
143???????? //获得横轴对象,并设置相关的绘图属性
144???????? CategoryAxis domainAxis = plot.getDomainAxis();
145???????? domainAxis.setAxisLineStroke(new BasicStroke(1.6f));? // 设置轴线粗细
146???????? domainAxis.setAxisLinePaint(Color.BLACK);???????????? //设置轴线颜色
147???????? domainAxis.setCategoryLabelPositionOffset(5);???????? //设置统计种类与轴线的颜色
148???????? domainAxis.setLabelPaint(Color.BLACK);??????????????? //设置坐标轴标题颜色
149???????? domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);// 设置坐标轴标题旋转角度
150???????? /*------设置X轴坐标上的文字,解决中文乱码-----------*/
151???????? /* 本例指的是襄城、樊城、襄州、东津、鱼梁州这些字 */
152???????? domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 15));
153???????? /*------设置X轴的标题文字,解决中文乱码------------*/
154???????? /* 本例指的是"地区"两个字 */
155???????? domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));
156
157???????? //获得纵轴对象,并设置相关的绘图属性
158???????? ValueAxis rangeAxis = plot.getRangeAxis();
159???????? rangeAxis.setAxisLineStroke(new BasicStroke(1.6f));?????? //设置轴线粗细
160???????? rangeAxis.setAxisLinePaint(Color.RED);??????????????????? // 设置轴线颜色
161???????? rangeAxis.setUpperBound(30.0f);?????????????????????????? // 设置坐标最大值
162???????? rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));?????? //设置坐标标记大小
163???????? rangeAxis.setTickMarkPaint(Color.BLACK);????????????????? // 设置坐标标记颜色
164???????? rangeAxis.setLabelPaint(Color.BLACK);???????????????????? //设置坐标轴标题颜色
165???????? rangeAxis.setLabelAngle(Math.PI / 2);???????????????????? //设置坐标轴标题旋转角度???
166???????? rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 15)); //设置Y轴的标题文字,解决中文乱码
167???????? rangeAxis.setUpperMargin(0.15);?????????????????????????? //设置最高一个柱与图片顶端的距离
168???????? rangeAxis.setLowerMargin(0.15);?????????????????????????? //设置最低的一个柱与图片底端的距离
169
170 //也可以通过NumberAxis的方法解决y轴中文乱码问题???????
171???????? /*------设置Y轴的标题文字,解决中文乱码------------*/
172???????? // 本例中指的是"销量"两个字
173 //NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
174 //numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 15));
175???????? /*------设置Y轴坐标上的文字,解决中文乱码------------*/
176???????? /* 本例中指的是"0,25,50..."这些值,因为本来这些就是数值,所以有没有此句都可以 */
177???????? // numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN,
178 // 15));
179
180???????? /*------解决底部汉字乱码的问题,如过程序没有底部,下面的一行代码如果释放的话会报空指针的错误-----------*/
181???????? chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 15));//显示的是dataset.addValue中的值"襄阳"
182???????? /**
183????????? * ServletUtilities类的saveChartAsPNG方法参数介绍:
184????????? * saveChartAsPNG(
185????????? * JFreeChart chart, chart对象
186????????? * int width, 图片的宽度
187????????? * int height, 图片的高度
188????????? * ChartRenderingInfo info,info对象,可以为null
189????????? * HttpSession session session对象,可以为null
190????????? * )
191 */
192
193???????? String filename = ServletUtilities
194???????????????? .saveChartAsPNG(chart, 600, 400, null);
195???????? /**
196????????? * 使用writeImageMap方法输出图片 <?暂时还有问题,后期会修改?>
197???????? ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
198???????? ChartUtilities.writeImageMap(out, filename, info, true);
199 */
200???????? return filename;
201???? }
202???? //条形图示例2 一个不同颜色的条形图
203???? public String bar2() throws IOException {
204???????? double[][] data = http://www.cnblogs.com/java-pan/archive/2011/12/17/new double[][] { {25},{ 20 }, { 15 }, { 10 },
205???????????????? { 5 } };
206???????? String[] rowKeys = { "襄城", "樊城", "襄州", "东津","鱼梁州" };
207???????? String[] columnKeys = { "" };
208???????? CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
209???????????????? rowKeys, columnKeys, data);
210???????? JFreeChart chart = ChartFactory
211???????????????? .createBarChart3D("襄阳城区人口统计", "地区", "人口", dataset,
212???????????????????????? PlotOrientation.VERTICAL, true, false, false);
213???????? //设置主标题指定字体,解决中文乱码
214???????? Font font = new Font("宋体", Font.BOLD, 16);
215???????? TextTitle title = new TextTitle("襄阳城区人口统计(主标题)", font);
216???????? chart.setTitle(title);
217
218???????? CategoryPlot plot = chart.getCategoryPlot();
219???????? NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
220???????? CategoryAxis domainAxis = plot.getDomainAxis();
221???????? /*------设置X轴坐标上的文字-----------*/
222???????? //domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
223???????? /*------设置X轴的标题文字------------*/
224???????? domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
225???????? /*------设置Y轴坐标上的文字-----------*/
226???????? //numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
227???????? /*------设置Y轴的标题文字------------*/
228???????? numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
229
230???????? /*------解决底部汉字乱码的问题-----------*/
231???????? chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
232???????? String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
233???????????????? null);
234???????? return filename;
235???? }
236
237???? //条形图示例3 多个条形图对比
238???? public String bar3() throws IOException {
239???????? double[][] data = http://www.cnblogs.com/java-pan/archive/2011/12/17/new double[][] { { 1310, 1220, 1110, 1000 ,666},
240???????????????? { 720, 700, 680, 640 ,777}, { 1130, 1020, 980, 800 ,888},
241???????????????? { 440, 400, 360, 300 ,999} ,{400,400,400,400,555}};
242
243???????? String[] rowKeys = { "猪肉", "牛肉", "鸡肉", "鱼肉","羊肉" };
244???????? String[] columnKeys = { "襄城", "樊城", "襄州", "东津","鱼梁州" };
245???????? CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
246???????????????? rowKeys, columnKeys, data);
247???????? JFreeChart chart = ChartFactory
248???????????????? .createBarChart3D("襄阳各区肉类销量统计图", "地区", "销量", dataset,
249???????????????????????? PlotOrientation.VERTICAL, true, false, false);
250???????? //设置主标题指定字体,解决中文乱码
251???????? Font font = new Font("宋体", Font.BOLD, 16);
252???????? TextTitle title = new TextTitle("肉类销量统计图(主标题)", font);
253???????? chart.setTitle(title);
254
255???????? CategoryPlot plot = chart.getCategoryPlot();
256???????? NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
257???????? CategoryAxis domainAxis = plot.getDomainAxis();
258???????? /*------设置X轴坐标上的文字-----------*/
259???????? domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
260???????? /*------设置X轴的标题文字------------*/
261???????? domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
262???????? /*------设置Y轴坐标上的文字-----------*/
263???????? //numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
264???????? /*------设置Y轴的标题文字------------*/
265???????? numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
266
267???????? /*------解决底部汉字乱码的问题-----------*/
268???????? chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
269???????? String filename = ServletUtilities
270???????????????? .saveChartAsPNG(chart, 500, 300, null);
271???????? return filename;
272???? }
273
274???? //条形图示例4 多个条形图对比并且每个条形图上面加上数字
275???? public String bar4() throws IOException {
276???????? double[][] data = http://www.cnblogs.com/java-pan/archive/2011/12/17/new double[][] { { 1310, 1220, 1110, 1000 ,666},
277???????????????? { 720, 700, 680, 640 ,777}, { 1130, 1020, 980, 800,888 },
278???????????????? { 440, 400, 360, 300 ,999} ,{400,400,400,400,555}};
279
280???????? String[] rowKeys = { "猪肉", "牛肉", "鸡肉", "鱼肉" ,"羊肉"};
281???????? String[] columnKeys = { "襄城", "樊城", "襄州", "东津","鱼梁州" };
282???????? CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
283???????????????? rowKeys, columnKeys, data);
284???????? JFreeChart chart = ChartFactory.createBarChart3D("襄阳各区肉类销量统计图", "地区",
285???????????????? "销量", dataset, PlotOrientation.VERTICAL, true, true, false);
286
287???????? CategoryPlot plot = chart.getCategoryPlot();
288
289???????? //设置网格背景颜色
290???????? plot.setBackgroundPaint(Color.white);
291???????? //设置网络竖线颜色
292???????? plot.setDomainGridlinePaint(Color.pink);
293???????? //显示每个柱的数值,并修改该数字的字体属性
294???????? BarRenderer3D renderer = new BarRenderer3D();
295???????? renderer
296???????????????? .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
297???????? renderer.setBaseItemLabelsVisible(true);
298???????? //默认的数字显示在柱子中,通过以下两句调整数字的显示
299 //注意,此句很关键,若无此句,那数字的显示会覆盖,给人数字没有显示出来的问题
300???????? renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
301???????????????? ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
302???????? renderer.setItemLabelAnchorOffset(10D);
303???????? //设置每个地区所包含的平行柱的之间的距离
304 //renderer.setItemMargin(0.3);
305???????? plot.setRenderer(renderer);
306
307???????? //设置地区、销量的显示位置
308 //将下方的“肉类”放到上方
309???????? plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
310???????? //将默认的左边的“销量”放到右方
311???????? plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
312???????? NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
313???????? CategoryAxis domainAxis = plot.getDomainAxis();
314???????? //图表标题以及副标题乱码
315???????? Font font = new Font("宋体", Font.BOLD, 16);
316???????? TextTitle title = new TextTitle("襄阳", font);//副标题
317???????? TextTitle subtitle = new TextTitle("肉类销量统计图", new Font("黑体", Font.BOLD,
318???????????????? 12));
319???????? chart.addSubtitle(subtitle);//子标题
320???????? chart.setTitle(title); //标题
321
322 //X轴乱码
323 //X轴坐标上的文字:
324???????? domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
325???????? //X轴坐标标题(肉类)
326???????? domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
327???????? //Y轴坐标上的文字
328???????? numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
329???????? //Y轴坐标标题(销量):
330???????? numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
331???????? //图表底部乱码(猪肉等文字)
332???????? chart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 12));
333???????? String filename = ServletUtilities
334???????????????? .saveChartAsPNG(chart, 700, 400, null);
335???????? return filename;
336???? }
337 }


3 result



bar1的运行截图:

访问的URL为:http://localhost:8080/JFreeChart/BarServlet?num=1

image

bar2的运行截图:

访问的URL为:http://localhost:8080/JFreeChart/BarServlet?num=2

image

bar3的运行截图:

访问的URL为:http://localhost:8080/JFreeChart/BarServlet?num=3

image

bar4的运行截图:

访问的URL为:http://localhost:8080/JFreeChart/BarServlet?num=4

image

说明:以上代码按照图片建立目录结构,导入jfreechart的jar包,把代码拷贝到文件中即可以直接使用,只是如果有需要的,拷贝时请不要直接拷贝web.xml中的web-app标签的生命以及定义的属性信息,这个可能会因为环境引起错误。

?

热点排行