jfreechart绘制折线图(平面和3D)

   对于jfreechart绘制折线图的学习,提供了一个经典例子,在jsp页面能看到图片。

  项目工程下载:http://download.csdn.net/detail/njupt_t/9404596

  首页面:


代码 index.jsp:

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML>
<html>
  <head>
    <title>智能手机软件下载数量统计</title>
    <style type="text/css">
    *{font-size: 15px}
    </style>
  </head>
  <body>
    <div align="center">
    <h1>查看统计报表</h1>
    <a href="ChartServlet">普通样式</a>
    <a href="ChartServlet?style=3d">3D样式</a>
    </div>
  </body>
</html>

平面图:


3d:


代码result.jsp:

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML>
<html>
  <head>
    <head>
    <title>优秀杀毒软件杀毒数量统计</title>
  </head>
  <body>
  <div align="center">
    <img src="${graphURL}" border="1">
    <br><br>
    <a href="index.jsp">返回</a>
    </div>
  </body>
</html>

servlet代码:

ChartServlet.java

package com.wgh.service;


import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.jfree.chart.servlet.ServletUtilities;


import com.wgh.util.ChartUtil;
/**
 * 制图Servlet类,用于获取图片
 */
@WebServlet("/ChartServlet")
public class ChartServlet extends HttpServlet {
private static final long serialVersionUID = -3308103803568844498L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 显示样式(是否为3D效果)
String style = request.getParameter("style");
String fileName = null; // 生成图片的文件名
if(style != null && "3d".equals(style)){
// 获取生成图片的名称
fileName = ServletUtilities.saveChartAsJPEG(
ChartUtil.createChart(true), 500, 300, request.getSession());
}else{
fileName = ServletUtilities.saveChartAsJPEG(
ChartUtil.createChart(false), 500, 300, request.getSession());
}
// 获取图片的路径
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;
// 将路径放到request对象中
request.setAttribute("graphURL", graphURL);
// 页面转发到result.jsp
request.getRequestDispatcher("result.jsp").forward(request, response);
}


}

ChartUtil.java:


package com.wgh.util;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;


/**
 * 自定义制图工具类
 */
public class ChartUtil {
//字体
private static final Font PLOT_FONT = new Font("宋体", Font.BOLD, 15);
/**
* 创建数据集合
* @return CategoryDataset对象
*/
public static CategoryDataset createDataSet() {
//图例名称
String[] line = { "杀毒软件一", "杀毒软件二", "杀毒软件三" };
//类别
String[] category = { "2004年","2005年", "2006年", "2007年", "2008年" };
Random random = new Random(); //实例化Random对象
//实例化DefaultCategoryDataset对象
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
//使用循环向数据集合中添加数据
for (int i = 0; i < line.length; i++) {
for (int j = 0; j < category.length; j++) {
dataSet.addValue(100000 + random.nextInt(100000), line[i],
category[j]);
}
}
return dataSet;
}
/**
* 生成制图对象
* @param is3D 是否为3D效果
* @return JFreeChart对象
*/
public static JFreeChart createChart(boolean is3D) {
JFreeChart chart = null;
if(is3D){
chart = ChartFactory.createLineChart3D(
"2004-2008年优秀杀毒软件杀毒数量统计", //图表标题
"杀毒软件", //X轴标题
"查杀病毒数量", //Y轴标题
createDataSet(), //绘图数据集
PlotOrientation.VERTICAL, //绘制方向
true, //显示图例
true, //采用标准生成器
false //是否生成超链接
);
}else{
chart = ChartFactory.createLineChart("2004-2008年优秀杀毒软件杀毒数量统计", //图表标题
"杀毒软件", //X轴标题
"查杀病毒数量", //Y轴标题
createDataSet(), //绘图数据集
PlotOrientation.VERTICAL, //绘制方向
true, //是否显示图例
true, //是否采用标准生成器
false //是否生成超链接
);
}

//设置标题字体
chart.getTitle().setFont(new Font("隶书", Font.BOLD, 23));
//设置图例类别字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
chart.setBackgroundPaint(new Color(192,228,106)); //设置背景色
//获取绘图区对象
CategoryPlot plot = chart.getCategoryPlot();
plot.getDomainAxis().setLabelFont(PLOT_FONT); //设置横轴字体
plot.getDomainAxis().setTickLabelFont(PLOT_FONT); //设置坐标轴标尺值字体
plot.getRangeAxis().setLabelFont(PLOT_FONT); //设置纵轴字体
plot.setBackgroundPaint(Color.WHITE); //设置绘图区背景色
plot.setRangeGridlinePaint(Color.RED); //设置水平方向背景线颜色
plot.setRangeGridlinesVisible(true); //设置是否显示水平方向背景线,默认值为true
plot.setDomainGridlinePaint(Color.RED); //设置垂直方向背景线颜色
plot.setDomainGridlinesVisible(true); //设置是否显示垂直方向背景线,默认值为false
//获取折线对象
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
                    .getRenderer();
BasicStroke realLine = new BasicStroke(1.6f); //设置实线
float dashes[] = { 8.0f }; //定义虚线数组
BasicStroke brokenLine = new BasicStroke(1.6f, //线条粗细
BasicStroke.CAP_SQUARE, //端点风格
BasicStroke.JOIN_MITER, //折点风格
8.f, //折点处理办法
dashes, //虚线数组
0.0f); //虚线偏移量
renderer.setSeriesStroke(1, brokenLine); //利用虚线绘制
renderer.setSeriesStroke(2, brokenLine); //利用虚线绘制
renderer.setSeriesStroke(3, realLine); //利用实线绘制
return chart;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值