1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
102 Color fg = gc.getForeground();
103 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY));
104 gc.setLineStyle(SWT.LINE_DASH);
105
106 int ly = yForValue(drawingArea, 10);
107 gc.drawLine(0, ly, gc.getClipping().x + gc.getClipping().width, ly);
108
109 ly = yForValue(drawingArea, 50);
110 gc.drawLine(0, ly, gc.getClipping().x + gc.getClipping().width, ly);
111
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
119 LineChartInterval lci = (LineChartInterval) interval;
120
121
122
123
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
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 }