论坛首页 Java企业应用论坛

项目知识点总结1

浏览 3549 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-29  
    
    项目刚刚完成,里面有几个知识点是同志们共同奋斗才最终完成的,闲来无事,总结一下,以备后用。 项目采用了当前比较流行的Struts2(velocity)+Spring+Ibatis的架构

 JFreeChart配置安装 
    1、解压jfreechart_0.9.21.zip到指定位置,其中source是jfreechart的源码,jfreechart-0.9.21-demo.jar是例子程序(该部分留给大家自己去研究) 
    2、为了配置成功,我们需要关注的文件有如下三个:jfreechart-0.9.21.jar、lib\jcommon-0.9.6.jar、 lib\gnujaxp.jar 
/**
 * @disc 生成曲线图
 * @return JFreeChart
 */
private JFreeChart createCurveChart(){ 
//时间曲线数据集合 
TimeSeries timeSeries = fillDataSet(); 

TimeSeriesCollection lineDataset = new TimeSeriesCollection(); 
lineDataset.addSeries(timeSeries); 
JFreeChart chart = ChartFactory.createTimeSeriesChart("游戏点击量", "日期", "点击量", lineDataset, true, true, true); 

XYPlot plot = (XYPlot) chart.getPlot(); 
DateAxis dateaxis = (DateAxis)plot.getDomainAxis(); //对domain 轴上日期显示格式定义 
SimpleDateFormat frm = new SimpleDateFormat("yyyyMMdd"); 
dateaxis.setDateFormatOverride(frm); 
dateaxis.setLowerMargin(0.05); 
dateaxis.setUpperMargin(0.05); 
int height = 400 ; 
// 创建渐变色对象 
GradientPaint chartGP = new GradientPaint(0,0,new Color(245, 245, 245),0,height,new Color(250, 250, 250),false);     
chart.setBackgroundPaint(chartGP); // new Color(22, 255, 1) new Color(192,251,192) 

long l = compareDateTime(maxString, minString); 

long dayNumber = l/8; 

dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, (new Integer(dayNumber + "")).intValue()+1,frm));   


XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer(); 
BasicStroke realLine = new BasicStroke(2.0f); 
xylineandshaperenderer.setSeriesStroke(0,realLine); 

//设置网格背景颜色 
plot.setBackgroundPaint(Color.white); 

//设置网格竖线颜色 
plot.setDomainGridlinePaint(new Color(6,6,6)); 
//设置网格横线颜色 
plot.setRangeGridlinePaint(new Color(6,6,6)); 
//设置曲线图与xy轴的距离 
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D)); 

//设置曲线显示各数据点的值 
XYItemRenderer xyitem = plot.getRenderer();   
xyitem.setBaseItemLabelsVisible(true);   
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); 
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); 
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 8)); 

//曲线颜色:红色 
xyitem.setSeriesPaint(0,new Color(190,42,42));   
//设置曲线上数据点的类型:圆点 
        xylineandshaperenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-3.5, -3.5, 7, 7)); 
//设置曲线是否显示数据点 
xylineandshaperenderer.setBaseShapesVisible(true); 
//设置第一条曲线数据点填充为黄色,如果一个图表有多条曲线可分别设置 
xylineandshaperenderer.setSeriesFillPaint(0, new Color(255,202,0)); 
//应用填充色 
xylineandshaperenderer.setUseFillPaint(true);     
plot.setRenderer(xyitem); 

//设置子标题 
TextTitle subtitle = new TextTitle(query.getBeginDate() + " -- " + query.getEndDate(), new Font("黑体", Font.BOLD, 12)); 
chart.addSubtitle(subtitle); 
//设置主标题 
chart.setTitle(new TextTitle("游戏点击量", new Font("隶书", Font.ITALIC, 15))); 
chart.setAntiAlias(true); 
return chart; 
} 
//曲线图 

