1 25 package org.jrobin.graph; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.*; 30 import javax.imageio.ImageIO ; 31 import java.awt.Font ; 32 import java.awt.Color ; 33 import java.awt.Graphics2D ; 34 import java.awt.BasicStroke ; 35 import java.awt.RenderingHints ; 36 import java.awt.image.RenderedImage ; 37 import java.awt.image.BufferedImage ; 38 39 import org.jrobin.core.RrdException; 40 41 46 class Grapher extends RrdExporter 47 { 48 protected static final String SPACER = " "; protected static final int GRAPH_RESOLUTION = 400; protected static final int DEFAULT_WIDTH = 400; protected static final int DEFAULT_HEIGHT = 100; 56 protected static final int UBORDER_SPACE = 10; protected static final int BBORDER_SPACE = 10; protected static final int LBORDER_SPACE = 10; protected static final int RBORDER_SPACE = 13; 62 protected static final int CHART_UPADDING = 5; protected static final int CHART_BPADDING = 25; protected static final int CHART_RPADDING = 10; protected static final int CHART_LPADDING = 50; protected static final int CHART_BPADDING_NM = 10; protected static final int CHART_LPADDING_NM = 10; 69 protected static final int LINE_PADDING = 4; 71 protected static final Font TITLE_FONT = new Font ("Lucida Sans Typewriter", Font.BOLD, 12); 73 protected static final Font NORMAL_FONT = new Font ("Lucida Sans Typewriter", Font.PLAIN, 10); 74 75 private Font title_font = TITLE_FONT; private Font normal_font = NORMAL_FONT; private Color normalFontColor = null; 78 private int numPoints = GRAPH_RESOLUTION; 80 private int chart_lpadding, chart_bpadding; private int imgWidth, imgHeight; private int chartWidth, chartHeight; private int nfont_width, nfont_height, tfont_width, tfont_height; private int commentBlock; private int graphOriginX, graphOriginY, x_offset, y_offset; 86 87 private RrdGraphDef graphDef; 88 89 private PlotDef[] plotDefs; 90 private long[] tsChart; 91 92 private ValueFormatter valueFormat; 93 private BasicStroke defaultStroke; 94 private ValueGrid vGrid; 95 private TimeGrid tGrid; 96 97 106 Grapher( RrdGraphDef graphDef, RrdGraph rrdGraph ) 107 { 108 super( graphDef, rrdGraph ); 109 this.graphDef = graphDef; 110 111 if ( graphDef.getDefaultFont() != null ) 113 normal_font = graphDef.getDefaultFont(); 114 if ( graphDef.getTitleFont() != null ) 115 title_font = graphDef.getTitleFont(); 116 normalFontColor = graphDef.getDefaultFontColor(); 117 118 nfont_height = normal_font.getSize(); nfont_width = nfont_height / 2 + 1; 120 121 tfont_height = ( title_font.isBold() ? title_font.getSize() + 2 : title_font.getSize() ); 123 tfont_width = ( title_font.isBold() ? tfont_height / 2 : tfont_height / 2 + 1 ); 124 125 valueFormat = new ValueFormatter( graphDef.getBaseValue(), graphDef.getScaleIndex() ); 127 128 defaultStroke = new BasicStroke (); 130 131 startTime = graphDef.getStartTime(); 132 endTime = graphDef.getStartTime(); 133 } 134 135 136 144 private void calculateDimensions( int cWidth, int cHeight ) 145 { 146 chartWidth = ( cWidth == 0 ? DEFAULT_WIDTH : cWidth ); 148 chartHeight = ( cHeight == 0 ? DEFAULT_HEIGHT : cHeight ); 149 150 if ( cWidth > 0 ) numPoints = cWidth; 151 152 chart_lpadding = ( graphDef.showMajorGridY() ? graphDef.getChartLeftPadding() : CHART_LPADDING_NM ); 154 chart_bpadding = ( graphDef.showMajorGridX() ? CHART_BPADDING : CHART_BPADDING_NM ); 155 156 commentBlock = 0; 158 if ( graphDef.showLegend() ) 159 commentBlock = graphDef.getCommentLineCount() * (nfont_height + LINE_PADDING) - LINE_PADDING; 160 161 x_offset = LBORDER_SPACE; 163 if ( graphDef.getVerticalLabel() != null ) 164 x_offset += nfont_height + LINE_PADDING; 165 imgWidth = chartWidth + x_offset + RBORDER_SPACE + chart_lpadding + CHART_RPADDING; 166 167 y_offset = UBORDER_SPACE; 168 if ( graphDef.getTitle() != null ) y_offset += ((tfont_height + LINE_PADDING) * graphDef.getTitle().getLineCount() + tfont_height) + LINE_PADDING; 170 imgHeight = chartHeight + commentBlock + y_offset + BBORDER_SPACE + CHART_UPADDING + CHART_BPADDING; 171 } 172 173 178 private void calculateDimensionsGlobal( int cWidth, int cHeight ) 179 { 180 imgWidth = cWidth; 181 imgHeight = cHeight; 182 183 if ( cWidth > 0 ) numPoints = cWidth; 184 185 chart_lpadding = ( graphDef.showMajorGridY() ? graphDef.getChartLeftPadding() : CHART_LPADDING_NM ); 187 chart_bpadding = ( graphDef.showMajorGridX() ? CHART_BPADDING : CHART_BPADDING_NM ); 188 189 commentBlock = 0; 191 if ( graphDef.showLegend() ) 192 commentBlock = graphDef.getCommentLineCount() * (nfont_height + LINE_PADDING) - LINE_PADDING; 193 194 x_offset = LBORDER_SPACE; 196 if ( graphDef.getVerticalLabel() != null ) 197 x_offset += nfont_height + LINE_PADDING; 198 chartWidth = imgWidth - x_offset - RBORDER_SPACE - chart_lpadding - CHART_RPADDING; 199 200 y_offset = UBORDER_SPACE; 201 if ( graphDef.getTitle() != null ) y_offset += ((tfont_height + LINE_PADDING) * graphDef.getTitle().getLineCount() + tfont_height) + LINE_PADDING; 203 chartHeight = imgHeight - commentBlock - y_offset - BBORDER_SPACE - CHART_UPADDING - CHART_BPADDING; 204 } 205 206 215 protected BufferedImage createImage( int cWidth, int cHeight, int colorType ) throws RrdException, IOException 216 { 217 calculateDimensions( cWidth, cHeight ); 218 219 BufferedImage bImg = new BufferedImage ( imgWidth, imgHeight, colorType ); 221 Graphics2D graphics = (Graphics2D ) bImg.getGraphics(); 222 223 render( graphics ); 224 225 graphics.dispose(); 227 228 return bImg; 229 } 230 231 240 protected BufferedImage createImageGlobal( int cWidth, int cHeight, int colorType ) throws RrdException, IOException 241 { 242 calculateDimensionsGlobal( cWidth, cHeight ); 243 244 BufferedImage bImg = new BufferedImage ( imgWidth, imgHeight, colorType ); 246 Graphics2D graphics = (Graphics2D ) bImg.getGraphics(); 247 248 render( graphics ); 249 250 graphics.dispose(); 252 253 return bImg; 254 } 255 256 266 protected void renderImage( int cWidth, int cHeight, Graphics2D graphics, boolean useGlobal ) throws RrdException, IOException 267 { 268 if ( useGlobal ) 269 calculateDimensionsGlobal( cWidth, cHeight ); 270 else 271 calculateDimensions( cWidth, cHeight ); 272 273 render( graphics ); 274 } 275 276 285 protected boolean shouldGenerate( long prevGenTime ) throws RrdException, IOException 286 { 287 FetchSourceList fetchSources = graphDef.getFetchSources(); 288 fetchSources.setRrdOpener( getRrdOpener() ); 289 290 fetchSources.openAll(); 291 292 if ( graphDef.isLazy() && fetchSources.getLastUpdateTime() * 1000 < prevGenTime ) 293 { 294 fetchSources.releaseAll(); 296 297 return false; 298 } 299 300 return true; 301 } 302 303 312 private void render( Graphics2D graphics ) throws RrdException, IOException 313 { 314 calculateSeries(); 317 plotImageBackground( graphics ); 319 plotChart( graphics ); 321 plotComments( graphics ); 323 plotOverlay( graphics ); 325 plotSignature( graphics ); 327 graphics.dispose(); 329 } 330 331 337 private void calculateSeries() throws RrdException, IOException 338 { 339 super.calculateSeries( chartWidth ); 341 342 numPoints = numRows; 343 344 348 tsChart = new long[ chartWidth ]; 349 plotDefs = graphDef.getPlotDefs(); 350 351 for ( int i = 0; i < plotDefs.length; i++ ) 352 { 353 plotDefs[i].setSource( sources, sourceIndex ); 354 plotDefs[i].prepareValues( chartWidth ); 355 } 356 357 for ( int i = 0; i < chartWidth; i++ ) 358 { 359 long t = (long) (startTime + i * ((endTime - startTime) / (double) (chartWidth - 1))); 360 361 for ( int j = 0; j < plotDefs.length; j++ ) 362 plotDefs[j].setValue( i, t, timestamps ); 363 364 tsChart[i] = t; 365 } 366 } 367 368 372 private void plotImageBackground( Graphics2D g ) 373 { 374 g.setColor( graphDef.getBackColor() ); 376 g.fillRect(0, 0, imgWidth, imgHeight ); 377 378 try { 380 File bgImage = graphDef.getBackground(); 381 if ( bgImage != null ) { 382 RenderedImage img = ImageIO.read(bgImage); 383 g.drawRenderedImage( img, null ); 384 } 385 } catch (IOException e) {} 386 387 Color bc = graphDef.getBorderColor(); 389 BasicStroke bs = graphDef.getBorderStroke(); 390 391 if ( bs != null && bc != null ) { 393 g.setColor( bc ); 394 g.setStroke( bs ); 395 396 int w = new Float (bs.getLineWidth()).intValue(); 398 if ( w > 0 ) 399 g.drawRect( w / 2, w / 2, imgWidth - w, imgHeight - w); 400 401 g.setStroke( defaultStroke ); 402 } 403 else { 405 g.setColor( new Color ( 0xdc, 0xdc, 0xdc ) ); 406 g.fillRect( 0, 0, 2, imgHeight - 1 ); 407 g.fillRect( 0, 0, imgWidth - 1, 2 ); 408 g.setColor( Color.GRAY ); 409 g.drawLine( 0, imgHeight - 1, imgWidth, imgHeight - 1 ); 410 g.drawLine( imgWidth - 1, 0, imgWidth - 1, imgHeight ); 411 g.drawLine( 1, imgHeight - 2, imgWidth, imgHeight - 2 ); 412 g.drawLine( imgWidth - 2, 1, imgWidth - 2, imgHeight ); 413 } 414 415 plotImageTitle( g ); 416 417 plotVerticalLabel( g ); 418 } 419 420 425 private void plotChart( Graphics2D graphics ) throws RrdException 426 { 427 int lux = x_offset + chart_lpadding; 428 int luy = y_offset + CHART_UPADDING; 429 430 if ( graphDef.getBackground() == null ) { 433 graphics.setColor( graphDef.getCanvasColor() ); 434 graphics.fillRect( lux, luy, chartWidth, chartHeight ); 435 } 436 437 graphics.setColor( graphDef.getFrameColor() ); 439 graphics.drawRect( lux, luy, chartWidth, chartHeight ); 440 441 double val; 442 double[] tmpSeries = new double[numPoints]; 443 444 boolean rigid = false; 445 double lowerValue = Double.MAX_VALUE; 446 double upperValue = Double.MIN_VALUE; 447 448 GridRange range = graphDef.getGridRange(); 449 if ( range != null ) 450 { 451 rigid = range.isRigid(); 452 lowerValue = range.getLowerValue(); 453 upperValue = range.getUpperValue(); 454 455 if ( Double.isNaN(lowerValue) ) lowerValue = Double.MAX_VALUE; 456 if ( Double.isNaN(upperValue) ) upperValue = Double.MIN_VALUE; 457 } 458 459 for ( int i = 0; i < plotDefs.length; i++ ) 461 { 462 Source src = plotDefs[i].getSource(); 463 464 if ( !rigid && src != null ) 466 { 467 double min = src.getAggregate( Source.AGG_MINIMUM ); 468 double max = src.getAggregate( Source.AGG_MAXIMUM ); 469 470 if ( plotDefs[i].plotType == PlotDef.PLOT_STACK && i >= 1 ) 472 { 473 if ( plotDefs[i - 1].plotType == PlotDef.PLOT_STACK ) { 475 for (int j = 0; j < tmpSeries.length; j++) 476 { 477 val = tmpSeries[j] + plotDefs[i].getValue(j, timestamps); 478 479 if ( val < lowerValue ) lowerValue = val; 480 if ( val > upperValue ) upperValue = val; 481 482 tmpSeries[j] = val; 483 } 484 } 485 else { 487 for (int j = 0; j < tmpSeries.length; j++) 488 { 489 val = plotDefs[i - 1].getValue(j, timestamps) + plotDefs[i].getValue(j, timestamps); 490 491 if ( val < lowerValue ) lowerValue = val; 492 if ( val > upperValue ) upperValue = val; 493 494 tmpSeries[j] = val; 495 } 496 497 } 498 } 499 else { 501 if ( min < lowerValue ) lowerValue = min; 502 if ( max > upperValue ) upperValue = max; 503 } 504 } 505 506 } 507 508 vGrid = new ValueGrid( range, lowerValue, upperValue, graphDef.getValueAxis(), graphDef.getBaseValue() ); 509 tGrid = new TimeGrid( startTime, endTime, graphDef.getTimeAxis(), graphDef.getFirstDayOfWeek() ); 510 511 lowerValue = vGrid.getLowerValue(); 512 upperValue = vGrid.getUpperValue(); 513 514 ChartGraphics g = new ChartGraphics( graphics ); 516 g.setDimensions( chartWidth, chartHeight ); 517 g.setXRange( tGrid.getStartTime(), tGrid.getEndTime() ); 518 g.setYRange( lowerValue, upperValue ); 519 520 double diff = 1.0d; 522 if ( lowerValue < 0 ) 523 diff = 1.0d - ( lowerValue / ( -upperValue + lowerValue )); 524 graphOriginX = lux; 525 graphOriginY = new Double (luy + chartHeight * diff).intValue(); 526 527 if ( !graphDef.isFrontGrid() ) plotChartGrid( g ); 529 530 if ( graphDef.useAntiAliasing() ) 532 graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); 533 534 graphics.setClip( lux, luy, chartWidth, chartHeight); 536 graphics.translate( graphOriginX, graphOriginY ); 537 538 int lastPlotType = PlotDef.PLOT_LINE; 539 double[] parentSeries = new double[tsChart.length]; 540 541 int[] xValues = new int[tsChart.length]; 543 for (int i = 0; i < tsChart.length; i++) 544 xValues[i] = g.getX(tsChart[i]); 545 546 for ( int i = 0; i < plotDefs.length; i++ ) 548 { 549 plotDefs[i].draw( g, xValues, parentSeries, lastPlotType ); 550 if( plotDefs[i].plotType != PlotDef.PLOT_STACK ) 551 lastPlotType = plotDefs[i].plotType; 552 } 553 554 graphics.translate( -graphOriginX, -graphOriginY ); 556 graphics.setClip( 0, 0, imgWidth, imgHeight); 557 graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF ); 558 559 if ( graphDef.isFrontGrid() ) plotChartGrid( g ); 561 } 562 563 568 private void plotChartGrid( ChartGraphics chartGraph ) 569 { 570 Graphics2D g = chartGraph.getGraphics(); 571 g.setFont( normal_font ); 572 573 int lux = x_offset + chart_lpadding; 574 int luy = y_offset + CHART_UPADDING; 575 576 boolean minorX = graphDef.showMinorGridX(); 577 boolean minorY = graphDef.showMinorGridY(); 578 boolean majorX = graphDef.showMajorGridX(); 579 boolean majorY = graphDef.showMajorGridY(); 580 581 Color minColor = graphDef.getMinorGridColor(); 582 Color majColor = graphDef.getMajorGridColor(); 583 584 float[] dashPattern = { 1, 1 }; 586 BasicStroke dStroke = new BasicStroke (1, BasicStroke.CAP_BUTT, 587 BasicStroke.JOIN_MITER, 10, 588 dashPattern, 0); 589 590 int tmpx = lux + chartWidth; 592 int tmpy = luy + chartHeight; 593 594 g.setColor( graphDef.getAxisColor() ); 596 g.drawLine( lux - 4, tmpy, tmpx + 4, tmpy ); 597 g.setColor( graphDef.getArrowColor() ); 598 g.drawLine( tmpx + 4, tmpy - 3, tmpx + 4, tmpy + 3 ); 599 g.drawLine( tmpx + 4, tmpy - 3, tmpx + 9, tmpy ); 600 g.drawLine( tmpx + 4, tmpy + 3, tmpx + 9, tmpy ); 601 602 if ( graphDef.showGridX() ) 604 { 605 TimeMarker[] timeList = tGrid.getTimeMarkers(); 606 boolean labelCentered = tGrid.centerLabels(); 607 long labelGridWidth = tGrid.getMajorGridWidth(); 608 609 int pixWidth = 0; 610 if ( labelCentered ) 611 pixWidth = ( chartGraph.getX( labelGridWidth ) - chartGraph.getX( 0 ) ); 612 613 for (int i = 0; i < timeList.length; i++) 614 { 615 long secTime = timeList[i].getTimestamp(); 616 int posRel = chartGraph.getX(secTime); 617 int pos = lux + posRel; 618 String label = timeList[i].getLabel(); 619 620 if ( posRel >= 0 ) { 621 if ( majorX && timeList[i].isLabel() ) 622 { 623 g.setColor( majColor ); 624 g.setStroke( dStroke ); 625 g.drawLine( pos, luy, pos, luy + chartHeight ); 626 g.setStroke( defaultStroke ); 627 g.drawLine( pos, luy - 2, pos, luy + 2 ); 628 g.drawLine( pos, luy + chartHeight - 2, pos, luy + chartHeight + 2 ); 629 int txtDistance = (label.length() * nfont_width) / 2; 632 633 if ( labelCentered ) 634 { 635 if ( pos + pixWidth <= lux + chartWidth ) 636 graphString( g, label, pos + 2 + pixWidth/2 - txtDistance, luy + chartHeight + nfont_height + LINE_PADDING ); 637 } 638 else if ( (pos - lux > txtDistance + 2) && (pos + txtDistance + 2 < lux + chartWidth) ) 639 graphString( g, label, pos - txtDistance, luy + chartHeight + nfont_height + LINE_PADDING ); 640 } 641 else if ( minorX ) 642 { 643 g.setColor( minColor ); 644 g.setStroke( dStroke ); 645 g.drawLine( pos, luy, pos, luy + chartHeight ); 646 g.setStroke( defaultStroke ); 647 g.drawLine( pos, luy - 1, pos, luy + 1 ); 648 g.drawLine( pos, luy + chartHeight - 1, pos, luy + chartHeight + 1 ); 649 650 } 651 } 652 } 653 } 654 655 valueFormat.setScaling( true, false ); if ( graphDef.showGridY() ) 658 { 659 ValueMarker[] valueList = vGrid.getValueMarkers(); 660 661 for (int i = 0; i < valueList.length; i++) 662 { 663 int valRel = chartGraph.getY( valueList[i].getValue() ); 664 665 valueFormat.setFormat( valueList[i].getValue(), 2, 0 ); 666 String label = (valueFormat.getScaledValue() + " " + valueFormat.getPrefix()).trim(); 667 668 if ( majorY && valueList[i].isMajor() ) 669 { 670 g.setColor( majColor ); 671 g.setStroke( dStroke ); 672 g.drawLine( graphOriginX, graphOriginY - valRel, graphOriginX + chartWidth, graphOriginY - valRel ); 673 g.setStroke( defaultStroke ); 674 g.drawLine( graphOriginX - 2, graphOriginY - valRel, graphOriginX + 2, graphOriginY - valRel ); 675 g.drawLine( graphOriginX + chartWidth - 2, graphOriginY - valRel, graphOriginX + chartWidth + 2, graphOriginY - valRel ); 676 graphString( g, label, graphOriginX - (label.length() * nfont_width) - 7, graphOriginY - valRel + nfont_height/2 - 1 ); 677 } 678 else if ( minorY ) 679 { 680 g.setColor( minColor ); 681 g.setStroke( dStroke ); 682 g.drawLine( graphOriginX, graphOriginY - valRel, graphOriginX + chartWidth, graphOriginY - valRel ); 683 g.setStroke( defaultStroke ); 684 g.drawLine( graphOriginX - 1, graphOriginY - valRel, graphOriginX + 1, graphOriginY - valRel ); 685 g.drawLine( graphOriginX + chartWidth - 1, graphOriginY - valRel, graphOriginX + chartWidth + 1, graphOriginY - valRel ); 686 } 687 688 } 689 } 690 691 } 692 693 698 private void plotComments( Graphics2D g ) throws RrdException 699 { 700 if ( !graphDef.showLegend() ) return; 701 702 LinkedList markerList = new LinkedList(); 703 704 int posy = y_offset + chartHeight + CHART_UPADDING + CHART_BPADDING + ( graphDef.showMajorGridX() ? nfont_height : 0 ); 706 int posx = LBORDER_SPACE; 707 708 g.setColor( normalFontColor ); 709 g.setFont( normal_font ); 710 711 Comment[] clist = graphDef.getComments(); 712 StringBuffer tmpStr = new StringBuffer (""); 713 714 boolean newLine = false; 715 boolean drawText = false; 716 717 for (int i = 0; i < clist.length; i++) 718 { 719 if ( clist[i].commentType == Comment.CMT_LEGEND ) 720 { 721 markerList.addLast( new LegendMarker( tmpStr.length() * nfont_width, ((Legend) clist[i]).getColor() ) ); 722 tmpStr.append( " " ); } 724 else if ( clist[i].commentType == Comment.CMT_GPRINT ) 725 ((Gprint) clist[i]).setValue( sources, sourceIndex, valueFormat ); 726 727 ArrayList tknpairs = clist[i].getTokens(); 728 729 for (int j = 0; j < tknpairs.size(); j++) 730 { 731 String str = (String ) tknpairs.get(j++); 732 Byte tkn = (Byte ) tknpairs.get(j); 733 734 if ( clist[i].trimString() ) 735 tmpStr.append( str.trim() ); 736 else 737 tmpStr.append( str ); 738 739 if ( tkn != Comment.TKN_NULL ) 740 { 741 drawText = true; 742 if ( tkn == Comment.TKN_ALF ) { 743 newLine = true; 744 posx = LBORDER_SPACE; 745 } 746 else if ( tkn == Comment.TKN_ARF ) { 747 newLine = true; 748 posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * nfont_width); 749 } 750 else if ( tkn == Comment.TKN_ACF ) { 751 newLine = true; 752 posx = imgWidth / 2 - (tmpStr.length() * nfont_width) / 2; 753 } 754 else if ( tkn == Comment.TKN_AL ) 755 posx = LBORDER_SPACE; 756 else if ( tkn == Comment.TKN_AR ) 757 posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * nfont_width); 758 else if ( tkn == Comment.TKN_AC ) 759 posx = imgWidth / 2 - (tmpStr.length() * nfont_width) / 2; 760 } 761 762 if ( !newLine && clist[i].addSpacer() ) 763 tmpStr.append( SPACER ); 764 765 if ( drawText ) { 767 768 graphString( g, tmpStr.toString(), posx, posy ); 769 tmpStr = new StringBuffer (""); 770 drawText = false; 771 772 while ( !markerList.isEmpty() ) { 774 LegendMarker lm = (LegendMarker) markerList.removeFirst(); 775 g.setColor( lm.getColor() ); 776 g.fillRect( posx + lm.getXPosition(), posy - 9, 10, 10 ); 777 g.setColor( normalFontColor ); 778 g.drawRect( posx + lm.getXPosition(), posy - 9, 10, 10 ); 779 } 780 } 781 782 if ( newLine ) { 783 posy += nfont_height + LINE_PADDING; 784 newLine = false; 785 } 786 787 } 788 } 789 790 if ( tmpStr.length() > 0) 791 { 792 posx = LBORDER_SPACE; 793 graphString( g, tmpStr.toString(), posx, posy ); 794 tmpStr = new StringBuffer (""); 795 drawText = false; 796 797 while ( !markerList.isEmpty() ) { 799 LegendMarker lm = (LegendMarker) markerList.removeFirst(); 800 g.setColor( lm.getColor() ); 801 g.fillRect( posx + lm.getXPosition(), posy - 9, 10, 10 ); 802 g.setColor( normalFontColor ); 803 g.drawRect( posx + lm.getXPosition(), posy - 9, 10, 10 ); 804 } 805 } 806 } 807 808 813 private void plotOverlay( Graphics2D g ) 814 { 815 try 817 { 818 File overlayImg = graphDef.getOverlay(); 819 if ( overlayImg != null ) 820 { 821 BufferedImage img = ImageIO.read(overlayImg); 822 823 int w = img.getWidth(); 824 int h = img.getHeight(); 825 int rgbWhite = Color.WHITE.getRGB(); 826 int pcolor, red, green, blue; 827 828 for (int i = 0; i < w; i++) { 831 for (int j = 0; j < h; j++) { 832 pcolor = img.getRGB(i, j); 833 if ( pcolor != rgbWhite ) 834 { 835 red = (pcolor >> 16) & 0xff; 836 green = (pcolor >> 8) & 0xff; 837 blue = pcolor & 0xff; 838 839 g.setColor( new Color (red, green, blue) ); 840 g.drawLine( i, j, i, j ); 841 } 842 } 843 } 844 } 845 } catch (IOException e) {} 846 } 847 848 852 private void plotImageTitle( Graphics2D g ) 853 { 854 Title graphTitle = graphDef.getTitle(); 855 856 if ( graphTitle == null ) 858 return; 859 860 int posy = tfont_height - 1 + UBORDER_SPACE; 862 int posx = LBORDER_SPACE; 863 864 g.setColor( graphDef.getTitleFontColor() ); 866 g.setFont( title_font ); 867 868 StringBuffer tmpStr = new StringBuffer (""); 870 boolean newLine = false; 871 872 ArrayList tknpairs = graphTitle.getTokens(); 873 for (int j = 0; j < tknpairs.size(); j++) 874 { 875 String str = (String ) tknpairs.get(j++); 876 Byte tkn = (Byte ) tknpairs.get(j); 877 878 tmpStr.append( str ); 879 if ( tkn != Comment.TKN_NULL ) 880 { 881 if ( tkn == Comment.TKN_ALF ) { 882 newLine = true; 883 posx = LBORDER_SPACE; 884 } 885 else if ( tkn == Comment.TKN_ARF ) { 886 newLine = true; 887 posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * tfont_width) - tfont_width; 888 } 889 else if ( tkn == Comment.TKN_ACF ) { 890 newLine = true; 891 posx = imgWidth / 2 - (tmpStr.length() * tfont_width) / 2; 892 } 893 else if ( tkn == Comment.TKN_AL ) 894 posx = LBORDER_SPACE; 895 else if ( tkn == Comment.TKN_AR ) 896 posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * tfont_width) - tfont_width; 897 else if ( tkn == Comment.TKN_AC ) 898 posx = imgWidth / 2 - (tmpStr.length() * tfont_width) / 2; 899 } 900 else { posx = imgWidth / 2 - (tmpStr.length() * tfont_width) / 2; 902 } 903 904 g.drawString( tmpStr.toString(), posx, posy ); 906 tmpStr = new StringBuffer (""); 907 908 if ( newLine ) 910 { 911 posy += tfont_height + LINE_PADDING; 912 newLine = false; 913 } 914 } 915 916 } 917 918 922 private void plotVerticalLabel( Graphics2D g ) 923 { 924 String valueAxisLabel = graphDef.getVerticalLabel(); 925 926 if ( valueAxisLabel == null ) 927 return; 928 929 g.setColor( normalFontColor ); 930 int labelWidth = valueAxisLabel.length() * nfont_width; 931 932 g.setFont( normal_font ); 934 g.rotate( -Math.PI/2.0 ); 935 graphString( g, valueAxisLabel, - y_offset - CHART_UPADDING 936 - chartHeight / 2 937 - labelWidth / 2, 938 LBORDER_SPACE + nfont_height 939 ); 940 g.rotate( Math.PI/2.0 ); 941 } 942 943 947 private void plotSignature( Graphics2D g ) 948 { 949 if ( !graphDef.showSignature() ) 950 return; 951 952 String sig = "www.jrobin.org"; 953 g.setColor( Color.GRAY ); 954 g.setFont( new Font ("Courier", Font.PLAIN, 10) ); 955 956 g.rotate( Math.PI/2.0 ); 957 g.drawString( sig, 5, - imgWidth + 9 ); 958 g.rotate( -Math.PI/2.0 ); 959 } 960 961 968 private void graphString( Graphics2D g, String str, int x, int y ) 969 { 970 Color oc = g.getColor(); 971 972 g.setColor( normalFontColor ); 973 g.drawString( str, x, y ); 974 975 g.setColor( oc ); 976 } 977 } 978 | Popular Tags |