jfreechart 线图


package realtimeMonitor;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;

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

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class MemoryUsedLineServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public MemoryUsedLineServlet() {
super();
}

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
//memoryUsedRate = RMIClient.getRMIInterface().getMemeryUsedRate("10.10.9.188");
//System.out.print("memoryUsedRate"+memoryUsedRate);
getDynamicLine(request,"MemoryUsed",Integer.parseInt(ResolveJSONForShell.resolveJSONForMemoryUsed()),"MemoryUsed");
}
catch(Exception ex)
{
ex.printStackTrace();
}


}

private static TimeSeries timeSeries;
public String getDynamicLine(HttpServletRequest request,String imageFileName,int warnValueStr,String timeSeriesName){

JFreeChart chart = null;
if((null==(timeSeries))||("".equals(timeSeries))){
timeSeries = new TimeSeries("MemoryUsed",Millisecond.class);
request.getSession().setAttribute(timeSeriesName, timeSeries);
}
timeSeries=(TimeSeries) request.getSession().getAttribute(timeSeriesName);
timeSeries.addOrUpdate(new Millisecond(), warnValueStr);
chart=createChart("MemoryUsed","MemoryUsed","",timeSeries,request);
FileOutputStream ous = null;
String filePath="";//绝对路径
String webPath="WarnImages/"+imageFileName+".png";//"+request.getSession().getId()+"
try {
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
filePath += webPath;
// System.out.println(filePath);
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (null != chart) {
ous = new FileOutputStream(filePath);
ChartUtilities.writeChartAsPNG(ous, chart, 400, 200);
//ous.flush();
}
} catch (IOException e) {
e.printStackTrace();
} // 生成图片
finally {
try {
ous.close();// 最后关闭文件流
} catch (IOException e) {
e.printStackTrace();
}
}

return webPath;
}

public static JFreeChart createChart(String chartContent,String title,String yaxisName,TimeSeries timeSeries,HttpServletRequest request){

TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeSeries);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,"",yaxisName,timeseriescollection,true,true,false);

jfreechart.getTitle().setFont(new Font("宋体", Font.BOLD,12));

XYPlot xyplot = jfreechart.getXYPlot();
xyplot.setBackgroundPaint(Color.gray);
xyplot.getRenderer().setSeriesPaint(0,Color.CYAN);
jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

NumberAxis numberAxis=(NumberAxis) xyplot.getRangeAxis();
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,12));

ValueAxis valueaxis = xyplot.getDomainAxis();
valueaxis.setLabelFont(new Font("宋体",Font.BOLD,12));
valueaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));

valueaxis.setAutoRange(true);

valueaxis.setFixedAutoRange(30000D);

valueaxis = xyplot.getRangeAxis();
//valueaxis.setRange(0.0D,200D);

return jfreechart;
}

private int randomNum()
{
System.out.println("MemoryUsed:"+(Math.random()*20+80));
return (int)(Math.random()*20+80);
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值