View Javadoc

1   /* ============================================================
2    * JRobin : Pure java implementation of RRDTool's functionality
3    * ============================================================
4    *
5    * Project Info:  http://www.jrobin.org
6    * Project Lead:  Sasa Markovic (saxon@jrobin.org)
7    *
8    * Developers:    Sasa Markovic (saxon@jrobin.org)
9    *
10   *
11   * (C) Copyright 2003-2005, by Sasa Markovic.
12   *
13   * This library is free software; you can redistribute it and/or modify it under the terms
14   * of the GNU Lesser General Public License as published by the Free Software Foundation;
15   * either version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19   * See the GNU Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public License along with this
22   * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307, USA.
24   */
25  package org.jrobin.graph;
26  
27  import org.jrobin.core.Util;
28  
29  import java.awt.*;
30  
31  class ValueAxisMrtg implements RrdGraphConstants {
32  	private ImageParameters im;
33  	private ImageWorker worker;
34  	private RrdGraphDef gdef;
35  
36  	ValueAxisMrtg(RrdGraph rrdGraph) {
37  		this.im = rrdGraph.im;
38  		this.gdef = rrdGraph.gdef;
39  		this.worker = rrdGraph.worker;
40  		im.unit = gdef.unit;
41  	}
42  
43  	boolean draw() {
44  		Font font = gdef.smallFont;
45  		Paint mGridColor = gdef.colors[COLOR_MGRID];
46  		Paint fontColor = gdef.colors[COLOR_FONT];
47  		int labelOffset = (int) (worker.getFontAscent(font) / 2);
48  
49  		if (Double.isNaN((im.maxval - im.minval) / im.magfact)) {
50  			return false;
51  		}
52  
53  		int xLeft = im.xorigin;
54  		int xRight = im.xorigin + im.xsize;
55  		String labfmt;
56  		if (im.scaledstep / im.magfact * Math.max(Math.abs(im.quadrant), Math.abs(4 - im.quadrant)) <= 1.0) {
57  			labfmt = "%5.2f";
58  		}
59  		else {
60  			labfmt = Util.sprintf("%%4.%df", 1 - ((im.scaledstep / im.magfact > 10.0 || Math.ceil(im.scaledstep / im.magfact) == im.scaledstep / im.magfact) ? 1 : 0));
61  		}
62  		if (im.symbol != ' ' || im.unit != null) {
63  			labfmt += " ";
64  		}
65  		if (im.symbol != ' ') {
66  			labfmt += im.symbol;
67  		}
68  		if (im.unit != null) {
69  			labfmt += im.unit;
70  		}
71  		for (int i = 0; i <= 4; i++) {
72  			int y = im.yorigin - im.ysize * i / 4;
73  			if (y >= im.yorigin - im.ysize && y <= im.yorigin) {
74  				String graph_label = Util.sprintf(labfmt, im.scaledstep / im.magfact * (i - im.quadrant));
75  				int length = (int) (worker.getStringWidth(graph_label, font));
76  				worker.drawString(graph_label, xLeft - length - PADDING_VLABEL, y + labelOffset, font, fontColor);
77  				worker.drawLine(xLeft - 2, y, xLeft + 2, y, mGridColor, TICK_STROKE);
78  				worker.drawLine(xRight - 2, y, xRight + 2, y, mGridColor, TICK_STROKE);
79  				worker.drawLine(xLeft, y, xRight, y, mGridColor, GRID_STROKE);
80  			}
81  		}
82  		return true;
83  	}
84  
85  }