jfreechart 生成折线图例子

<div id="demo">
<%//从数据库读取属性
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url1="jdbc:oracle:thin:@128.1.1.1:1521:bjmis";
String usr1="bjmis_dba";
String pwd1="seatrust";
Connection conn1=DriverManager.getConnection(url1,usr1,pwd1);
Statement stmt0 = conn1.createStatement();
String sql0="select * from DIAGRAM";
ResultSet rs0 = stmt0.executeQuery(sql0);
String color0=null;
String isShowGredLine0 = null;
String refreshTime0 = "100";
while(rs0.next()){
  color0=rs0.getString("BACKCOLOR");
  isShowGredLine0 = rs0.getString("SHOWGRID");
  refreshTime0=rs0.getString("REFRESHSPAN");
}
SetPropertyDao dao = (SetPropertyDao)request.getAttribute("parameters");
Color convertedColor = Color.ORANGE;
if(dao==null){
 convertedColor = new Color(Integer.parseInt(color0,10));//把数据库的整型数据的颜色转换成rgb形式的
}else{
    try {
      convertedColor = new Color(Integer.parseInt(dao.getBgColor(),10));//把数据库的整型数据的颜色转换成rgb形式的
    } catch(NumberFormatException e) {
        // codes to deal with this exception
    }
}
%>
<div id="1">
<%
//连接数据库
Connection conn=null;
TimeSeries timeseries=null;//下限
TimeSeries timeseries1=null;//平均值
TimeSeries timeseries2=null;//最大值
TimeSeries timeseries11=null;//上限
TimeSeries timeseriesMin=null;//最小值
//try{document.getElementById("radioByDayOrHour").value=="1"
//Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//String url="jdbc:oracle:thin:@128.1.1.1:1521:BJMIS";
//String usr="bjmis_dba";
//String pwd="seatrust";
//conn=DriverManager.getConnection(url,usr,pwd);
//Statement stmt=conn.createStatement();
//String orclsql="select VALUE,OP_DATE from bjmis_dba.node_history where detector='01010306' order by op_date";
//String orclsql="select to_char(op_date,'yyyy-MM-dd') as groupDate,Avg(value) as avgValue from bjmis_dba.node_history where detector='01010101' group by to_char(op_date,'yyyy-MM-dd')";
//ResultSet rs=stmt.executeQuery(orclsql);
ArrayList nodeInfoList = (ArrayList)request.getAttribute("nodeInfo");
ArrayList nodeMaxList = (ArrayList)request.getAttribute("nodeMaxInfo");
ArrayList nodeMinList = (ArrayList)request.getAttribute("nodeMinInfo");
//设置曲线,共三条曲线
timeseries = new TimeSeries("最小值",Day.class);//时间的最小单位为分,线值名是“平均”
timeseries1 = new TimeSeries("平均值曲线",Day.class);
timeseries2 = new TimeSeries("最大值",Day.class);
timeseries11 = new TimeSeries("最大值曲线",Day.class);
timeseriesMin= new TimeSeries("最小值曲线",Day.class);
//TimeSeries timeseries3 = new TimeSeries("第二条曲线",Minute.class);
//从数据库中取值,数据和时间
//if(forma.RadioButton("gender", 1,true)){
 //}

