/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
- package eu.future.earth.gwt.charts.client;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.cobogw.gwt.user.client.CSS;
- import org.cobogw.gwt.user.client.Color;
-
- import com.google.gwt.canvas.client.Canvas;
- import com.google.gwt.canvas.dom.client.Context2d;
- import com.google.gwt.core.client.GWT;
- import com.google.gwt.user.client.ui.AbsolutePanel;
- import com.google.gwt.user.client.ui.Composite;
- import com.google.gwt.user.client.ui.Label;
-
- public class GraphRaster extends Composite {
-
- private AbsolutePanel holder = new AbsolutePanel();
-
- private Canvas canvas;
-
- public GraphRaster(int coordX, int coordY) {
- super();
- initWidget(holder);
- canvas = Canvas.createIfSupported();
- setPixelSize(coordX, coordY);
- }
-
- public void renderGrid() {
-
- }
-
- private String backgroundColor = "#FFFFFF";
-
- private int height = 400;
-
- private int width = 400;
-
- private int leftOffSet = 50;
-
- private int bottumOffSet = 20;
-
- public void setPixelSize(int newWidth, int newHeight) {
- height = newHeight;
- width = newWidth;
- resize();
- }
-
- private void resize() {
- holder.setPixelSize(width, height);
- holder.clear();
- int offsety = 0;
- if (showXItems) {
- offsety = bottumOffSet;
- }
- int offsetx = 0;
- if (showYItems) {
- offsetx = leftOffSet;
- }
- canvas.setCoordinateSpaceHeight(height - offsety);
- canvas.setCoordinateSpaceWidth(width - offsetx);
- canvas.setPixelSize(width - offsetx, height - offsety);
- holder.add(canvas, offsetx, 0);
- if (backgroundColor != null) {
- CSS.setProperty(canvas, CSS.A.BACKGROUND_COLOR, backgroundColor);
- }
- }
-
- private List<GraphRenderer> lines = new ArrayList<GraphRenderer>();
-
- public void addLine(GraphRenderer newLine) {
- lines.add(newLine);
- }
-
- private List<RasterLine> yRaster = new ArrayList<RasterLine>();
-
- private List<RasterLine> xRaster = new ArrayList<RasterLine>();
-
- private boolean showXItems = true;
-
- private boolean showYItems = true;
-
- public void addYLine(RasterLine newLine) {
- yRaster.add(newLine);
- }
-
- public void addXLine(RasterLine newLine) {
- xRaster.add(newLine);
- }
-
- public boolean isShowXItems() {
- return showXItems;
- }
-
- public void setShowXItems(boolean showXItems) {
- this.showXItems = showXItems;
- resize();
- }
-
- public boolean isShowYItems() {
- return showYItems;
- }
-
- public void setShowYItems(boolean showYItems) {
- this.showYItems = showYItems;
- resize();
- }
-
- public void clearData() {
- lines.clear();
- }
-
- public void draw() {
- Context2d paintOn = this.canvas.getContext2d();
- paintOn.setLineWidth(1);
- paintOn.setStrokeStyle(Color.RED);
- paintOn.beginPath();
- paintOn.setGlobalAlpha(1.0);
- paintOn.moveTo(0, 0);
-
- int height = this.canvas.getCoordinateSpaceHeight();
- int widht = this.canvas.getCoordinateSpaceWidth();
-
- paintOn.lineTo(0, height);
- paintOn.lineTo(widht, height);
- paintOn.stroke();
- paintOn.closePath();
-
- int offsety = 0;
- if (showXItems) {
- offsety = bottumOffSet;
- }
- int offsetx = 0;
- if (showYItems) {
- offsetx = leftOffSet;
- }
- GWT.log("" + offsety + "," + offsetx, null);
- paintOn.setLineWidth(0.5);
- paintOn.setStrokeStyle(Color.BLACK);
-
- for (RasterLine xLine : xRaster) {
- if (xLine.getPos() > 0) {
- if (xLine.isBold()) {
- // canvas.setLineWidth(1.0);
- paintOn.setGlobalAlpha(1.0);
- } else {
- // canvas.setLineWidth(1.0);
- paintOn.setGlobalAlpha(0.2);
- }
- paintOn.beginPath();
-
- paintOn.moveTo(xLine.getPos(), height);
- paintOn.lineTo(xLine.getPos(), 0);
- paintOn.stroke();
- paintOn.closePath();
- }
- if (showXItems && xLine.isShowLabel()) {
- Label label = new Label(xLine.getLabel());
- label.setTitle(xLine.getTitle());
- holder.add(label, (int) (offsetx - 10 + xLine.getPos()), height - offsety);
- }
-
- }
-
- for (RasterLine yLine : yRaster) {
- if (yLine.getPos() > 0) {
- if (yLine.isBold()) {
- paintOn.setGlobalAlpha(0.6);
- } else {
- paintOn.setGlobalAlpha(0.2);
- }
- paintOn.beginPath();
- paintOn.moveTo(0, height - yLine.getPos());
- paintOn.lineTo(widht, height - yLine.getPos());
- paintOn.stroke();
- paintOn.closePath();
- }
- if (showYItems && yLine.isShowLabel()) {
- Label label = new Label(yLine.getLabel());
- label.setTitle(yLine.getTitle());
- label.setHorizontalAlignment(Label.ALIGN_RIGHT);
- if (offsetx > 5) {
- label.setWidth(offsetx - 4 + "px");
- }
- holder.add(label, 0, (int) ((height - yLine.getPos()) - offsety - 10));
- }
- }
-
- double maxX = 0;
- double maxY = 0;
- for (GraphRenderer item : lines) {
- maxX = Math.max(maxX, item.getMaxX());
- maxY = Math.max(maxY, item.getMaxY());
- }
-
- for (GraphRenderer line : lines) {
- line.renderData(this.canvas);
- }
- }
-
- @Override
- public int getOffsetHeight() {
- return height;
- }
-
- }