因为我所在的项目需要用到动态生成显示图片,于是研究了一点,先把源代码以及完整的例子贴出来。
(1)画柱状图
JAVA源代码:
/**
*@author qingbao-gao
*<p>Blog:http://www.cnweblog.com/nm1504</p>
*<p>E-mail:yyk1504@163.com</p>
*<p>创建时间:2008-1-3</p>
*<p>Copyright: XXxxxXX (c)2008-1-3</p>
*/
package com.mfsoft.net.jfreemap;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.Color;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import com.mfsoft.credit.modelmang.operation.EvalItemDataOperation;
import com.mfsoft.credit.modelmang.operation.ModelDataTypeOperation;
import com.mfsoft.net.pre.MoveWeightAverage;
import com.mfsoft.net.pre.TimeSeriesForecast;
public class DisplayMap
{
public DisplayMap()
{
}
/**
* <P>获得图形</P>
* @param test 数据存储数组
* @param polyLineResult 柱状图要显示的柱形
* @param simuResult 柱状图要显示的柱形
* @param moveResult 柱状图要显示的柱形
* @param timeResult 柱状图要显示的柱形
* @author qingbao-gao
* <p>Date:2008-01-03 PM 2:21</p>
* @return JFreeChart形式的对象供调用
*/
public static JFreeChart displayResult(double[][]test,int polyLineResult,int simuResult,int moveResult,int timeResult)
{
int length=test.length;
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//double [][]test=mdt.getMonthData("200609","200708","1100000000023","R");
/*
double[][]test=new double[][]{
{200606 ,63534},
{200607 ,64624},
{200608 ,64947},
{200609 ,64996},
{200610 ,65201},
{200611 ,65318},
{200612 ,65800},
{200701 ,66126},
{200702 ,66238},
{200703 ,66553},
{200704 ,66790},
{200705 ,67125},
{200706 ,67461},
{200707 ,67833},
{200708 ,68064},
{200709 ,68329},
};*/
//int taps=4;
for(int i=0;i<test.length-1;i++)
{
dataset.addValue(test[i][1], "各月份数据", (String.valueOf(test[i][0])).substring(2, 7));
//dataset.addValue(450, "各月份数据", "2月份");
//dataset.addValue(550, "各月份数据", "3月份");
//dataset.addValue(600, "各月份数据", "4月份");
}
EvalItemDataOperation ei=new EvalItemDataOperation();
String nextMonth=ei.getNextMonth(String.valueOf(test[length-1][0]));
dataset.addValue(simuResult, "神经网络预测", nextMonth);
dataset.addValue(timeResult, "时间序列预测", nextMonth);
dataset.addValue(moveResult, "移动加权平均", nextMonth);
dataset.addValue(polyLineResult, "曲线拟合预测", nextMonth);
JFreeChart chart = ChartFactory.createBarChart3D("时间序列柱状图-四种预测方法比较","预测显示比较","实际客
户数与预测客户数",dataset,PlotOrientation.VERTICAL,true,false,false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot catePlot = chart.getCategoryPlot();
CategoryAxis domainAxis = catePlot.getDomainAxis();
//domainAxis.setVerticalCategoryLabels(false);
catePlot.setDomainAxis(domainAxis);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
//显示每个柱的数值,并修改该数值的字体属性
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
catePlot.setRenderer(renderer);
//设置柱的透明度
catePlot.setForegroundAlpha(0.8f);
return chart;
}
}
下面是JSP里调用上面的类实现图片显示的JSP文件
<%@page contentType="text/html; charset=GB2312"%>
<%@page import="org.jfree.chart.ChartUtilities"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="com.mfsoft.net.jfreemap.DisplayMap" %>
<%@page contentType="text/html; charset=GB2312"%>
<%@page import="com.mfsoft.credit.modelmang.operation.EvalItemDataOperation" %>
<%@page import="com.mfsoft.net.jfreemap.*" %>
<%@page import="com.mfsoft.credit.modelmang.operation.ModelDataTypeOperation" %>
<html>
<head>
<title>柱状图显示结果</title>
</head>
<body>
<%
String timeType=(String)request.getParameter("timeType");
String areaid=(String)request.getParameter("areaid");
String areaType=(String)request.getParameter("areaType");
String endTime=EvalItemDataOperation.getEndYearsToday();
String startTime=EvalItemDataOperation.getNYearsToday(endTime,1);
int lineScore=55000;
int nnetScore=45632;
int timeScore=50000;
int moveWeightScore=60000;
nnetScore=Integer.valueOf((String)request.getParameter("nnetScore"));
lineScore=Integer.valueOf((String)request.getParameter("lineScore"));
timeScore=Integer.valueOf((String)request.getParameter("timeScore"));
moveWeightScore=Integer.valueOf((String)request.getParameter("moveWeightScore"));
MoveWeightLineMap mp=new MoveWeightLineMap();
double[][]st={{}};
if(timeType!=null&&areaid!=null&&areaType!=null)
{
if(timeType.equals("0"))
{
st=ModelDataTypeOperation.getMonthData(startTime,endTime,areaid,areaType);
}
else
if(timeType.equals("1"))
{
st=ModelDataTypeOperation.getThirdYearData(startTime,endTime,areaid,areaType);
}
else
if(timeType.equals("2"))
{
st=ModelDataTypeOperation.getHalfYearData(startTime,endTime,areaid,areaType);
}
else
{
st=ModelDataTypeOperation.getYearData(startTime,endTime,areaid,areaType);
}
DisplayMap dm=new DisplayMap();
JFreeChart chart=dm.displayResult(st,lineScore, nnetScore, moveWeightScore, timeScore);
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),chart,1000,600);
}
else
{
out.print("传递过来的值为空,不能显示柱状图");
}
%>
</body>
</html>