1
2
3
4
5
6
7 package org.utgenome.gwt.utgb.server.app;
8
9 import java.awt.Color;
10 import java.awt.Font;
11 import java.awt.Graphics2D;
12 import java.awt.image.BufferedImage;
13 import java.io.IOException;
14
15 import javax.imageio.ImageIO;
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.utgenome.gwt.utgb.server.WebTrackBase;
21 import org.xerial.util.log.Logger;
22
23
24
25
26
27 public class ScaleBar extends WebTrackBase {
28 private static final long serialVersionUID = 1L;
29 private static Logger _logger = Logger.getLogger(ScaleBar.class);
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public long range;
44 public int width = 700;
45 public int height = 20;
46
47 private final int[] template = { 5, 2, 1 };
48 private BufferedImage image;
49 private Graphics2D g;
50
51
52
53
54
55
56
57 public ScaleBar() {
58 }
59
60 @Override
61 public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62
63 image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
64 g = image.createGraphics();
65 draw();
66
67 response.setContentType("image/png");
68 ImageIO.write(image, "png", response.getOutputStream());
69
70
71
72
73 }
74
75 private void draw() {
76 long half_range = range / 2;
77 int digit = Long.toString(half_range).length();
78 long unit = (long) Math.pow(10, digit - 1);
79
80 for (int i = 0; i < template.length; i++) {
81 if (half_range >= template[i] * unit) {
82 unit *= template[i];
83 break;
84 }
85 }
86 double ratio = (double) unit / (double) range / 2.0;
87 int x_left = (int) ((0.5 - ratio) * width + 0.5);
88 int x_right = (int) ((0.5 + ratio) * width + 0.5);
89
90 int x_1 = (int) ((0.5 - ratio * 0.8) * width + 0.5);
91 int x_2 = (int) ((0.5 - ratio * 0.6) * width + 0.5);
92 int x_3 = (int) ((0.5 - ratio * 0.4) * width + 0.5);
93 int x_4 = (int) ((0.5 - ratio * 0.2) * width + 0.5);
94 int x_c = (int) (0.5 * width + 0.5);
95 int x_6 = (int) ((0.5 + ratio * 0.2) * width + 0.5);
96 int x_7 = (int) ((0.5 + ratio * 0.4) * width + 0.5);
97 int x_8 = (int) ((0.5 + ratio * 0.6) * width + 0.5);
98 int x_9 = (int) ((0.5 + ratio * 0.8) * width + 0.5);
99
100 int y_lr_u = height * 7 / 20;
101 int y_lr_l = height * 13 / 20;
102 int y_c_u = height * 2 / 5;
103 int y_c_l = height * 3 / 5;
104 ;
105 int y_o_u = height * 9 / 20;
106 ;
107 int y_o_l = height * 11 / 20;
108
109 int unit_digit = Long.toString(unit).length();
110 int unit_remainder = (unit_digit - 1) % 3;
111 unit_remainder++;
112 int unit_quotient = (unit_digit - 1) / 3;
113
114 String suffix = "";
115 if (unit_quotient == 0) {
116 }
117 else if (unit_quotient == 1) {
118 suffix = "k";
119 }
120 else if (unit_quotient == 2) {
121 suffix = "M";
122 }
123 else if (unit_quotient == 3) {
124 suffix = "G";
125 }
126 else if (unit_quotient == 4) {
127 suffix = "T";
128 }
129
130 g.setColor(Color.BLACK);
131 g.drawLine(x_left, height / 2, x_right, height / 2);
132
133 g.drawLine(x_left, y_lr_u, x_left, y_lr_l);
134 g.drawLine(x_right, y_lr_u, x_right, y_lr_l);
135
136 g.drawLine(x_c, y_c_u, x_c, y_c_l);
137
138 g.drawLine(x_1, y_o_u, x_1, y_o_l);
139 g.drawLine(x_2, y_o_u, x_2, y_o_l);
140 g.drawLine(x_3, y_o_u, x_3, y_o_l);
141 g.drawLine(x_4, y_o_u, x_4, y_o_l);
142 g.drawLine(x_6, y_o_u, x_6, y_o_l);
143 g.drawLine(x_7, y_o_u, x_7, y_o_l);
144 g.drawLine(x_8, y_o_u, x_8, y_o_l);
145 g.drawLine(x_9, y_o_u, x_9, y_o_l);
146
147 Font f = new Font("SansSerif", Font.PLAIN, 10);
148 g.setFont(f);
149 g.drawString(Long.toString(unit).substring(0, unit_remainder) + suffix + "b", x_right + 5, height / 2 + height / 15);
150 }
151 }