JFreeChart参考资料
http://apps.hi.baidu.com/share/detail/33782477 阿蜜果blog
http://phl.iteye.com/blog/717900 jfreechart 资料
http://www.blogjava.net/huyi0616/archive/2009/05/26/278046.html 日期轴范围
DateAxis(ValueAxis)类:
void setMaximumDate(Date maximumDate)日期轴上的最小日期
void setMinimumDate(Date minimumDate)日期轴上的最大日期
void setRange(Date lower,Date upper)日期轴范围
void setDateFormatOverride(DateFormat formatter)日期轴日期标签的显示格式
void setTickUnit(DateTickUnit unit)日期轴的日期标签(需要将AutoTickUnitSelection设false)
http://book.51cto.com/art/201001/181553.htm JFreeChart绘制折线图 书上的 杀毒软件
http://zhidao.baidu.com/question/238084369.html JDBC 取值
http://blog.csdn.net/gongtao200118/article/details/1882932 JDBC 取值例子(全) 共14篇
http://blog.csdn.net/gongtao200118/article/details/1885051 JFreeChart 心得 涉及时间处理
http://developer.51cto.com/art/201112/308882.htm JFreeChart最佳实践:折线图
1 import java.awt.BasicStroke; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Random; 7 8 import org.jfree.chart.ChartFactory; 9 import org.jfree.chart.JFreeChart; 10 import org.jfree.chart.axis.DateAxis; 11 import org.jfree.chart.axis.DateTickUnit; 12 import org.jfree.chart.labels.ItemLabelAnchor; 13 import org.jfree.chart.labels.ItemLabelPosition; 14 import org.jfree.chart.labels.StandardXYItemLabelGenerator; 15 import org.jfree.chart.labels.StandardXYToolTipGenerator; 16 import org.jfree.chart.plot.XYPlot; 17 import org.jfree.chart.renderer.xy.XYItemRenderer; 18 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 19 import org.jfree.chart.title.TextTitle; 20 import org.jfree.data.time.Hour; 21 import org.jfree.data.time.TimeSeries; 22 import org.jfree.data.time.TimeSeriesCollection; 23 import org.jfree.ui.TextAnchor;
1 public class ChartUtil { 2 private static final Font PLOT_FONT = new Font("宋体", Font.BOLD, 15); 3 /** 4 * 创建数据集合 5 * @return CategoryDataset对象 6 */ 7 public static TimeSeriesCollection createDataSet() { 8 /* 9 //图例名称 10 String[] line = { "2012-04-24", "2012-04-25", "2012-04-26" }; 11 //类别 12 String[] category = { "2004年","2005年", "2006年", "2007年", "2008年" }; 13 Random random = new Random(); //实例化Random对象 14 //实例化DefaultCategoryDataset对象 15 DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); 16 //使用循环向数据集合中添加数据 17 for (int i = 0; i < line.length; i++) { 18 for (int j = 0; j < category.length; j++) { 19 dataSet.addValue(100000 + random.nextInt(100000), line[i], category[j]); 20 } 21 } 22 */ 23 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 24 25 26 27 int year = c.get(Calendar.YEAR); 28 int month = c.get(Calendar.MONTH); 29 int day = c.get(Calendar.DATE); 30 int hour = c.get(Calendar.HOUR_OF_DAY); 31 int minute = c.get(Calendar.MINUTE); 32 int second = c.get(Calendar.SECOND); 33 // System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); 34 35 36 TimeSeries[] timeSeries = new TimeSeries[3]; 37 timeSeries[0] = new TimeSeries("today", Hour.class); 38 timeSeries[1] = new TimeSeries("yesterday", Hour.class); 39 timeSeries[2] = new TimeSeries("twodaysbefore", Hour.class); 40 //访问量统计时间线 41 //TimeSeries today = new TimeSeries("today", Hour.class); 42 //TimeSeries yesterday = new TimeSeries("yesterday", Hour.class); 43 44 //时间曲线数据集合 45 TimeSeriesCollection lineDataset = new TimeSeriesCollection(); 46 Random random = new Random(); 47 //Day localDay = new Day(); 48 for(int i=0;i<3;i++){ 49 for(int j=1;j<=24;j=j+2){ 50 timeSeries[i].addOrUpdate(new Hour(j, day , month, year), 5000+random.nextInt(5000)); 51 } 52 lineDataset.addSeries(timeSeries[i]); 53 } 54 55 //构造数据集合 56 /* 57 yesterday.add(new Month(1, 2007), 10200); 58 yesterday.add(new Month(2, 2007), 9000); 59 yesterday.add(new Month(3, 2007), 6200); 60 yesterday.add(new Month(4, 2007), 8200); 61 yesterday.add(new Month(5, 2007), 8200); 62 yesterday.add(new Month(6, 2007), 11200); 63 yesterday.add(new Month(7, 2007), 13200); 64 yesterday.add(new Month(8, 2007), 8300); 65 yesterday.add(new Month(9, 2007), 10400); 66 yesterday.add(new Month(10, 2007), 12500); 67 yesterday.add(new Month(11, 2007), 10600); 68 yesterday.add(new Month(12, 2007), 10500); 69 lineDataset.addSeries(yesterday); 70 71 */ 72 73 74 //JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true); 75 76 77 return lineDataset; 78 } 79 /** 80 * 生成制图对象 81 * @param is3D 是否为3D效果 82 * @return JFreeChart对象 83 */ 84 public static JFreeChart createChart(boolean is3D) { 85 JFreeChart chart = null; 86 87 chart = ChartFactory.createTimeSeriesChart 88 ("Pending messages for past three days", //图表标题 89 "Time", //X轴标题 90 "Messages", //Y轴标题 91 createDataSet(), //绘图数据集 92 true, //是否显示图例 93 true, //是否采用标准生成器 94 false //是否生成超链接 95 ); 96 97 98 /* 99 //设置标题字体 100 chart.getTitle().setFont(new Font("隶书", Font.BOLD, 23)); 101 //设置图例类别字体 102 chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15)); 103 chart.setBackgroundPaint(new Color(192,228,106)); //设置背景色 104 105 //获取绘图区对象 106 CategoryPlot plot = chart.getCategoryPlot(); 107 plot.getDomainAxis().setLabelFont(PLOT_FONT); //设置横轴字体 108 109 plot.getDomainAxis().setTickLabelFont(PLOT_FONT);//设置坐标轴标尺值字体 110 111 plot.getRangeAxis().setLabelFont(PLOT_FONT); //设置纵轴字体 112 113 plot.setBackgroundPaint(Color.WHITE); //设置绘图区背景色 114 115 plot.setRangeGridlinePaint(Color.RED); //设置水平方向背景线颜色 116 117 plot.setRangeGridlinesVisible(true); //设置是否显示水平方向背景线,默认值为true 118 119 plot.setDomainGridlinePaint(Color.RED); //设置垂直方向背景线颜色 120 121 plot.setDomainGridlinesVisible(true); //设置是否显示垂直方向背景线,默认值为false 122 123 //获取折线对象 124 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); 125 126 BasicStroke realLine = new BasicStroke(1.6f); //设置实线 127 float dashes[] = { 8.0f }; //定义虚线数组 128 BasicStroke brokenLine = new BasicStroke(1.6f, //线条粗细 129 BasicStroke.CAP_SQUARE, //端点风格 130 BasicStroke.JOIN_MITER, //折点风格 131 8.f, //折点处理办法 132 dashes, //虚线数组 133 0.0f); //虚线偏移量 134 renderer.setSeriesStroke(1, brokenLine); //利用虚线绘制 135 136 renderer.setSeriesStroke(2, brokenLine); //利用虚线绘制 137 138 renderer.setSeriesStroke(3, realLine); //利用实线绘制 139 140 141 */ 142 XYPlot plot = (XYPlot)chart.getPlot(); 143 plot.setBackgroundPaint(Color.WHITE); //设置网格背景颜色 144 plot.setRangeGridlinePaint(Color.pink); //设置网格横线颜色 145 plot.setDomainGridlinePaint(Color.pink); //设置网格竖线颜色 146 147 //设置曲线图与xy轴的距离 148 // plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D)); 149 150 //获取折线对象 151 XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer(); 152 153 //设置曲线是否显示数据点 154 xylineandshaperenderer.setBaseShapesVisible(true); 155 156 //设置曲线显示各数据点的值 157 XYItemRenderer xyitem = plot.getRenderer(); 158 xyitem.setBaseItemLabelsVisible(true); 159 xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); 160 xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); 161 xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14)); 162 plot.setRenderer(xyitem); 163 164 //X轴为日期格式,这里是专门的处理日期的类, 165 SimpleDateFormat format = new SimpleDateFormat("HH"); 166 DateAxis dateaxis = (DateAxis) plot.getDomainAxis(); 167 dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 2, format)); //以小时为刻度,时间格式为yyyy-MM-dd,如2008-02-06 168 169 //折线的粗细调 170 StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator(); 171 xylineandshaperenderer.setToolTipGenerator(xytool); 172 xylineandshaperenderer.setStroke(new BasicStroke(1.5f)); 173 174 //设置子标题 175 TextTitle subtitle = new TextTitle("2006/2007年度访问量对比", new Font("黑体", Font.BOLD, 12)); 176 chart.addSubtitle(subtitle); 177 //设置主标题 //new Font("sans-serif", Font.PLAIN, 11 178 chart.setTitle(new TextTitle("It is amazing", new Font("sans-serif", Font.BOLD, 40))); 179 chart.setAntiAlias(true); 180 181 return chart; 182 } 183 }
index.jsp
<%@ page import="java.util.*,java.text.SimpleDateFormat" %>
<%
session.removeAttribute("JFreeChart_Deleter"); ///
Date d = new Date();
String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DATE,-2);
String towDaysBefore = sdf.format(cal1.getTime());
cal1.add(Calendar.DATE,+1);
String yesterday = sdf.format(cal1.getTime());
String today = sdf.format(Calendar.getInstance().getTime());
%>
<h2><%=towDaysBefore %> </h2>
<h2><%=yesterday %> </h2>
<h2><%=today %> </h2>
result.jsp
<img src="<%= request.getAttribute("graphURL") %>" border="1">