/ftr-gwt-charts/src/eu/future/earth/gwt/charts/client/GraphRaster.java
Java | 206 lines | 163 code | 41 blank | 2 comment | 21 complexity | 97a3cee974204d319befa74388209093 MD5 | raw file
Possible License(s): Apache-2.0
1package eu.future.earth.gwt.charts.client; 2 3import java.util.ArrayList; 4import java.util.List; 5 6import org.cobogw.gwt.user.client.CSS; 7import org.cobogw.gwt.user.client.Color; 8 9import com.google.gwt.canvas.client.Canvas; 10import com.google.gwt.canvas.dom.client.Context2d; 11import com.google.gwt.core.client.GWT; 12import com.google.gwt.user.client.ui.AbsolutePanel; 13import com.google.gwt.user.client.ui.Composite; 14import com.google.gwt.user.client.ui.Label; 15 16public class GraphRaster extends Composite { 17 18 private AbsolutePanel holder = new AbsolutePanel(); 19 20 private Canvas canvas; 21 22 public GraphRaster(int coordX, int coordY) { 23 super(); 24 initWidget(holder); 25 canvas = Canvas.createIfSupported(); 26 setPixelSize(coordX, coordY); 27 } 28 29 public void renderGrid() { 30 31 } 32 33 private String backgroundColor = "#FFFFFF"; 34 35 private int height = 400; 36 37 private int width = 400; 38 39 private int leftOffSet = 50; 40 41 private int bottumOffSet = 20; 42 43 public void setPixelSize(int newWidth, int newHeight) { 44 height = newHeight; 45 width = newWidth; 46 resize(); 47 } 48 49 private void resize() { 50 holder.setPixelSize(width, height); 51 holder.clear(); 52 int offsety = 0; 53 if (showXItems) { 54 offsety = bottumOffSet; 55 } 56 int offsetx = 0; 57 if (showYItems) { 58 offsetx = leftOffSet; 59 } 60 canvas.setCoordinateSpaceHeight(height - offsety); 61 canvas.setCoordinateSpaceWidth(width - offsetx); 62 canvas.setPixelSize(width - offsetx, height - offsety); 63 holder.add(canvas, offsetx, 0); 64 if (backgroundColor != null) { 65 CSS.setProperty(canvas, CSS.A.BACKGROUND_COLOR, backgroundColor); 66 } 67 } 68 69 private List<GraphRenderer> lines = new ArrayList<GraphRenderer>(); 70 71 public void addLine(GraphRenderer newLine) { 72 lines.add(newLine); 73 } 74 75 private List<RasterLine> yRaster = new ArrayList<RasterLine>(); 76 77 private List<RasterLine> xRaster = new ArrayList<RasterLine>(); 78 79 private boolean showXItems = true; 80 81 private boolean showYItems = true; 82 83 public void addYLine(RasterLine newLine) { 84 yRaster.add(newLine); 85 } 86 87 public void addXLine(RasterLine newLine) { 88 xRaster.add(newLine); 89 } 90 91 public boolean isShowXItems() { 92 return showXItems; 93 } 94 95 public void setShowXItems(boolean showXItems) { 96 this.showXItems = showXItems; 97 resize(); 98 } 99 100 public boolean isShowYItems() { 101 return showYItems; 102 } 103 104 public void setShowYItems(boolean showYItems) { 105 this.showYItems = showYItems; 106 resize(); 107 } 108 109 public void clearData() { 110 lines.clear(); 111 } 112 113 public void draw() { 114 Context2d paintOn = this.canvas.getContext2d(); 115 paintOn.setLineWidth(1); 116 paintOn.setStrokeStyle(Color.RED); 117 paintOn.beginPath(); 118 paintOn.setGlobalAlpha(1.0); 119 paintOn.moveTo(0, 0); 120 121 int height = this.canvas.getCoordinateSpaceHeight(); 122 int widht = this.canvas.getCoordinateSpaceWidth(); 123 124 paintOn.lineTo(0, height); 125 paintOn.lineTo(widht, height); 126 paintOn.stroke(); 127 paintOn.closePath(); 128 129 int offsety = 0; 130 if (showXItems) { 131 offsety = bottumOffSet; 132 } 133 int offsetx = 0; 134 if (showYItems) { 135 offsetx = leftOffSet; 136 } 137 GWT.log("" + offsety + "," + offsetx, null); 138 paintOn.setLineWidth(0.5); 139 paintOn.setStrokeStyle(Color.BLACK); 140 141 for (RasterLine xLine : xRaster) { 142 if (xLine.getPos() > 0) { 143 if (xLine.isBold()) { 144 // canvas.setLineWidth(1.0); 145 paintOn.setGlobalAlpha(1.0); 146 } else { 147 // canvas.setLineWidth(1.0); 148 paintOn.setGlobalAlpha(0.2); 149 } 150 paintOn.beginPath(); 151 152 paintOn.moveTo(xLine.getPos(), height); 153 paintOn.lineTo(xLine.getPos(), 0); 154 paintOn.stroke(); 155 paintOn.closePath(); 156 } 157 if (showXItems && xLine.isShowLabel()) { 158 Label label = new Label(xLine.getLabel()); 159 label.setTitle(xLine.getTitle()); 160 holder.add(label, (int) (offsetx - 10 + xLine.getPos()), height - offsety); 161 } 162 163 } 164 165 for (RasterLine yLine : yRaster) { 166 if (yLine.getPos() > 0) { 167 if (yLine.isBold()) { 168 paintOn.setGlobalAlpha(0.6); 169 } else { 170 paintOn.setGlobalAlpha(0.2); 171 } 172 paintOn.beginPath(); 173 paintOn.moveTo(0, height - yLine.getPos()); 174 paintOn.lineTo(widht, height - yLine.getPos()); 175 paintOn.stroke(); 176 paintOn.closePath(); 177 } 178 if (showYItems && yLine.isShowLabel()) { 179 Label label = new Label(yLine.getLabel()); 180 label.setTitle(yLine.getTitle()); 181 label.setHorizontalAlignment(Label.ALIGN_RIGHT); 182 if (offsetx > 5) { 183 label.setWidth(offsetx - 4 + "px"); 184 } 185 holder.add(label, 0, (int) ((height - yLine.getPos()) - offsety - 10)); 186 } 187 } 188 189 double maxX = 0; 190 double maxY = 0; 191 for (GraphRenderer item : lines) { 192 maxX = Math.max(maxX, item.getMaxX()); 193 maxY = Math.max(maxY, item.getMaxY()); 194 } 195 196 for (GraphRenderer line : lines) { 197 line.renderData(this.canvas); 198 } 199 } 200 201 @Override 202 public int getOffsetHeight() { 203 return height; 204 } 205 206}