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 ValueAxisLogarithmic implements RrdGraphConstants {
32  	private static final double[][] yloglab = {
33  			{1e9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
34  			{1e3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
35  			{1e1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
36  			/* {  1e1, 1,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0 }, */
37  			{1e1, 1, 2.5, 5, 7.5, 0, 0, 0, 0, 0, 0, 0},
38  			{1e1, 1, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0},
39  			{1e1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0},
40  			{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
41  	};
42  
43  	private RrdGraph rrdGraph;
44  	private ImageParameters im;
45  	private ImageWorker worker;
46  	private RrdGraphDef gdef;
47  
48  	ValueAxisLogarithmic(RrdGraph rrdGraph) {
49  		this.rrdGraph = rrdGraph;
50  		this.im = rrdGraph.im;
51  		this.gdef = rrdGraph.gdef;
52  		this.worker = rrdGraph.worker;
53  	}
54  
55  	boolean draw() {
56  		Font font = gdef.smallFont;
57  		Paint gridColor = gdef.colors[COLOR_GRID];
58  		Paint mGridColor = gdef.colors[COLOR_MGRID];
59  		Paint fontColor = gdef.colors[COLOR_FONT];
60  		int fontHeight = (int) Math.ceil(rrdGraph.getSmallFontHeight());
61  		int labelOffset = (int) (worker.getFontAscent(font) / 2);
62  
63  		double pixpex = (double) im.ysize / (Math.log10(im.maxval) - Math.log10(im.minval));
64  		if (Double.isNaN(pixpex)) {
65  			return false;
66  		}
67  		double minstep, pixperstep;
68  		int minoridx = 0, majoridx = 0;
69  		for (int i = 0; yloglab[i][0] > 0; i++) {
70  			minstep = Math.log10(yloglab[i][0]);
71  			for (int ii = 1; yloglab[i][ii + 1] > 0; ii++) {
72  				if (yloglab[i][ii + 2] == 0) {
73  					minstep = Math.log10(yloglab[i][ii + 1]) - Math.log10(yloglab[i][ii]);
74  					break;
75  				}
76  			}
77  			pixperstep = pixpex * minstep;
78  			if (pixperstep > 5) {
79  				minoridx = i;
80  			}
81  			if (pixperstep > 2 * fontHeight) {
82  				majoridx = i;
83  			}
84  		}
85  		int x0 = im.xorigin, x1 = x0 + im.xsize;
86  		for (double value = Math.pow(10, Math.log10(im.minval)
87  				- Math.log10(im.minval) % Math.log10(yloglab[minoridx][0]));
88  			 value <= im.maxval;
89  			 value *= yloglab[minoridx][0]) {
90  			if (value < im.minval) {
91  				continue;
92  			}
93  			int i = 0;
94  			while (yloglab[minoridx][++i] > 0) {
95  				int y = rrdGraph.mapper.ytr(value * yloglab[minoridx][i]);
96  				if (y <= im.yorigin - im.ysize) {
97  					break;
98  				}
99  				worker.drawLine(x0 - 1, y, x0 + 1, y, gridColor, TICK_STROKE);
100 				worker.drawLine(x1 - 1, y, x1 + 1, y, gridColor, TICK_STROKE);
101 				worker.drawLine(x0, y, x1, y, gridColor, GRID_STROKE);
102 			}
103 		}
104 		for (double value = Math.pow(10, Math.log10(im.minval)
105 				- (Math.log10(im.minval) % Math.log10(yloglab[majoridx][0])));
106 			 value <= im.maxval;
107 			 value *= yloglab[majoridx][0]) {
108 			if (value < im.minval) {
109 				continue;
110 			}
111 			int i = 0;
112 			while (yloglab[majoridx][++i] > 0) {
113 				int y = rrdGraph.mapper.ytr(value * yloglab[majoridx][i]);
114 				if (y <= im.yorigin - im.ysize) {
115 					break;
116 				}
117 				worker.drawLine(x0 - 2, y, x0 + 2, y, mGridColor, TICK_STROKE);
118 				worker.drawLine(x1 - 2, y, x1 + 2, y, mGridColor, TICK_STROKE);
119 				worker.drawLine(x0, y, x1, y, mGridColor, GRID_STROKE);
120 				String graph_label = Util.sprintf("%3.0e", value * yloglab[majoridx][i]);
121 				int length = (int) (worker.getStringWidth(graph_label, font));
122 				worker.drawString(graph_label, x0 - length - PADDING_VLABEL, y + labelOffset, font, fontColor);
123 			}
124 		}
125 		return true;
126 	}
127 }