if(nodeInfoList!=null){//&&nodeMaxList==null&&nodeMinList==null)){
Iterator nodeIterator = nodeInfoList.iterator();
//Iterator nodemaxIterator = nodeMaxList.iterator();
//Iterator nodeminIterator = nodeMinList.iterator();
while(nodeIterator.hasNext())//&&nodemaxIterator.hasNext()&&nodeminIterator.hasNext())
{
 ShowDayNodeChartDao shownodeInfos = (ShowDayNodeChartDao)nodeIterator.next(); 
 //ShowDayNodeChartDao showmaxnodeInfo=(ShowDayNodeChartDao)nodemaxIterator.next();
 //ShowDayNodeChartDao showminnodeInfo=(ShowDayNodeChartDao)nodeminIterator.next();
float y0=-50;
Number y1= (Number)shownodeInfos.getAvgValue();
//Number y11 = (Number)showmaxnodeInfo.getDayMaxValue();
//Number y22 = (Number)showminnodeInfo.getDayMinValue();
float y2=400;
String s = shownodeInfos.getAvgOpdate();
//out.println(y22);
//out.println(y1);
//out.println(s);
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
//ParsePosition pos = new ParsePosition(0);
//String s1=sdf.format(sdf.parse(s)).toString();
//从yyyyMMddHHmm时间格式的char中取出年月日小时分设为int值
int x=Integer.parseInt(s.substring(0,4));
int y=Integer.parseInt(s.substring(5,7));
int z=Integer.parseInt(s.substring(8,10));
//int xx= Integer.parseInt(s.substring(11,13));
//int yy=Integer.parseInt(s.substring(14,16));
if((nodeMinList==null&&nodeMaxList==null)||(nodeMinList!=null&&nodeMaxList!=null)||
  (nodeMinList!=null&&nodeMaxList==null)||(nodeMinList==null&&nodeMaxList!=null)){
 //timeseries.add(new Day(z,y,x),y0);
 // timeseries2.add(new Day(z,y,x),y2);
}
timeseries1.add(new Day(z,y,x),y1);//
//timeseries1.add(new Day(z,y,x),y1);
//timeseries11.add(new Minute(yy,xx,z,y,x),y11);//最大值曲线
//timeseriesMin.add(new Minute(yy,xx,z,y,x),y22);//最小值曲线
}
}
if(nodeMaxList!=null){
 Iterator nodemaxIterator = nodeMaxList.iterator();
 while(nodemaxIterator.hasNext()){
  ShowDayNodeChartDao showmaxnodeInfo=(ShowDayNodeChartDao)nodemaxIterator.next();
  float y0=-50;
     Number y11 = (Number)showmaxnodeInfo.getDayMaxValue();
     //out.println(y11);
     float y2=400;
     String s = showmaxnodeInfo.getDayMaxDate();
     //out.println(s);
     //out.println(y22);
     //out.println(y11);
     //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
     //ParsePosition pos = new ParsePosition(0);
     //String s1=sdf.format(sdf.parse(s)).toString();
     //从yyyyMMddHHmm时间格式的char中取出年月日小时分设为int值
     int x=Integer.parseInt(s.substring(0,4));
     int y=Integer.parseInt(s.substring(5,7));
     int z=Integer.parseInt(s.substring(8,10));
     //int xx= Integer.parseInt(s.substring(11,13));
     //int yy=Integer.parseInt(s.substring(14,16));
     if((nodeInfoList==null&&nodeMinList==null)||(nodeInfoList==null&&nodeMinList!=null)){
      //timeseries.add(new Day(z,y,x),y0);
      // timeseries2.add(new Day(z,y,x),y2);
     }
     timeseries11.add(new Day(z,y,x),y11);//最大值曲线
 }
}
if(nodeMinList!=null){
 Iterator nodeminIterator = nodeMinList.iterator();
 while(nodeminIterator.hasNext()){
  ShowDayNodeChartDao showminnodeInfo=(ShowDayNodeChartDao)nodeminIterator.next();
  float y0=-50;
  Number y22 = (Number)showminnodeInfo.getDayMinValue();
     float y2=400;
     String s = showminnodeInfo.getDayMinDate();
     //从yyyyMMddHHmm时间格式的char中取出年月日小时分设为int值
     int x=Integer.parseInt(s.substring(0,4));
     int y=Integer.parseInt(s.substring(5,7));
     int z=Integer.parseInt(s.substring(8,10));
     //int xx= Integer.parseInt(s.substring(11,13));
     //int yy=Integer.parseInt(s.substring(14,16));
     if((nodeInfoList==null&&nodeMaxList==null)){
      //timeseries.add(new Day(z,y,x),y0);
      // timeseries2.add(new Day(z,y,x),y2);
     }
     timeseriesMin.add(new Day(z,y,x),y22);//最小值曲线
 }
}
//}catch(ClassNotFoundException cnfex){ // 捕获加载驱动程序异常
 //cnfex.printStackTrace();
//}catch (SQLException sqlex) {// 捕获连接数据库异常
 //sqlex.printStackTrace();
  //}
//finally{
 //conn.close();//关闭数据库连接
//}

//连接曲线
TimeSeriesCollection dataset = new TimeSeriesCollection();
//dataset.addSeries(timeseries);
dataset.addSeries(timeseries1);
//dataset.addSeries(timeseries2);
dataset.addSeries(timeseries11);
dataset.addSeries(timeseriesMin);

