// datasource类型 private static final String DS_TYPE = "GAUGE"; // 默认从多个数据中取综合值的方式 private static final ConsolFun DEFAULT_CONSOL_FUN = ConsolFun.MAX; // 默认曲线形状 private static final ChartSeries DEFAULT_CHART_SERIES = ChartSeries.LINE; // 默认图片格式 private static final GraphFormat DEFAULT_GRAPH_FORMAT = GraphFormat.PNG; // 默认顺序颜色 private static final Color[] colors = new Color[] { Color.GREEN, Color.BLUE, Color.CYAN, Color.ORANGE, Color.PINK, Color.MAGENTA, Color.GRAY, Color.DARK_GRAY }; /** * 向RRD文件,插入数据 * * @param rrdPath * RRD文件路径 * @param dsNames dsName数组,类似于数据表的栏位概念 * @param values * @param collectTimeUnitsSecond * 数据所在时间,以秒为单位 * @throws RrdException * @throws IOException */ public static void insertRrdData(String rrdPath, String[] dsNames, double[] values, long collectTimeUnitsSecond) throws RrdException, IOException { if (dsNames != null && dsNames.length > 0) { // 取得RRD数据库连接池 RrdDbPool rrdPool = RrdDbPool.getInstance(); // 取得rrd数据库文件 RrdDb rrdDb = rrdPool.requestRrdDb(rrdPath); // 创建rrd记录 Sample sample = rrdDb.createSample(collectTimeUnitsSecond); for (int i = 0; i < values.length; i++) { sample.setValue(dsNames[i], values[i]); } // update database sample.update(); // release RRD database reference rrdPool.release(rrdDb); } } /** * 重新创建RRD文件 * @param rrdPath RRD文件路径 * @param dsNames dsName数组,类似于数据表的栏位概念 * @param archiveModels * @param startTime 插入的数据不能早于这个时间,为0时,将默认使用当前时间 * @param rrdStep RRD文件接收数据的频率,即多少秒接收一次数据 * @throws RrdException * @throws IOException */ public static void createRrdFile(String rrdPath, String[] dsNames, Collection<JRobinArchiveModel> archiveModels, long startTime, long rrdStep) throws RrdException, IOException { RrdDef rrdDef = null; long step = rrdStep; // create RRD file since it does not exist if (step > 0) { if (startTime > 0) { rrdDef = new RrdDef(rrdPath, startTime - 1, step); } else { rrdDef = new RrdDef(rrdPath, startTime - 1); } } else { rrdDef = new RrdDef(rrdPath); } // (String dsName, String dsType, long heartbeat,double minValue, // double maxValue) for (int i = 0; i < dsNames.length; i++) { rrdDef.addDatasource(dsNames[i], DS_TYPE, 600, Double.NaN, Double.NaN); } for (Iterator<JRobinArchiveModel> iterator = archiveModels.iterator(); iterator .hasNext();) { JRobinArchiveModel robinArchiveModel = iterator.next(); ConsolFun consolFun = robinArchiveModel.getConsolFun(); if (consolFun == null) { consolFun = DEFAULT_CONSOL_FUN; } rrdDef.addArchive(consolFun.toString(), 0.5, robinArchiveModel .getSteps(), robinArchiveModel.getRows()); } // create RRD file in the pool RrdDbPool rrdPool = RrdDbPool.getInstance(); RrdDb rrdDb = rrdPool.requestRrdDb(rrdDef); rrdPool.release(rrdDb); } /** * 返回生成图片的字节码,而不写入文件 * @param chartModels 一组曲线定义对象 * @param graphingParam 绘图的基本参数 * @return * @throws RrdException * @throws IOException */ public static byte[] graphBytes(Collection<JRobinChartModel> chartModels, JRobinGraphingParam graphingParam) throws RrdException, IOException { byte[] bytes = null; RrdGraph rrdGraph = constructionRrdGraph(chartModels, graphingParam); // 取得图形的字节码 if (!graphingParam.isUseCustomGraphSizeFlag()) { bytes = byteGraph(rrdGraph, graphingParam.getGraphFormat()); } else { bytes = byteGraph(rrdGraph, graphingParam.getGraphFormat(), graphingParam.getWidth(), graphingParam.getHeight(), graphingParam.getQuality()); } return bytes; } /** * 返回生成图片输出到指定文件(文件位置由JRobinGraphingParam对象的grapfilePath指定) * @param chartModels 一组曲线定义对象 * @param graphingParam 绘图的基本参数 * @throws RrdException * @throws IOException */ public static void graphing(Collection<JRobinChartModel> chartModels, JRobinGraphingParam graphingParam) throws RrdException, IOException { RrdGraph rrdGraph = constructionRrdGraph(chartModels, graphingParam); // 保存图形到文件 if (!graphingParam.isUseCustomGraphSizeFlag()) { saveGraph(rrdGraph, graphingParam.getGraphFormat(), graphingParam.getGrapfilePath()); } else { saveGraph(rrdGraph, graphingParam.getGraphFormat(), graphingParam .getWidth(), graphingParam.getHeight(), graphingParam .getQuality(), graphingParam.getGrapfilePath()); } } /** * 构建RrdGraph图形对象 * @param chartModels * @param graphingParam * @return * @throws RrdException */ private static RrdGraph constructionRrdGraph( Collection<JRobinChartModel> chartModels, JRobinGraphingParam graphingParam) throws RrdException { // 图形 RrdGraph rrdGraph = null; String defaultRrdPath = graphingParam.getRrdPath(); int legendColumns = graphingParam.getLegendColumns(); if (legendColumns == 0) { legendColumns = 1; } // 图形定义 RrdGraphDef graphDef = new RrdGraphDef(); // 指定数据的时间跨度 graphDef.setTimePeriod(graphingParam.getStartTime(), graphingParam .getEndTime()); // 不显示JRobin的签名 graphDef.setShowSignature(false); // 中文字体 graphDef.setDefaultFont(new Font("Monospaced", Font.PLAIN, 11)); graphDef.setTitleFont(new Font("SansSerif", Font.BOLD, 14)); // 标题 if (graphingParam.getTitle() != null) { graphDef.setTitle(graphingParam.getTitle()); } // 指定Y轴取值范围 if (graphingParam.isUseCustomGridYRangeFlag()) { graphDef.setGridRange(graphingParam.getGridYLower(), graphingParam .getGridYUpper(), true); } int i = 0; for (Iterator<JRobinChartModel> iterator = chartModels.iterator(); iterator .hasNext(); i++) { JRobinChartModel robinChartModel = iterator.next(); // 合并方式 ConsolFun consolFun = robinChartModel.getConsolFun(); if (consolFun == null) { consolFun = DEFAULT_CONSOL_FUN; } // 曲线颜色 Color color = robinChartModel.getColor(); if (color == null) { color = colors[i % colors.length]; } // 曲线说明 String legend = robinChartModel.getLegend(); // 曲线形状 ChartSeries chartSeries = robinChartModel.getChartSeries(); // 数据源名称 String dsName = robinChartModel.getDsName(); // 曲线标识 String lineName = dsName + "_" + consolFun.toString(); String rrdPath = robinChartModel.getRrdPath(); //如果robinChartModel未指定rrdPath,则使用JRobinGraphingParam对象的rrdPath if (rrdPath == null || rrdPath.trim().length() == 0){ rrdPath = defaultRrdPath; } // 加入该曲线的相关数据 graphDef .datasource(lineName, rrdPath, dsName, consolFun.toString()); // 根据每行显示几个legend,加入换行符 if ((i + 1) % legendColumns == 0) { legend += "\n"; } // 绘制曲线 graphingChar(graphDef, chartSeries, lineName, color, legend); } // 注释 graphDef.comment(graphingParam.getComment()); // 图形 rrdGraph = new RrdGraph(graphDef); return rrdGraph; } /** * 绘制一条曲线到graphDef * * @param graphDef 定义图形的对象 * @param chartSeries 线型 * @param dsName * @param color * @param legend * @throws RrdException */ private static void graphingChar(RrdGraphDef graphDef, ChartSeries chartSeries, String dsName, Color color, String legend) throws RrdException { if (chartSeries == null) { chartSeries = DEFAULT_CHART_SERIES; } // 根据不同的图形形状类别,调用不同的方法 if (chartSeries.equals(ChartSeries.LINE)) { graphDef.line(dsName, color, legend); } else if (chartSeries.equals(ChartSeries.AREA)) { graphDef.area(dsName, color, legend); } else if (chartSeries.equals(ChartSeries.STACK)) { graphDef.stack(dsName, color, legend); } } /** * 按默认宽高,生成图片文件 * * @param rrdGraph * @param graphFormat * @param graphingPath * @throws RrdException * @throws IOException */ private static void saveGraph(RrdGraph rrdGraph, GraphFormat graphFormat, String graphingPath) throws RrdException, IOException { if (graphFormat == null) { graphFormat = DEFAULT_GRAPH_FORMAT; } if (graphFormat.equals(GraphFormat.PNG)) { rrdGraph.saveAsPNG(graphingPath); } else if (graphFormat.equals(GraphFormat.GIF)) { rrdGraph.saveAsGIF(graphingPath); } else if (graphFormat.equals(GraphFormat.JPEG)) { rrdGraph.saveAsJPEG(graphingPath, 0.5f); } } /** * 按指定宽高生成图片文件 * * @param rrdGraph * @param graphFormat * @param width * @param height * @param quality * @param graphingPath * @throws RrdException * @throws IOException */ private static void saveGraph(RrdGraph rrdGraph, GraphFormat graphFormat, int width, int height, float quality, String graphingPath) throws RrdException, IOException { if (graphFormat == null) { graphFormat = DEFAULT_GRAPH_FORMAT; } if (graphFormat.equals(GraphFormat.PNG)) { rrdGraph.saveAsPNG(graphingPath, width, height); } else if (graphFormat.equals(GraphFormat.GIF)) { rrdGraph.saveAsGIF(graphingPath, width, height); } else if (graphFormat.equals(GraphFormat.JPEG)) { if (quality == 0) { quality = 0.5f; } rrdGraph.saveAsJPEG(graphingPath, width, height, quality); } } /** * 按默认宽高,返回图片的字节码 * * @param rrdGraph * @param graphFormat * @return * @throws RrdException * @throws IOException */ private static byte[] byteGraph(RrdGraph rrdGraph, GraphFormat graphFormat) throws RrdException, IOException { byte[] bytes = null; if (graphFormat == null) { graphFormat = DEFAULT_GRAPH_FORMAT; } if (graphFormat.equals(GraphFormat.PNG)) { bytes = rrdGraph.getPNGBytes(); } else if (graphFormat.equals(GraphFormat.GIF)) { bytes = rrdGraph.getGIFBytes(); } else if (graphFormat.equals(GraphFormat.JPEG)) { bytes = rrdGraph.getJPEGBytes(0.5f); } return bytes; } /** * 按指定宽高,返回图片的字节码 * * @param rrdGraph * @param graphFormat * @param width * @param height * @param quality * @return * @throws RrdException * @throws IOException */ private static byte[] byteGraph(RrdGraph rrdGraph, GraphFormat graphFormat, int width, int height, float quality) throws RrdException, IOException { byte[] bytes = null; if (graphFormat == null) { graphFormat = DEFAULT_GRAPH_FORMAT; } if (graphFormat.equals(GraphFormat.PNG)) { bytes = rrdGraph.getPNGBytes(width, height); } else if (graphFormat.equals(GraphFormat.GIF)) { bytes = rrdGraph.getGIFBytes(width, height); } else if (graphFormat.equals(GraphFormat.JPEG)) { if (quality == 0) { quality = 0.5f; } bytes = rrdGraph.getJPEGBytes(width, height, quality); } return bytes; }