View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // Scale.java
20  // Since: 2010/09/28
21  //
22  //--------------------------------------
23  package org.utgenome.gwt.utgb.client.canvas;
24  
25  import org.utgenome.gwt.utgb.client.canvas.GWTGraphCanvas.GraphStyle;
26  
27  /**
28   * Scale for drawing bar graphs
29   * 
30   * 
31   * @author leo
32   * 
33   */
34  public class Scale {
35  
36  	private float min;
37  	private float max;
38  	private int height;
39  	private boolean logScale = false;
40  	private float logBase = 2;
41  	private boolean isReverseYAxis = false;
42  
43  	public Scale() {
44  
45  	}
46  
47  	public Scale(GraphStyle style) {
48  		updateStyle(style);
49  	}
50  
51  	public float getMin() {
52  		return min;
53  	}
54  
55  	public float getMax() {
56  		return max;
57  	}
58  
59  	private void setMinMax(float min, float max) {
60  		this.min = min;
61  		this.max = max;
62  		isReverseYAxis = min > max;
63  	}
64  
65  	public void updateStyle(GraphStyle style) {
66  		this.height = style.windowHeight;
67  		this.logScale = style.logScale;
68  		this.logBase = style.logBase;
69  		if (!style.autoScale)
70  			setMinMax(style.minValue, style.maxValue);
71  		else
72  			setMinMax(style.autoScaledMin, style.autoScaledMax);
73  	}
74  
75  	/**
76  	 * Return pixel Y position of the given graph value
77  	 * 
78  	 * @param value
79  	 * @return
80  	 */
81  	float getYPosition(float value) {
82  
83  		if (min == max)
84  			return 0.0f;
85  
86  		float tempMin = max < min ? max : min;
87  		float tempMax = max > min ? max : min;
88  
89  		if (logScale) {
90  			value = getLogValue(value);
91  			tempMax = getLogValue(tempMax);
92  			tempMin = getLogValue(tempMin);
93  		}
94  		float valueHeight = (value - tempMin) / (tempMax - tempMin) * height;
95  
96  		if (isReverseYAxis)
97  			return valueHeight;
98  		else
99  			return height - valueHeight;
100 	}
101 
102 	public float getLogValue(float value) {
103 		if (Math.log(logBase) == 0.0)
104 			return value;
105 
106 		float temp = 0.0f;
107 		if (value > 0.0f) {
108 			temp = (float) (Math.log(value) / Math.log(logBase) + 1.0);
109 			if (temp < 0.0f)
110 				temp = 0.0f;
111 		}
112 		else if (value < 0.0f) {
113 			temp = (float) (Math.log(-value) / Math.log(logBase) + 1.0);
114 			if (temp < 0.0f)
115 				temp = 0.0f;
116 			temp *= -1.0f;
117 		}
118 		return temp;
119 	}
120 
121 }