View Javadoc

1   /*
2    *  File: LineChartRenderer.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.jaret.examples.timebars.linechart.swt.renderer;
21  
22  import java.util.List;
23  
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.graphics.Color;
26  import org.eclipse.swt.graphics.GC;
27  import org.eclipse.swt.graphics.Point;
28  import org.eclipse.swt.graphics.Rectangle;
29  import org.eclipse.swt.printing.Printer;
30  import org.eclipse.swt.widgets.Display;
31  
32  import de.jaret.examples.timebars.linechart.model.DataPoint;
33  import de.jaret.examples.timebars.linechart.model.LineChartInterval;
34  import de.jaret.examples.timebars.linechart.model.ModelCreator;
35  import de.jaret.util.date.Interval;
36  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
37  import de.jaret.util.ui.timebars.swt.renderer.RendererBase;
38  import de.jaret.util.ui.timebars.swt.renderer.TimeBarRenderer;
39  
40  /***
41   * renderer rendering a line chart in a LineChartInterval.
42   * 
43   * @author Peter Kliem
44   * @version $Id: LineChartRenderer.java 766 2008-05-28 21:36:48Z kliem $
45   */
46  public class LineChartRenderer extends RendererBase implements TimeBarRenderer {
47  
48      /***
49       * Create renderer for printing.
50       * 
51       * @param printer printer device
52       */
53      public LineChartRenderer(Printer printer) {
54          super(printer);
55      }
56  
57      /***
58       * Construct renderer for screen use.
59       * 
60       */
61      public LineChartRenderer() {
62          super(null);
63      }
64  
65      /***
66       * Calculate y value for a given value in the line chart example.
67       * 
68       * @param drawingArea drawing area (of which the height is needed)
69       * @param value value to project
70       * @return projected y coordinate
71       */
72      public static int yForValue(Rectangle drawingArea, int value) {
73          double vForPix = (double) drawingArea.height / ModelCreator.MAX;
74          return drawingArea.y + drawingArea.height - (int) (vForPix * value);
75      }
76  
77      /***
78       * Calculate the value represented by an y coordinate in the line chart example.
79       * 
80       * @param drawingArea drawing area as the base for the projection
81       * @param y y coordinate
82       * @return the value
83       */
84      public static int valueForY(Rectangle drawingArea, int y) {
85          double vForPix = (double) drawingArea.height / ModelCreator.MAX;
86          int off = drawingArea.height - y;
87          return (int) ((double) off / vForPix);
88      }
89  
90      /***
91       * {@inheritDoc}
92       */
93      public void draw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval, boolean selected,
94              boolean printing, boolean overlap) {
95  
96          Color bg = gc.getBackground();
97  
98          gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
99          gc.fillRectangle(gc.getClipping());
100 
101         // draw lines for 10, 50, 90
102         Color fg = gc.getForeground();
103         gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY));
104         gc.setLineStyle(SWT.LINE_DASH);
105         // 10
106         int ly = yForValue(drawingArea, 10);
107         gc.drawLine(0, ly, gc.getClipping().x + gc.getClipping().width, ly);
108         // 50
109         ly = yForValue(drawingArea, 50);
110         gc.drawLine(0, ly, gc.getClipping().x + gc.getClipping().width, ly);
111         // 90
112         ly = yForValue(drawingArea, 90);
113         gc.drawLine(0, ly, gc.getClipping().x + gc.getClipping().width, ly);
114 
115         gc.setLineStyle(SWT.LINE_SOLID);
116         gc.setForeground(fg);
117 
118         // get all points to be drawn
119         LineChartInterval lci = (LineChartInterval) interval;
120 
121         // get the data points to draw
122         // since the drawing of the connecting lines will fail due to the scroll optimization take some more points
123         // on each side
124         List<DataPoint> points = lci.getDataPoints(delegate.getStartDate().copy().backHours(3), delegate.getEndDate()
125                 .copy().advanceHours(3));
126 
127         Point last = null;
128 
129         for (DataPoint dataPoint : points) {
130             int x = delegate.xForDate(dataPoint.getTime());
131             int y = yForValue(drawingArea, dataPoint.getValue());
132 
133             if (last != null) {
134                 gc.drawLine(last.x, last.y, x, y);
135             }
136             last = new Point(x, y);
137             drawPoint(gc, x, y);
138         }
139         gc.setBackground(bg);
140     }
141 
142     private void drawPoint(GC gc, int x, int y) {
143         int size = 3;
144         Color fg = gc.getForeground();
145         gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_MAGENTA));
146 
147         gc.drawLine(x - size, y - size, x + size, y + size);
148         gc.drawLine(x - size, y + size, x + size, y - size);
149 
150         gc.setForeground(fg);
151     }
152 
153     /***
154      * {@inheritDoc}
155      */
156     public String getToolTipText(Interval interval, Rectangle drawingArea, int x, int y, boolean overlapping) {
157         // return the value
158         return "" + valueForY(drawingArea, y);
159     }
160 
161     /***
162      * {@inheritDoc}
163      */
164     public boolean contains(Interval interval, Rectangle drawingArea, int x, int y, boolean overlapping) {
165         return true;
166     }
167 
168     /***
169      * {@inheritDoc}
170      */
171     public Rectangle getContainingRectangle(Interval interval, Rectangle drawingArea, boolean overlapping) {
172         return drawingArea;
173     }
174 
175     /***
176      * {@inheritDoc}
177      */
178     public TimeBarRenderer createPrintrenderer(Printer printer) {
179         return new LineChartRenderer(printer);
180     }
181 
182     /***
183      * {@inheritDoc}
184      */
185     public void dispose() {
186     }
187 
188 }