1 25 package org.jrobin.graph; 26 27 import java.io.*; 28 import java.awt.Font ; 29 import java.awt.Color ; 30 import java.awt.BasicStroke ; 31 import java.util.*; 32 import java.text.SimpleDateFormat ; 33 import java.text.DateFormat ; 34 35 import org.jrobin.core.RrdException; 36 import org.jrobin.core.XmlWriter; 37 38 58 public class RrdGraphDef extends RrdExportDef implements Serializable 59 { 60 private Title title = null; private String valueAxisLabel = null; private TimeAxisLabel timeAxisLabel = null; 67 private boolean lazyGeneration = false; private boolean gridX = true; private boolean gridY = true; private boolean minorGridX = true; private boolean minorGridY = true; private boolean majorGridX = true; private boolean majorGridY = true; private boolean frontGrid = true; private boolean antiAliasing = true; private boolean showLegend = true; private boolean drawSignature = true; 79 private Color backColor = new Color ( 245, 245, 245 ); private Color canvasColor = Color.WHITE; private Color borderColor = Color.LIGHT_GRAY; private Color normalFontColor = Color.BLACK; private Color titleFontColor = Color.BLACK; private Color majorGridColor = new Color (130,30,30); private Color minorGridColor = new Color (140,140,140); private Color axisColor = new Color (130,30,30); private Color arrowColor = Color.RED; private Color frameColor = Color.LIGHT_GRAY; 90 private Font titleFont = null; private Font normalFont = null; 93 private File background = null; private File overlay = null; 96 private int chart_lpadding = Grapher.CHART_LPADDING; 98 private int firstDayOfWeek = TimeAxisUnit.MONDAY; 100 private double baseValue = ValueFormatter.DEFAULT_BASE; private int scaleIndex = ValueFormatter.NO_SCALE; 103 private BasicStroke borderStroke = null; private TimeAxisUnit tAxis = null; private ValueAxisUnit vAxis = null; private GridRange gridRange = null; 108 private int commentLines = 0; private int commentLineShift = 0; 112 private ArrayList plotDefs = new ArrayList( 10 ); private ArrayList comments = new ArrayList( 10 ); 115 116 122 public RrdGraphDef() { 123 } 124 125 132 public RrdGraphDef( long startTime, long endTime ) throws RrdException 133 { 134 setTimePeriod( startTime, endTime ); 135 } 136 137 144 public RrdGraphDef( Date start, Date end) throws RrdException 145 { 146 setTimePeriod( start, end ); 147 } 148 149 156 public RrdGraphDef( GregorianCalendar start, GregorianCalendar end ) throws RrdException 157 { 158 setTimePeriod( start, end ); 159 } 160 161 162 173 public void setLazy( boolean lazyGeneration ) 174 { 175 this.lazyGeneration = lazyGeneration; 176 } 177 178 182 public void setTitle( String title ) throws RrdException 183 { 184 this.title = new Title( title ); 185 } 186 187 191 public void setVerticalLabel( String label) 192 { 193 this.valueAxisLabel = label; 194 } 195 196 211 public void setTimeAxisLabel( String label ) throws RrdException 212 { 213 if ( label != null ) 214 { 215 timeAxisLabel = new TimeAxisLabel( label ); 216 commentLines += timeAxisLabel.getLineCount(); 217 commentLineShift = (timeAxisLabel.isCompleteLine() ? 0 : 1); 218 219 comments.add( 0, timeAxisLabel ); 220 } 221 } 222 223 227 public void setBackColor( Color backColor ) 228 { 229 this.backColor = backColor; 230 } 231 232 236 public void setCanvasColor( Color canvasColor ) 237 { 238 this.canvasColor = canvasColor; 239 } 240 241 248 public void setImageBorder( Color c, int w ) 249 { 250 this.borderStroke = new BasicStroke ( w ); 251 if ( c != null ) 252 this.borderColor = c; 253 } 254 255 260 public void setTitleFontColor( Color c ) 261 { 262 this.titleFontColor = c; 263 } 264 265 270 public void setDefaultFontColor( Color c ) 271 { 272 this.normalFontColor = c; 273 } 274 275 280 public void setTitleFont( Font f ) 281 { 282 this.titleFont = f; 283 } 284 285 290 public void setDefaultFont( Font f ) 291 { 292 this.normalFont = f; 293 } 294 295 300 public void setMajorGridColor( Color c ) 301 { 302 this.majorGridColor = c; 303 } 304 305 309 public void setMinorGridColor( Color c ) 310 { 311 this.minorGridColor = c; 312 } 313 314 318 public void setFrameColor( Color c ) 319 { 320 this.frameColor = c; 321 } 322 323 327 public void setAxisColor( Color c ) 328 { 329 this.axisColor = c; 330 } 331 332 336 public void setArrowColor( Color c ) 337 { 338 this.arrowColor = c; 339 } 340 341 345 public void setMinorGridX( boolean visible ) 346 { 347 this.minorGridX = visible; 348 } 349 350 354 public void setMinorGridY( boolean visible ) 355 { 356 this.minorGridY = visible; 357 } 358 359 363 public void setMajorGridX( boolean visible ) 364 { 365 this.majorGridX = visible; 366 } 367 368 372 public void setMajorGridY( boolean visible ) 373 { 374 this.majorGridY = visible; 375 } 376 377 381 public void setGridX( boolean visible ) 382 { 383 this.gridX = visible; 384 } 385 386 390 public void setGridY( boolean visible ) 391 { 392 this.gridY = visible; 393 } 394 395 400 public void setFrontGrid( boolean frontGrid ) 401 { 402 this.frontGrid = frontGrid; 403 } 404 405 411 public void setShowLegend( boolean showLegend ) 412 { 413 this.showLegend = showLegend; 414 } 415 416 423 public void setShowSignature( boolean showSignature ) 424 { 425 this.drawSignature = showSignature; 426 } 427 428 433 public void setAntiAliasing( boolean aa ) 434 { 435 this.antiAliasing = aa; 436 } 437 438 442 public void setChartLeftPadding( int lp ) 443 { 444 this.chart_lpadding = lp; 445 } 446 447 458 public void setBackground( String fileName ) 459 { 460 File bgFile = new File( fileName ); 461 if ( bgFile.exists() ) 462 this.background = bgFile; 463 } 464 465 477 public void setOverlay( String fileName ) 478 { 479 File ovFile = new File( fileName ); 480 if ( ovFile.exists() ) 481 this.overlay = ovFile; 482 } 483 484 491 public void setBaseValue( double base ) 492 { 493 this.baseValue = base; 494 } 495 496 507 public void setUnitsExponent( int e ) 508 { 509 this.scaleIndex = (6 - e / 3); } 511 512 int getUnitsExponent() { 513 return (6 - scaleIndex) * 3; 514 } 515 516 524 525 public void setGridRange( double lower, double upper, boolean rigid ) 526 { 527 gridRange = new GridRange( lower, upper, rigid ); 528 } 529 530 535 public void setLowerLimit( double lower ) 536 { 537 gridRange = new GridRange( lower, Double.NaN, false ); 538 } 539 540 547 public void setValueAxis( double gridStep, double labelStep ) 548 { 549 vAxis = new ValueAxisUnit( gridStep, labelStep ); 550 } 551 552 568 public void setTimeAxis( int minGridTimeUnit, 569 int minGridUnitSteps, 570 int majGridTimeUnit, 571 int majGridUnitSteps, 572 String dateFormat, 573 boolean centerLabels ) 574 { 575 this.tAxis = new TimeAxisUnit( minGridTimeUnit, 576 minGridUnitSteps, 577 majGridTimeUnit, 578 majGridUnitSteps, 579 new SimpleDateFormat ( dateFormat ), 580 centerLabels , 581 firstDayOfWeek 582 ); 583 } 584 585 590 public void setFirstDayOfWeek( int day ) 591 { 592 firstDayOfWeek = day; 593 } 594 595 606 public void line( String sourceName, Color color, String legend ) throws RrdException 607 { 608 plotDefs.add( new Line(sourceName, color) ); 609 addLegend( legend, color ); 610 } 611 612 623 public void line( String sourceName, Color color, String legend, int lineWidth ) throws RrdException 624 { 625 plotDefs.add( new Line(sourceName, color, lineWidth) ); 626 addLegend( legend, color ); 627 } 628 629 642 public void line( GregorianCalendar t1, double v1, GregorianCalendar t2, double v2, Color color, String legend, int lineWidth ) throws RrdException 643 { 644 plotDefs.add( new CustomLine( t1.getTimeInMillis() / 1000, v1, t2.getTimeInMillis() / 1000, v2, color, lineWidth ) ); 645 addLegend( legend, color ); 646 } 647 648 659 public void area( String sourceName, Color color, String legend ) throws RrdException 660 { 661 plotDefs.add( new Area(sourceName, color) ); 662 addLegend( legend, color ); 663 } 664 665 679 public void area( GregorianCalendar t1, double v1, GregorianCalendar t2, double v2, Color color, String legend ) throws RrdException 680 { 681 plotDefs.add( new CustomArea( t1.getTimeInMillis() / 1000, v1, t2.getTimeInMillis() / 1000, v2, color ) ); 682 addLegend( legend, color ); 683 } 684 685 695 public void stack( String sourceName, Color color, String legend ) throws RrdException 696 { 697 plotDefs.add( new Stack(sourceName, color) ); 698 addLegend( legend, color ); 699 } 700 701 709 public void hrule(double value, Color color, String legend) throws RrdException { 710 plotDefs.add( new CustomLine( Long.MIN_VALUE, value, Long.MAX_VALUE, value, color ) ); 711 addLegend( legend, color ); 712 } 713 714 723 public void hrule(double value, Color color, String legend, int lineWidth) throws RrdException { 724 plotDefs.add( new CustomLine( Long.MIN_VALUE, value, Long.MAX_VALUE, value, color, lineWidth ) ); 725 addLegend( legend, color ); 726 } 727 728 735 public void vrule( GregorianCalendar timestamp, Color color, String legend ) throws RrdException { 736 long timeSecs = timestamp.getTimeInMillis() / 1000; 737 plotDefs.add( new CustomLine( timeSecs, Double.MIN_VALUE, timeSecs, Double.MAX_VALUE, color ) ); 738 addLegend( legend, color ); 739 } 740 741 750 public void vrule( GregorianCalendar timestamp, Color color, String legend, int lineWidth ) throws RrdException { 751 long timeSecs = timestamp.getTimeInMillis() / 1000; 752 plotDefs.add( new CustomLine( timeSecs, Double.MIN_VALUE, timeSecs, Double.MAX_VALUE, color, lineWidth ) ); 753 addLegend( legend, color ); 754 } 755 756 768 public void comment(String text) throws RrdException { 769 addComment( new Comment(text) ); 770 } 771 772 782 public void time( String text, String pattern ) throws RrdException { 783 addComment( new TimeText( text, pattern ) ); 784 } 785 786 795 public void time( String text, DateFormat format ) throws RrdException { 796 addComment( new TimeText( text, format ) ); 797 } 798 799 810 public void time( String text, String pattern, long timestamp ) throws RrdException { 811 addComment( new TimeText( text, pattern, timestamp ) ); 812 } 813 814 824 public void time( String text, DateFormat format, long timestamp ) throws RrdException { 825 addComment( new TimeText( text, format, timestamp ) ); 826 } 827 828 839 public void time( String text, String pattern, Date date ) throws RrdException { 840 addComment( new TimeText( text, pattern, date ) ); 841 } 842 843 853 public void time( String text, DateFormat format, Date date ) throws RrdException { 854 addComment( new TimeText( text, format, date ) ); 855 } 856 857 868 public void time( String text, String pattern, Calendar cal) throws RrdException { 869 addComment( new TimeText( text, pattern, cal ) ); 870 } 871 872 882 public void time( String text, DateFormat format, Calendar cal) throws RrdException { 883 addComment( new TimeText( text, format, cal ) ); 884 } 885 886 914 public void gprint(String sourceName, String consolFun, String format) throws RrdException 915 { 916 addComment( new Gprint(sourceName, consolFun, format) ); 917 } 918 919 933 public void gprint( String sourceName, String consolFun, String format, double base ) throws RrdException 934 { 935 addComment( new Gprint(sourceName, consolFun, format, base) ); 936 } 937 938 943 public void exportXmlTemplate(OutputStream stream) { 944 XmlWriter xml = new XmlWriter(stream); 945 xml.startTag("rrd_graph_def"); 946 xml.startTag("span"); 948 xml.writeTag("start", getStartTime() ); 949 xml.writeTag("end", getEndTime() ); 950 xml.closeTag(); xml.startTag("options"); 953 xml.writeTag("anti_aliasing", antiAliasing); 954 xml.writeTag("arrow_color", arrowColor); 955 xml.writeTag("axis_color", axisColor); 956 xml.writeTag("back_color", backColor); 957 if(background != null) { 958 xml.writeTag("background", background); 959 } 960 xml.writeTag("base_value", baseValue); 961 xml.writeTag("canvas", canvasColor); 962 xml.writeTag("left_padding", chart_lpadding); 963 if(normalFont != null) { 964 xml.writeTag("default_font", normalFont); 965 } 966 xml.writeTag("default_font_color", normalFontColor); 967 xml.writeTag("frame_color", frameColor); 968 xml.writeTag("front_grid", frontGrid); 969 if(gridRange != null) { 970 gridRange.exportXmlTemplate(xml); 971 } 972 xml.writeTag("grid_x", gridX); 973 xml.writeTag("grid_y", gridY); 974 if(borderStroke != null) { 975 xml.startTag("border"); 976 xml.writeTag("color", borderColor); 977 xml.writeTag("width", (int)borderStroke.getLineWidth()); 978 xml.closeTag(); } 980 xml.writeTag("major_grid_color", majorGridColor); 981 xml.writeTag("major_grid_x", majorGridX); 982 xml.writeTag("major_grid_y", majorGridY); 983 xml.writeTag("minor_grid_color", minorGridColor); 984 xml.writeTag("minor_grid_x", minorGridX); 985 xml.writeTag("minor_grid_y", minorGridY); 986 if(overlay != null) { 987 xml.writeTag("overlay", overlay); 988 } 989 xml.writeTag("show_legend", showLegend); 990 xml.writeTag("show_signature", drawSignature); 991 if(tAxis != null) { 992 tAxis.exportXmlTemplate(xml); 993 } 994 if(timeAxisLabel != null) { 995 timeAxisLabel.exportXmlTemplate(xml); 996 } 997 if(title != null) { 998 title.exportXmlTemplate(xml); 999 } 1000 if(titleFont != null) { 1001 xml.writeTag("title_font", titleFont); 1002 } 1003 xml.writeTag("title_font_color", titleFontColor); 1004 if(scaleIndex != ValueFormatter.NO_SCALE) { 1005 xml.writeTag("units_exponent", getUnitsExponent()); 1006 } 1007 if(vAxis != null) { 1008 vAxis.exportXmlTemplate(xml); 1009 } 1010 if(valueAxisLabel != null) { 1011 xml.writeTag("vertical_label", valueAxisLabel); 1012 } 1013 xml.closeTag(); xml.startTag("datasources"); 1016 for ( int i = 0; i < fetchSources.size(); i++ ) 1018 fetchSources.get( i ).exportXml(xml); 1019 for (int i = 0; i < cdefList.size(); i++ ) { 1021 Cdef cdef = (Cdef) cdefList.get(i); 1022 cdef.exportXml(xml); 1023 } 1024 xml.closeTag(); xml.startTag("graph"); 1026 for ( int i = 0; i < comments.size(); i++ ) 1027 { 1028 Comment cmt = (Comment) comments.get(i); 1029 if ( cmt.commentType == Comment.CMT_LEGEND || cmt.commentType == Comment.CMT_NOLEGEND) 1030 { 1031 PlotDef pDef = (PlotDef) plotDefs.get( ((Legend) cmt).getPlofDefIndex() ); 1032 pDef.exportXmlTemplate(xml, cmt.text); 1033 } 1034 else if(cmt instanceof TimeAxisLabel) { 1035 } 1037 else { 1038 cmt.exportXmlTemplate(xml); 1039 } 1040 } 1041 xml.closeTag(); xml.closeTag(); xml.flush(); 1044 } 1045 1046 protected boolean isLazy() { 1050 return lazyGeneration; 1051 } 1052 1053 protected Title getTitle() { 1054 return title; 1055 } 1056 1057 protected String getVerticalLabel() { 1058 return valueAxisLabel; 1059 } 1060 1061 protected Color getBackColor() { 1062 return backColor; 1063 } 1064 1065 protected Color getCanvasColor() { 1066 return canvasColor; 1067 } 1068 1069 protected Color getImageBorderColor() { 1070 return borderColor; 1071 } 1072 1073 protected BasicStroke getImageBorderStroke() { 1074 return borderStroke; 1075 } 1076 1077 protected Color getTitleFontColor() { 1078 return titleFontColor; 1079 } 1080 1081 protected Color getDefaultFontColor() { 1082 return normalFontColor; 1083 } 1084 1085 protected Font getTitleFont() { 1086 return titleFont; 1087 } 1088 1089 protected Font getDefaultFont() { 1090 return normalFont; 1091 } 1092 1093 protected Color getMajorGridColor() { 1094 return majorGridColor; 1095 } 1096 1097 protected Color getMinorGridColor() { 1098 return minorGridColor; 1099 } 1100 1101 protected Color getFrameColor() { 1102 return frameColor; 1103 } 1104 1105 protected Color getAxisColor() { 1106 return axisColor; 1107 } 1108 1109 protected Color getArrowColor() { 1110 return arrowColor; 1111 } 1112 1113 protected Color getBorderColor() { 1114 return borderColor; 1115 } 1116 1117 protected BasicStroke getBorderStroke() { 1118 return borderStroke; 1119 } 1120 1121 protected boolean showMinorGridX() { 1122 return minorGridX; 1123 } 1124 1125 protected boolean showMinorGridY() { 1126 return minorGridY; 1127 } 1128 1129 protected boolean showMajorGridX() { 1130 return majorGridX; 1131 } 1132 1133 protected boolean showMajorGridY() { 1134 return majorGridY; 1135 } 1136 1137 protected boolean showGridX() { 1138 return gridX; 1139 } 1140 1141 protected boolean showGridY() { 1142 return gridY; 1143 } 1144 1145 protected boolean drawFrontGrid() { 1146 return frontGrid; 1147 } 1148 1149 protected boolean showLegend() { 1150 return showLegend; 1151 } 1152 1153 protected boolean showSignature() { 1154 return drawSignature; 1155 } 1156 1157 protected boolean isFrontGrid() { 1158 return frontGrid; 1159 } 1160 1161 protected boolean useAntiAliasing() { 1162 return antiAliasing; 1163 } 1164 1165 protected int getChartLeftPadding() { 1166 return chart_lpadding; 1167 } 1168 1169 protected File getBackground() { 1170 return background; 1171 } 1172 1173 protected File getOverlay() { 1174 return overlay; 1175 } 1176 1177 protected double getBaseValue() { 1178 return baseValue; 1179 } 1180 1181 protected int getScaleIndex() { 1182 return scaleIndex; 1183 } 1184 1185 protected GridRange getGridRange() { 1186 return gridRange; 1187 } 1188 1189 protected ValueAxisUnit getValueAxis() { 1190 return vAxis; 1191 } 1192 1193 protected TimeAxisUnit getTimeAxis() { 1194 return tAxis; 1195 } 1196 1197 protected int getFirstDayOfWeek() { 1198 return firstDayOfWeek; 1199 } 1200 1201 protected PlotDef[] getPlotDefs() 1202 { 1203 return (PlotDef[]) plotDefs.toArray( new PlotDef[] {} ); 1204 } 1205 1206 protected Comment[] getComments() 1207 { 1208 return (Comment[]) comments.toArray( new Comment[] {} ); 1209 } 1210 1211 protected int getCommentLineCount() 1212 { 1213 return ( comments.size() > 0 ? commentLines + commentLineShift : 0 ); 1214 } 1215 1216 private void addComment( Comment cmt ) 1220 { 1221 commentLines += cmt.getLineCount(); 1222 commentLineShift = (cmt.isCompleteLine() ? 0 : 1); 1223 comments.add( cmt ); 1224 } 1225 1226 private void addLegend( String legend, Color color ) throws RrdException 1227 { 1228 addComment( new Legend(legend, color, plotDefs.size() - 1 ) ); 1231 } 1232} 1233 | Popular Tags |