SNMP之JRobin画图
程序员文章站
2022-04-19 22:34:42
...
[color=red]blog迁移至[/color]:[url=http://www.micmiu.com]http://www.micmiu.com[/url]
本文主要记录下学习如何运用JRboin画图的一些测试代码。
本次测试画图用到的测试数据demo_flow.rrd文件,是来自之前写的[url=http://sjsky.iteye.com/admin/blogs/811393]SNMP系列之(一)JRobin Core学习[/url]中的方法生成的,可供大家参考。JRboin的画图主要从下面两方面:
[list]
[*][color=blue]直接定义RrdGraphDef对象画图[/color]
[*][color=blue]根据定义好的模板XML文件生成图片[/color][/list]
先看下两中方法生成的图片效果图:
[img]http://dl.iteye.com/upload/attachment/351073/dfeadeaa-1b79-38f5-a78b-ca867e38de45.png[/img]
[img]http://dl.iteye.com/upload/attachment/351075/14d7e2f5-92a0-3a88-8469-b996a04532ab.png[/img]
下面是我学习时生成图片的测试代码以及定义的模板文件:
本文主要记录下学习如何运用JRboin画图的一些测试代码。
本次测试画图用到的测试数据demo_flow.rrd文件,是来自之前写的[url=http://sjsky.iteye.com/admin/blogs/811393]SNMP系列之(一)JRobin Core学习[/url]中的方法生成的,可供大家参考。JRboin的画图主要从下面两方面:
[list]
[*][color=blue]直接定义RrdGraphDef对象画图[/color]
[*][color=blue]根据定义好的模板XML文件生成图片[/color][/list]
先看下两中方法生成的图片效果图:
[img]http://dl.iteye.com/upload/attachment/351073/dfeadeaa-1b79-38f5-a78b-ca867e38de45.png[/img]
[img]http://dl.iteye.com/upload/attachment/351075/14d7e2f5-92a0-3a88-8469-b996a04532ab.png[/img]
下面是我学习时生成图片的测试代码以及定义的模板文件:
package com.snmp.jrobin;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.jrobin.core.Util;
import org.jrobin.graph.RrdGraph;
import org.jrobin.graph.RrdGraphDef;
import org.jrobin.graph.RrdGraphDefTemplate;
import org.xml.sax.InputSource;
/**
* @author Michael
*
*/
public class TestGraphCommon {
/**
* @param args
*/
public static void main(String[] args) {
String rootPath = "d:/test/jrobin/";
String imgFileName = "demo_graph_rrd.png";
TestGraphCommon test = new TestGraphCommon();
// 测试直接定义画图模板生成图片
test.graphByGraphDef(rootPath, imgFileName);
String tempName = "graph-def-template.xml";
// 测试根据定义的XML模板文件生成图片
test.graphByTemplate(rootPath, tempName);
}
/**
* 直接定义画图模板生成图片
* @param rootPath
* @param imgFileName
*/
private void graphByGraphDef(String rootPath, String imgFileName) {
try {
System.out.println("[rrd graph by RrdGraphDef start...]");
// 2010-10-01:1285862400L 2010-11-01:1288540800L
long startTime = Util.getTimestamp(2010, 10 - 1, 1);
long endTime = Util.getTimestamp(2010, 10 - 1, 31);
RrdGraphDef rgdef = new RrdGraphDef();
// If the filename is set to '-' the image will be created only in
// memory (no file will be created).
// rgdef.setFilename("-");
rgdef.setFilename(rootPath + imgFileName);
// "PNG", "GIF" or "JPG"
rgdef.setImageFormat("PNG");
// rgdef.setTimeSpan(startTime, endTime);
rgdef.setStartTime(startTime);
rgdef.setEndTime(endTime);
rgdef.setAntiAliasing(false);
rgdef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));
rgdef.setLargeFont(new Font("SansSerif", Font.BOLD, 14));
rgdef.setTitle("JRobin graph by RrdGraphDef demo");
// default 400
rgdef.setWidth(500);
// default 100
rgdef.setHeight(200);
// 一般不需要设置这个参数
// rgdef.setStep(86400);
rgdef.setVerticalLabel("transfer speed [bits/sec]");
rgdef.datasource("in", rootPath + "demo_flow.rrd", "input",
"AVERAGE");
rgdef.datasource("out", rootPath + "demo_flow.rrd", "output",
"AVERAGE");
rgdef.datasource("in8", "in,8,*");
rgdef.datasource("out8", "out,8,*");
// PS:先画域的再画线的,否则线会被域遮盖
rgdef.area("out8", new Color(0, 206, 0), "output traffic");
rgdef.line("in8", Color.BLUE, "input traffic\\l");
// \\l->左对齐 \\c->中间对齐 \\r->右对齐 \\j->自适应
// \\s-> \\g->glue \\J->
rgdef.gprint("in8", "MAX", "maxIn=%.2f %sbits/sec");
rgdef.gprint("out8", "MAX", "maxOut=%.2f %sbits/sec\\l");
rgdef.gprint("in8", "AVERAGE", "avgIn=%.2f %sbits/sec");
rgdef.gprint("out8", "AVERAGE", "avgOut=%.2f %sbits/sec\\l");
rgdef.gprint("in8", "TOTAL", "totalIn=%.2f %sbits/sec");
rgdef.gprint("out8", "TOTAL", "totalOut=%.2f %sbits/sec\\s");
rgdef.comment("画图测试");
RrdGraph graph = new RrdGraph(rgdef);
System.out.println("[rrd graph info:]"
+ graph.getRrdGraphInfo().dump());
// 如果filename没有设置,只是在内存中,可以调用下面的方法再次生成图片文件
if ("-".equals(graph.getRrdGraphInfo().getFilename())) {
createImgFile(graph, rootPath + imgFileName);
}
System.out.println("[rrd graph by RrdGraphDef success.]");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ImageIO 生成图片文件
*/
private void createImgFile(RrdGraph graph, String imgFileFullName) {
try {
BufferedImage image = new BufferedImage(graph.getRrdGraphInfo()
.getWidth(), graph.getRrdGraphInfo().getHeight(),
BufferedImage.TYPE_INT_RGB);
graph.render(image.getGraphics());
File imgFile = new File(imgFileFullName);
ImageIO.write(image, "PNG", imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据定义的XML模板生成图片
* @param rootPath
* @param imgFileName
*/
private void graphByTemplate(String rootPath, String tempName) {
try {
System.out.println("[rrd graph by xml template start...]");
RrdGraphDefTemplate defTemplate = new RrdGraphDefTemplate(
new InputSource(rootPath + tempName));
// setVariable 设置XML template的变量
defTemplate.setVariable("startTime", "20101001 00:00");
defTemplate.setVariable("endTime", "20101031 23:59");
defTemplate.setVariable("in_rrd_file", rootPath + "demo_flow.rrd");
defTemplate.setVariable("out_rrd_file", rootPath + "demo_flow.rrd");
RrdGraph graph = new RrdGraph(defTemplate.getRrdGraphDef());
BufferedImage image = new BufferedImage(graph.getRrdGraphInfo()
.getWidth(), graph.getRrdGraphInfo().getHeight(),
BufferedImage.TYPE_INT_RGB);
graph.render(image.getGraphics());
File imgFile = new File(rootPath + "demo_graph_tmp.PNG");
ImageIO.write(image, "PNG", imgFile);//
System.out.println("[rrd graph by xml template success.]");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="GB2312"?>
<rrd_graph_def>
<span>
<start>${startTime}</start>
<end>${endTime}</end>
</span>
<filename>-</filename>
<options>
<anti_aliasing>off</anti_aliasing>
<fonts>
<small_font>
<name>Monospaced</name>
<style>PLAIN</style>
<size>11</size>
</small_font>
<large_font>
<name>SansSerif</name>
<style>BOLD</style>
<size>14</size>
</large_font>
</fonts>
<title>JRobin graph by xml template demo</title>
<vertical_label>transfer speed [bits/sec]</vertical_label>
<width>500</width>
<height>200</height>
<base>1024</base>
</options>
<datasources>
<def>
<name>in</name>
<rrd>${in_rrd_file}</rrd>
<source>input</source>
<cf>AVERAGE</cf>
</def>
<def>
<name>out</name>
<rrd>${out_rrd_file}</rrd>
<source>output</source>
<cf>AVERAGE</cf>
</def>
</datasources>
<graph>
<area>
<datasource>in</datasource>
<color>#00ce00</color>
<legend>入速率</legend>
</area>
<line>
<datasource>out</datasource>
<color>#0000FF</color>
<legend>出速率\l</legend>
</line>
<gprint>
<datasource>in</datasource>
<cf>MAX</cf>
<format>入速率最大值:%7.2f %sbits/sec</format>
</gprint>
<gprint>
<datasource>out</datasource>
<cf>MAX</cf>
<format>出速率最大值:%7.2f %sbits/sec\l</format>
</gprint>
<gprint>
<datasource>in</datasource>
<cf>AVERAGE</cf>
<format>入速率平均值:%7.2f %sbits/sec</format>
</gprint>
<gprint>
<datasource>out</datasource>
<cf>AVERAGE</cf>
<format>出速率平均值:%7.2f %sbits/sec\l</format>
</gprint>
<gprint>
<datasource>in</datasource>
<cf>TOTAL</cf>
<format>总入流量:%7.2f %sbits</format>
</gprint>
<gprint>
<datasource>out</datasource>
<cf>TOTAL</cf>
<format>总出流量:%7.2f %sbits\l</format>
</gprint>
<comment>测试画图\l</comment>
</graph>
</rrd_graph_def>
上一篇: JRobin显示中文
下一篇: oracle表字段删除问题
推荐阅读
-
007-PHP之GD画图流程
-
Android编程画图之抗锯齿解决方法
-
C#画图之饼图折线图的实现方法
-
Android编程画图之抗锯齿解决方法
-
C#画图之饼图折线图的实现方法
-
html5 Canvas画图教程(5)—canvas里画曲线之arc方法
-
html5 Canvas画图教程(8)—canvas里画曲线之bezierCurveTo方法
-
html5 Canvas画图教程(7)—canvas里画曲线之quadraticCurveTo方法
-
html5 Canvas画图教程(6)—canvas里画曲线之arcTo方法
-
html5 Canvas画图教程(8)—canvas里画曲线之bezierCurveTo方法