SNMP之JRobin画图

来源:互联网 发布:windows 触摸板设置 编辑:程序博客网 时间:2024/05/16 18:00

SNMP之JRobin画图

本文主要记录下学习如何运用JRboin画图的一些测试代码。

本次测试画图用到的测试数据demo_flow.rrd文件,是来自之前写的SNMP系列之(一)JRobin Core学习中的方法生成的,可供大家参考。

JRboin的画图主要从下面两方面:

  • 直接定义RrdGraphDef对象画图
  • 根据定义好的模板XML文件生成图片

先看下两中方法生成的图片效果图:

下面是我写demo时生成图片的测试代码以及定义的模板XML文件:

1package com.snmp.jrobin;
2 
3import java.awt.Color;
4import java.awt.Font;
5import java.awt.image.BufferedImage;
6import java.io.File;
7 
8import javax.imageio.ImageIO;
9 
10import org.jrobin.core.Util;
11import org.jrobin.graph.RrdGraph;
12import org.jrobin.graph.RrdGraphDef;
13import org.jrobin.graph.RrdGraphDefTemplate;
14import org.xml.sax.InputSource;
15 
16/**
17 * @author Michael
18 *
19 */
20public class TestGraphCommon {
21 
22    /**
23     * @param args
24     */
25    public static void main(String[] args) {
26        String rootPath = "d:/test/jrobin/";
27        String imgFileName = "demo_graph_rrd.png";
28 
29        TestGraphCommon test = new TestGraphCommon();
30 
31        // 测试直接定义画图模板生成图片
32        test.graphByGraphDef(rootPath, imgFileName);
33 
34        String tempName = "graph-def-template.xml";
35        // 测试根据定义的XML模板文件生成图片
36        test.graphByTemplate(rootPath, tempName);
37    }
38 
39    /**
40     * 直接定义画图模板生成图片
41     * @param rootPath
42     * @param imgFileName
43     */
44    private void graphByGraphDef(String rootPath, String imgFileName) {
45        try {
46            System.out.println("[rrd graph by RrdGraphDef start...]");
47            // 2010-10-01:1285862400L 2010-11-01:1288540800L
48            long startTime = Util.getTimestamp(201010 11);
49            long endTime = Util.getTimestamp(201010 131);
50 
51            RrdGraphDef rgdef = new RrdGraphDef();
52            // If the filename is set to '-' the image will be created only in
53            // memory (no file will be created).
54            // rgdef.setFilename("-");
55            rgdef.setFilename(rootPath + imgFileName);
56 
57            // "PNG", "GIF" or "JPG"
58            rgdef.setImageFormat("PNG");
59            // rgdef.setTimeSpan(startTime, endTime);
60            rgdef.setStartTime(startTime);
61            rgdef.setEndTime(endTime);
62 
63            rgdef.setAntiAliasing(false);
64            rgdef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));
65            rgdef.setLargeFont(new Font("SansSerif", Font.BOLD, 14));
66 
67            rgdef.setTitle("JRobin graph by RrdGraphDef demo");
68            // default 400
69            rgdef.setWidth(500);
70            // default 100
71            rgdef.setHeight(200);
72 
73            // 一般不需要设置这个参数
74            // rgdef.setStep(86400);
75 
76            rgdef.setVerticalLabel("transfer speed [bits/sec]");
77 
78            rgdef.datasource("in", rootPath + "demo_flow.rrd""input",
79                    "AVERAGE");
80            rgdef.datasource("out", rootPath + "demo_flow.rrd""output",
81                    "AVERAGE");
82            rgdef.datasource("in8""in,8,*");
83            rgdef.datasource("out8""out,8,*");
84            // PS:先画域的再画线的,否则线会被域遮盖
85            rgdef.area("out8"new Color(02060), "output traffic");
86            rgdef.line("in8", Color.BLUE, "input traffic\\l");
87 
88            // \\l->左对齐 \\c->中间对齐 \\r->右对齐 \\j->自适应
89            // \\s-> \\g->glue \\J->
90            rgdef.gprint("in8""MAX""maxIn=%.2f %sbits/sec");
91            rgdef.gprint("out8""MAX""maxOut=%.2f %sbits/sec\\l");
92            rgdef.gprint("in8""AVERAGE""avgIn=%.2f %sbits/sec");
93            rgdef.gprint("out8""AVERAGE""avgOut=%.2f %sbits/sec\\l");
94            rgdef.gprint("in8""TOTAL""totalIn=%.2f %sbits/sec");
95            rgdef.gprint("out8""TOTAL""totalOut=%.2f %sbits/sec\\s");
96            rgdef.comment("画图测试");
97 
98            RrdGraph graph = new RrdGraph(rgdef);
99            System.out.println("[rrd graph info:]"
100                    + graph.getRrdGraphInfo().dump());
101            // 如果filename没有设置,只是在内存中,可以调用下面的方法再次生成图片文件
102            if ("-".equals(graph.getRrdGraphInfo().getFilename())) {
103                createImgFile(graph, rootPath + imgFileName);
104            }
105            System.out.println("[rrd graph by RrdGraphDef success.]");
106        catch (Exception e) {
107            e.printStackTrace();
108        }
109    }
110 
111    /**
112     * ImageIO 生成图片文件
113     */
114    private void createImgFile(RrdGraph graph, String imgFileFullName) {
115 
116        try {
117            BufferedImage image = new BufferedImage(graph.getRrdGraphInfo()
118                    .getWidth(), graph.getRrdGraphInfo().getHeight(),
119                    BufferedImage.TYPE_INT_RGB);
120            graph.render(image.getGraphics());
121            File imgFile = new File(imgFileFullName);
122            ImageIO.write(image, "PNG", imgFile);
123        catch (Exception e) {
124            e.printStackTrace();
125        }
126    }
127 
128    /**
129     * 根据定义的XML模板生成图片
130     * @param rootPath
131     * @param imgFileName
132     */
133    private void graphByTemplate(String rootPath, String tempName) {
134        try {
135            System.out.println("[rrd graph by xml template start...]");
136            RrdGraphDefTemplate defTemplate = new RrdGraphDefTemplate(
137                    new InputSource(rootPath + tempName));
138            // setVariable 设置XML template的变量
139            defTemplate.setVariable("startTime""20101001 00:00");
140            defTemplate.setVariable("endTime""20101031 23:59");
141            defTemplate.setVariable("in_rrd_file", rootPath + "demo_flow.rrd");
142            defTemplate.setVariable("out_rrd_file", rootPath + "demo_flow.rrd");
143            RrdGraph graph = new RrdGraph(defTemplate.getRrdGraphDef());
144 
145            BufferedImage image = new BufferedImage(graph.getRrdGraphInfo()
146                    .getWidth(), graph.getRrdGraphInfo().getHeight(),
147                    BufferedImage.TYPE_INT_RGB);
148            graph.render(image.getGraphics());
149            File imgFile = new File(rootPath + "demo_graph_tmp.PNG");
150            ImageIO.write(image, "PNG", imgFile);//
151            System.out.println("[rrd graph by xml template success.]");
152        catch (Exception e) {
153            e.printStackTrace();
154        }
155    }
156 
157}

模板XML文件:

查看源代码
打印帮助
1<?xml version="1.0" encoding="GB2312"?>
2<rrd_graph_def>
3    <span>
4        <start>${startTime}</start>
5        <end>${endTime}</end>
6    </span>
7    <filename>-</filename>
8    <options>
9        <anti_aliasing>off</anti_aliasing>
10        <fonts>
11                    <small_font>
12                        <name>Monospaced</name>
13                        <style>PLAIN</style>
14                        <size>11</size>
15                    </small_font>
16                    <large_font>
17                        <name>SansSerif</name>
18                        <style>BOLD</style>
19                        <size>14</size>
20                    </large_font>
21                </fonts>
22                 <title>JRobin graph by xml template demo</title>
23        <vertical_label>transfer speed [bits/sec]</vertical_label>
24        <width>500</width>
25        <height>200</height>
26        <base>1024</base>
27    </options>
28    <datasources>
29        <def>
30            <name>in</name>
31            <rrd>${in_rrd_file}</rrd>
32            <source>input</source>
33            <cf>AVERAGE</cf>
34        </def>
35        <def>
36            <name>out</name>
37            <rrd>${out_rrd_file}</rrd>
38            <source>output</source>
39            <cf>AVERAGE</cf>
40        </def>
41    </datasources>
42    <graph>
43        <area>
44            <datasource>in</datasource>
45            <color>#00ce00</color>
46            <legend>入速率</legend>
47        </area>
48        <line>
49            <datasource>out</datasource>
50            <color>#0000FF</color>
51            <legend>出速率\l</legend>
52        </line>
53        <gprint>
54            <datasource>in</datasource>
55            <cf>MAX</cf>
56            <format>入速率最大值:%7.2f %sbits/sec</format>
57        </gprint>
58        <gprint>
59            <datasource>out</datasource>
60            <cf>MAX</cf>
61            <format>出速率最大值:%7.2f %sbits/sec\l</format>
62        </gprint>
63       <gprint>
64            <datasource>in</datasource>
65            <cf>AVERAGE</cf>
66            <format>入速率平均值:%7.2f %sbits/sec</format>
67        </gprint>
68        <gprint>
69            <datasource>out</datasource>
70            <cf>AVERAGE</cf>
71            <format>出速率平均值:%7.2f %sbits/sec\l</format>
72        </gprint>
73        <gprint>
74            <datasource>in</datasource>
75            <cf>TOTAL</cf>
76            <format>总入流量:%7.2f %sbits</format>
77        </gprint>
78        <gprint>
79            <datasource>out</datasource>
80            <cf>TOTAL</cf>
81            <format>总出流量:%7.2f %sbits\l</format>
82        </gprint>
83        <comment>测试画图\l</comment>
84    </graph>
85</rrd_graph_def>