private TimeSeries fillDataSet() { 

ArrayList reports  = (ArrayList)reportStatBO.queryGameClickReport(query,"GAME_REPORT_CLICK_DAY"); 
int size = reports.size(); 
listSize = size; 
String gameName = ""; 
Integer year = new Integer(query.getBeginDate().substring(0, 4)); 
if(size>0){ 
ReportClick tempReportClick = (ReportClick)reports.get(0); 
gameName = tempReportClick.getGame_name(); 
}else{ 
return  new TimeSeries(gameName + "游戏点击报表", Day.class); 
} 
TimeSeries timeSeries = new TimeSeries(gameName+ "游戏点击报表", Day.class); 

xString = "天"; 
    timeSeries = new TimeSeries(gameName+ "游戏点击报表", Day.class); 
    String[] a = new String[size]; 
    
    int i = 0; 
    for(ReportClick reportClick:reports){ 
    
    String tmpDate = reportClick.getReport_date(); 
    a[i] = tmpDate; 
    String realDate = tmpDate.substring(0,4) + "-" +  tmpDate.substring(4,6) + "-" + tmpDate.substring(6); 
    Integer year1=new Integer(tmpDate.substring(0, 4)); 
Integer month=new Integer(tmpDate.substring(4, 6)); 
Integer day=new Integer(tmpDate.substring(6, 8)); 
timeSeries.add(new Day(day, month, year1),reportClick.getClick_count()); 
    i++; 
    } 
    Arrays.sort(a); 
maxString = a[size-1]; 
minString = a[0]; 
return timeSeries; 
} 

/**
 * @disc 生成柱状图
* @return JFreeChart
*/
@SuppressWarnings( { "deprecation", "unused" }) 
private JFreeChart createBarChart(List reports) { 

XYDataset dataset = getDataSet(reports); 
JFreeChart tmpChart = ChartFactory.createTimeSeriesChart("推广任务存活量报表", // 图表标题 
"时间", // 目录轴的显示标签 
"存活量", // 数值轴的显示标签 
dataset, // 数据集 
true, // 是否显示图例(对于简单的柱状图必须是false) 
true, // 是否生成工具 
false); // 是否生成URL链接 

XYPlot xyplot = (XYPlot) tmpChart.getPlot(); 

DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis(); // 对domain 
// 轴上日期显示格式定义 
dateaxis.setDateFormatOverride(new SimpleDateFormat("yyyyMMdd")); 
dateaxis.setLowerMargin(0.05); 
dateaxis.setUpperMargin(0.05); 
NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis(); 
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 

XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot 
.getRenderer(); 

if (reports != null && reports.size() > 0) { 
String begin = reports.get(0).getReport_date(); 
String end = reports.get(reports.size() - 1).getReport_date(); 
String beginDate = begin.substring(0, 4) + "-" 
+ begin.substring(4, 6) + "-" + begin.substring(6, 8); 
String endDate = end.substring(0, 4) + "-" + end.substring(4, 6) 
+ "-" + end.substring(6, 8); 
int day = DateUtils.compare(beginDate, endDate); 
if (day < 7) 
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1)); 
// if(reports.size()==1) 
// xylineandshaperenderer.setBaseShapesVisible(true);//数据点可见 
xylineandshaperenderer.setBaseItemLabelsVisible(true); 
// xylineandshaperenderer.setBaseOutlinePaint(Color.GRAY); 
xylineandshaperenderer 
.setItemLabelGenerator(new StandardXYItemLabelGenerator()); 
// xylineandshaperenderer.setItemLabelsVisible(true); 
} 

return tmpChart; 
} 

/** 
* 查询数据,生成JFreeChart的数据集 
*/ 
private XYDataset getDataSet(List reports) { 

if (reports == null || reports.size() < 1) 
return null; 
TimeSeries timeseries = new TimeSeries(reports.get(0).getGame_name(), 
Day.class);// 这里用的是Month.class,同样还有Day.class 
// Year.class 
// 等等 

for (ReportPushLive tmpReport : reports) { 
Integer year = new Integer(tmpReport.getReport_date().substring(0, 
4)); 
Integer month = new Integer(tmpReport.getReport_date().substring(4, 
6)); 
Integer day = new Integer(tmpReport.getReport_date() 
.substring(6, 8)); 
timeseries.add(new Day(day, month, year), tmpReport.getBar_count()); 
} 

TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); 
timeseriescollection.addSeries(timeseries); 

return timeseriescollection; 
} 
Action里面需要配置图片的宽度和高度:
<action name="reportClickChart" method="getReportClickChart" class="reportClickAction">
	<result name="success" type="chart">
		<param name="height">400</param>
		<param name="width">750</param>
	</result>			
</action>





   发表时间:2008-10-30  
有同学问我Action中的配置,8好意思,忘记写了.
在Action 中增加属性JFreeChart chart;
增加相应的get..set方法
增加方法getReportClickChart,
chart=createCurveChart();
在页面调用时引用的就是chart(就是生成的图片了) ,就OK了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics