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 class Mapper {
28     private RrdGraphDef gdef;
29     private ImageParameters im;
30     private double pixieX, pixieY;
31
32     Mapper(RrdGraph rrdGraph) {
33         this.gdef = rrdGraph.gdef;
34         this.im = rrdGraph.im;
35         pixieX = (double) im.xsize / (double) (im.end - im.start);
36         if (!gdef.logarithmic) {
37             pixieY = (double) im.ysize / (im.maxval - im.minval);
38         }
39         else {
40             pixieY = (double) im.ysize / (Math.log10(im.maxval) - Math.log10(im.minval));
41         }
42     }
43
44     int xtr(double mytime) {
45         return (int) ((double) im.xorigin + pixieX * (mytime - im.start));
46     }
47
48     int ytr(double value) {
49         double yval;
50         if (!gdef.logarithmic) {
51             yval = im.yorigin - pixieY * (value - im.minval) + 0.5;
52         }
53         else {
54             if (value < im.minval) {
55                 yval = im.yorigin;
56             }
57             else {
58                 yval = im.yorigin - pixieY * (Math.log10(value) - Math.log10(im.minval)) + 0.5;
59             }
60         }
61         if (!gdef.rigid) {
62             return (int) yval;
63         }
64         else if ((int) yval > im.yorigin) {
65             return im.yorigin + 2;
66         }
67         else if ((int) yval < im.yorigin - im.ysize) {
68             return im.yorigin - im.ysize - 2;
69         }
70         else {
71             return (int) yval;
72         }
73     }
74 }