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 java.util.List;
26
27 import org.utgenome.gwt.utgb.client.bio.CompactWIGData;
28 import org.utgenome.gwt.utgb.client.canvas.GWTGraphCanvas.GraphStyle;
29 import org.utgenome.gwt.utgb.client.track.TrackWindow;
30 import org.utgenome.gwt.widget.client.Style;
31
32 import com.google.gwt.user.client.ui.Composite;
33 import com.google.gwt.widgetideas.graphics.client.Color;
34 import com.google.gwt.widgetideas.graphics.client.GWTCanvas;
35
36
37
38
39
40
41
42 public class BarGraphCanvas extends Composite {
43
44 private final String DEFAULT_COLOR = "rgba(12,106,193,0.7)";
45 private GWTCanvas canvas = new GWTCanvas();
46 private TrackWindow window = new TrackWindow();
47 private final Scale scale = new Scale();
48
49 public BarGraphCanvas(TrackWindow window, int windowHeight) {
50 this.window = window;
51
52 initWidget(canvas);
53 setPixelSize(window.getPixelWidth(), windowHeight);
54 }
55
56 public void clear() {
57 canvas.clear();
58 }
59
60 public TrackWindow getTrackWindow() {
61 return window;
62 }
63
64 public void setTrackWindow(TrackWindow window, int newPixelX) {
65 TrackWindow prev = this.window;
66 this.window = window;
67
68 if (!prev.hasSameScaleWith(window)) {
69 Style.scaleXwithAnimation(canvas, (double) window.getPixelWidth() / prev.getPixelWidth(), newPixelX, 0.5);
70 }
71 else {
72 Style.scaleX(canvas, 1);
73 }
74 }
75
76 private float min = 0;
77 private float max = 0;
78
79 private List<CompactWIGData> graphData;
80 private int span = 1;
81
82 public void redraw(GraphStyle style) {
83 if (this.graphData == null)
84 return;
85
86 canvas.clear();
87 draw(this.graphData, style);
88 }
89
90 private void setPixelWidth(int width, int height) {
91 int pixelWidthWithSpan = window.convertToPixelLength(window.getSequenceLength() + this.span - 2);
92 canvas.setPixelSize(pixelWidthWithSpan, height);
93 canvas.setCoordSize(pixelWidthWithSpan, height);
94 }
95
96 public List<CompactWIGData> getGraphData() {
97 return graphData;
98 }
99
100 public void setGraphData(List<CompactWIGData> graphData) {
101 this.graphData = graphData;
102 }
103
104 public void draw(List<CompactWIGData> graphData, GraphStyle style) {
105
106 for (CompactWIGData each : graphData) {
107 if (each.getSpan() > span) {
108 this.span = each.getSpan();
109 }
110 }
111 setPixelWidth(window.getPixelWidth(), style.windowHeight);
112
113 scale.updateStyle(style);
114
115 for (CompactWIGData data : graphData) {
116
117 Color graphColor = new Color(DEFAULT_COLOR);
118 if (style.color.isDefined()) {
119 graphColor = new Color(style.color.get());
120 }
121 else if (data.getTrack().containsKey("color")) {
122 String colorStr = data.getTrack().get("color");
123 String c[] = colorStr.split(",");
124 if (c.length == 3)
125 graphColor = new Color(Integer.valueOf(c[0]), Integer.valueOf(c[1]), Integer.valueOf(c[2]));
126 }
127
128
129 canvas.saveContext();
130 canvas.setLineWidth(1.0f);
131 canvas.setStrokeStyle(graphColor);
132
133 min = scale.getMin();
134 max = scale.getMax();
135
136 float y2 = scale.getYPosition(0.0f);
137
138
139 final boolean isReverse = window.isReverseStrand();
140 final int pixelWidth = data.getData().length;
141
142 for (int i = 0; i < pixelWidth; ++i) {
143 float value = data.getData()[i];
144 float y1;
145 if (value == 0.0f) {
146 if (!style.drawZeroValue)
147 continue;
148 else {
149 y1 = y2 + ((min < max) ? -0.5f : 0.5f);
150 }
151 }
152 else {
153 y1 = scale.getYPosition(value);
154 }
155
156 int x = i;
157 if (isReverse) {
158 x = pixelWidth - x - 1;
159 }
160
161 canvas.saveContext();
162 canvas.beginPath();
163 canvas.translate(x + 0.5f, 0);
164 canvas.moveTo(0, y1);
165 canvas.lineTo(0, y2);
166 canvas.stroke();
167 canvas.restoreContext();
168 }
169 canvas.restoreContext();
170 }
171
172 }
173
174 }