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);
}
}
jfreechart 线图
最新推荐文章于 2024-09-05 19:35:27 发布