//设置曲线图
XYDataset xydataset = (XYDataset) dataset;
String title="";
String domin="时间";
String range="值";

//XYDataset xydataset1 = (XYDataset) dataset1;
String title1="";
String domin1="时间1";
String range1="值1";

JFreeChart chart = ChartFactory.createTimeSeriesChart(
title,
domin,
range,
xydataset,
true,
true,
false
);
dataset.setDomainIsPointsInTime(true); //x轴上的刻度点代表的是时间点而不是时间段
//JFreeChart chart1 = ChartFactory.createTimeSeriesChart(
//title1,
///domin1,
//range1,
//xydataset1,
//true,
//true,
//true
//);
chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 22)));//设置标题的字体
//chart1.setTitle(new TextTitle(title1, new Font("隶书", Font.ITALIC, 22)));//设置标题的字体
TextTitle subtitle = new TextTitle("按天数", new Font("黑体", Font.BOLD, 12));//增加子标题
//TextTitle subtitle1 = new TextTitle("按天数", new Font("黑体", Font.BOLD, 12));//增加子标题
subtitle.setPaint(Color.white);
chart.addSubtitle(subtitle);//增加子标题
//chart1.addSubtitle(subtitle1);
//下面注释的文字的作用是将文字的抗锯齿参数关闭
//chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
//chart.setBackgroundPaint(new Color(239, 251, 255));//设置曲线图背景色
chart.setBackgroundPaint(convertedColor);
//chart1.setBackgroundPaint(new Color(189, 235, 255));
XYPlot plot = (XYPlot) chart.getPlot();

//plot.addRangeMarker(new ValueMarker(5D)); //11.26
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
//XYPlot plot1 =(XYPlot)chart1.getPlot();
ValueAxis dominAxis = plot.getDomainAxis();//得到x轴标题文字
ValueAxis rangeAxis = plot.getRangeAxis();//得到y轴标题文字
rangeAxis.setLabelPaint(Color.white);//设置y轴标题颜色11.26
dominAxis.setLabelPaint(Color.white);//设置x轴标题颜色11.26
rangeAxis.setAutoRange(true);//自动设置数据范围
rangeAxis.setTickLabelsVisible(true);
//ValueAxis dominAxis1 = plot1.getDomainAxis();//得到x轴标题文字
//ValueAxis rangeAxis1 = plot1.getRangeAxis();//得到y轴标题文字
//rangeAxis1.setRange(10,1100);

dominAxis.setLabelFont(new Font("隶书",Font.PLAIN,15));//设置x轴标题文字
rangeAxis.setLabelFont(new Font("隶书",Font.PLAIN,15));//设置y轴标题文字

//dominAxis1.setLabelFont(new Font("隶书",Font.PLAIN,15));//设置x轴标题文字
//rangeAxis1.setLabelFont(new Font("隶书",Font.PLAIN,15));//设置y轴标题文字
//rangeAxis1.setLowerBound(100);设置y轴的最大值与最小值
//rangeAxis1.setUpperBound(1000);

dominAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,12));//设置x轴坐标上的文字
dominAxis.setTickLabelPaint(Color.white);//设置x轴字体颜色11.26
rangeAxis.setTickLabelPaint(Color.white);//设置y轴字体颜色11.26
//dominAxis1.setTickLabelFont(new Font("黑体",Font.PLAIN,12));//设置x轴坐标上的文字
XYPlot xyplot = (XYPlot)chart.getXYPlot();
DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();

//设置xy轴的格式
//NumberAxis yrangeaxis = (NumberAxis)xyplot.getRangeAxis();//10  30
//yrangeaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //10 30
dateaxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));//设置日期格式 注意顺序
xyplot.setDomainAxis(dateaxis);
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,1,new SimpleDateFormat("yyyy-MM-dd"))); //每隔一天显示一个刻度
dateaxis.setAutoTickUnitSelection(false);
dateaxis.setVerticalTickLabels(true);//横轴的日期垂直显示
Calendar cal = Calendar.getInstance();
//dateaxis.setMaximumDate(new Date(cal.get(Calendar.YEAR)-1900,cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE)));
daysMinus= Integer.parseInt(String.valueOf(request.getAttribute("dayMinus")));
dateaxis.setMaximumDate(new Date(cal.get(Calendar.YEAR)-1900,cal.get(Calendar.MONTH),
  cal.get(Calendar.DAY_OF_MONTH)+1));
