1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.gwt.utgb.client.canvas;
24
25 import org.utgenome.gwt.utgb.client.canvas.GWTGraphCanvas.GraphStyle;
26
27
28
29
30
31
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
77
78
79
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 }