JRobin 1.5修正了中文字符定位的问题,对API做了不小的调整,一些方法被废弃掉了。解决一个生成图片的严重Bug, 1.4在针对某些RRD文件生成图片时,会出现以下异常
java.lang.ArrayIndexOutOfBoundsException: 414
at org.jrobin.graph.ValueExtractor.extract(ValueExtractor.java:137)
at org.jrobin.graph.RrdExporter.calculateSeries(RrdExporter.java:421)
at org.jrobin.graph.Grapher.calculateSeries(Grapher.java:340)
at org.jrobin.graph.Grapher.render(Grapher.java:315)
at org.jrobin.graph.Grapher.createImage(Grapher.java:223)
at org.jrobin.graph.RrdGraph.getBufferedImage(RrdGraph.java:468)
at org.jrobin.graph.RrdGraph.saveAsPNG(RrdGraph.java:154)
at org.jrobin.graph.RrdGraph.saveAsPNG(RrdGraph.java:137)
// 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;
//文件名为"-",表示仅保存到内存,不写文件
graphingParam.setGrapfilePath("-");
RrdGraph rrdGraph = constructionRrdGraph(chartModels, graphingParam);
// 取得图形的字节码
//add by 1.5
bytes = rrdGraph.getRrdGraphInfo().getBytes();
return bytes;
}
/**
* 返回生成图片输出到指定文件(文件位置由JRobinGraphingParam对象的grapfilePath指定)
* @param chartModels 一组曲线定义对象
* @param graphingParam 绘图的基本参数
* @throws RrdException
* @throws IOException
*/
public static void graphing(Collection<JRobinChartModel> chartModels,
JRobinGraphingParam graphingParam)
throws RrdException, IOException {
constructionRrdGraph(chartModels, graphingParam);
}
/**
* 构建RrdGraph图形对象
* @param chartModels
* @param graphingParam
* @return
* @throws RrdException
* @throws IOException
*/
private static RrdGraph constructionRrdGraph(
Collection<JRobinChartModel> chartModels,
JRobinGraphingParam graphingParam) throws RrdException, IOException {
// 图形
RrdGraph rrdGraph = null;
String defaultRrdPath = graphingParam.getRrdPath();
int legendColumns = graphingParam.getLegendColumns();
if (legendColumns == 0) {
legendColumns = 1;
}
// 图形定义
RrdGraphDef graphDef = new RrdGraphDef();
// 指定数据的时间跨度
graphDef.setTimeSpan(graphingParam.getStartTime(), graphingParam
.getEndTime());
// 不显示JRobin的签名
graphDef.setShowSignature(false);
// 中文字体
graphDef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));
graphDef.setLargeFont(new Font("SansSerif", Font.BOLD, 14));
// 标题
if (graphingParam.getTitle() != null) {
graphDef.setTitle(graphingParam.getTitle());
}
// 指定Y轴取值范围
if (graphingParam.isUseCustomGridYRangeFlag()) {
graphDef.setMinValue(graphingParam.getGridYLower());
graphDef.setMaxValue(graphingParam.getGridYUpper());
}
String comment = graphingParam.getComment();
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 += "\\l";
}
// 绘制曲线
graphingChar(graphDef, chartSeries, lineName, color, legend);
}
if (comment != null){
//换行
if (i%legendColumns >0){
graphDef.comment("\\l");
}
// 注释
graphDef.comment(comment);
}
//指定图片格式
GraphFormat graphFormat = graphingParam.getGraphFormat();
if (graphFormat == null){
graphFormat = GraphFormat.PNG;
}
graphDef.setImageFormat(graphFormat.toString());
if (graphingParam.isUseCustomGraphSizeFlag()) {
graphDef.setWidth(graphingParam.getWidth());
graphDef.setHeight(graphingParam.getHeight());
graphDef.setImageQuality(graphingParam.getQuality());
}
//图片文件名
graphDef.setFilename(graphingParam.getGrapfilePath());
// 图形
//1.5版,构造的时候就创建图形
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);
}
}