//dateaxis.setMinimumDate(new Date(cal.get(Calendar.YEAR)-1900,cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),cal.get(Calendar.HOUR_OF_DAY)-10/24,cal.get(Calendar.MINUTE)));
dateaxis.setMinimumDate(new Date(cal.get(Calendar.YEAR)-1900,cal.get(Calendar.MONTH),
  cal.get(Calendar.DAY_OF_MONTH)-daysMinus));
//out.println(daysMinus);
//XYPlot xyplot1 = (XYPlot)chart1.getXYPlot();
//DateAxis dateaxis1 = (DateAxis)xyplot1.getDomainAxis();
//dateaxis1.setDateFormatOverride(new SimpleDateFormat("MM:HH"));//设置时间格式 注意顺序和大小写
//xyplot1.setRangeAxis(rangeAxis1);

//SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");日期格式错误应该是MM-dd-yyyy
LegendTitle legend = chart.getLegend();  //设置图下面的小方框中的字体
Font labelFont = new Font("宋体", Font.BOLD, 12);  //设置图下面的小方框中的字体
legend.setItemFont(labelFont); //设置图下面的小方框中的字体
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();

//LegendTitle legend1 = chart1.getLegend();  //设置图下面的小方框中的字体
Font labelFont1 = new Font("宋体", Font.BOLD, 12);  //设置图下面的小方框中的字体
//legend1.setItemFont(labelFont1); //设置图下面的小方框中的字体
//xylineandshaperenderer.setShapesVisible(false);//不显示数据点
//plot.setBackgroundPaint(new Color(239, 251, 255));//设置网格背景颜色
plot.setDomainGridlinePaint(Color.black);//设置网格竖线颜色
plot.setRangeGridlinePaint(Color.black);//设置网格横线颜色

String isShowGredLine=null;
if(dao==null){
 isShowGredLine = isShowGredLine0;
}else{
 isShowGredLine = dao.getIsShowGridLine().toString();
}
if(isShowGredLine.equals("1")){
 plot.setDomainGridlinesVisible(true);
 plot.setRangeGridlinesVisible(true);
}else{
 plot.setDomainGridlinesVisible(false);
 plot.setRangeGridlinesVisible(false);
}
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));//设置曲线图与xy轴的距离
//xylineandshaperenderer.setBaseShapesVisible(false);//设置曲线是否显示数据点
//plot1.setDomainGridlinePaint(Color.blue);//设置网格竖线颜色
//plot1.setRangeGridlinePaint(Color.blue);//设置网格横线颜色
if(isShowGredLine.equals("1")){
 //plot1.setDomainGridlinesVisible(true);
 //plot1.setRangeGridlinesVisible(true);
}else{
 //plot1.setDomainGridlinesVisible(false);
 //plot1.setRangeGridlinesVisible(false);
}
//plot1.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));//设置曲线图与xy轴的距离
xylineandshaperenderer.setBaseShapesVisible(true);//设置曲线是否显示数据点
xylineandshaperenderer.setSeriesPaint(0,new Color(0,0,0));//设置各条曲线的颜色 黑色
xylineandshaperenderer.setSeriesPaint(1,new Color(0,0,255)); //蓝色
xylineandshaperenderer.setSeriesPaint(2,new Color(255,0,0));//红色
xylineandshaperenderer.setSeriesPaint(3,new Color(0,255,255));//青色
xylineandshaperenderer.setSeriesPaint(4,new Color(153,204,255));//天蓝色
String filename = ServletUtilities.saveChartAsPNG(chart,650 ,350, null, session);
//从数据库读出刷新间隔
//String refreshTime="100";
if(dao==(null)){
 refreshTime = refreshTime0;
}
else{
 refreshTime = dao.getRefeshInteval();
}

//String filename1 = ServletUtilities.saveChartAsPNG(chart1, 500, 300, null, session);
 String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
//String graphURL1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename1;

%>
<img src="<%= graphURL %>" border=0 usemap="#<%= filename %>">
</div>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值