/ftr-gwt-charts/src/eu/future/earth/gwt/charts/client/GraphRaster.java

http://ftr-gwt-library.googlecode.com/ · Java · 206 lines · 163 code · 41 blank · 2 comment · 21 complexity · 97a3cee974204d319befa74388209093 MD5 · raw file

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