View Javadoc

1   /*
2    *  File: LineChartExample.java 
3    *  Copyright (c) 2004-2008  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;
21  
22  import org.eclipse.jface.window.ApplicationWindow;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.graphics.Point;
25  import org.eclipse.swt.layout.GridData;
26  import org.eclipse.swt.layout.GridLayout;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Control;
29  import org.eclipse.swt.widgets.Shell;
30  
31  import de.jaret.examples.timebars.linechart.model.LineChartInterval;
32  import de.jaret.examples.timebars.linechart.model.ModelCreator;
33  import de.jaret.examples.timebars.linechart.swt.renderer.LineChartHeaderRenderer;
34  import de.jaret.examples.timebars.linechart.swt.renderer.LineChartRenderer;
35  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
36  import de.jaret.util.ui.timebars.model.IRowHeightStrategy;
37  import de.jaret.util.ui.timebars.model.ITimeBarViewState;
38  import de.jaret.util.ui.timebars.model.TimeBarModel;
39  import de.jaret.util.ui.timebars.model.TimeBarRow;
40  import de.jaret.util.ui.timebars.swt.TimeBarViewer;
41  import de.jaret.util.ui.timebars.swt.renderer.BoxTimeScaleRenderer;
42  import de.jaret.util.ui.timebars.swt.util.TimeScaleDragSupport;
43  
44  /***
45   * Example showing how to draw a simple line chart with the time bars. This is showing one line chart only, but mixing
46   * intervals and line chart intervals is possible.
47   * 
48   * @author Peter Kliem
49   * @version $Id: LineChartExample.java 801 2008-12-27 22:44:54Z kliem $
50   */
51  public class LineChartExample extends ApplicationWindow {
52      /*** timebar viewer. */
53      private static TimeBarViewer _tbv;
54  
55      public LineChartExample() {
56          super(null);
57      }
58  
59      protected Control createContents(Composite parent) {
60          GridLayout gridLayout = new GridLayout();
61          gridLayout.numColumns = 1;
62          gridLayout.verticalSpacing = 0;
63          parent.setLayout(gridLayout);
64  
65          TimeBarModel model = ModelCreator.createModel();
66  
67          GridData gd = new GridData(GridData.FILL_BOTH);
68  
69          // create the time bar viewer
70          // horizontal scroll bar only! (since we only assume on erow, height of the
71          // row is done by a row height strategy)
72          _tbv = new TimeBarViewer(parent, SWT.H_SCROLL);
73          _tbv.setLayoutData(gd);
74  
75          _tbv.setTimeScalePosition(TimeBarViewer.TIMESCALE_POSITION_TOP);
76          _tbv.setModel(model);
77  
78          // choose a nice scale to begin with
79          _tbv.setPixelPerSecond(0.0065);
80  
81          // do some configurations on the viewer to make it more suitable for chart rendering
82  
83          // no selections
84          _tbv.getSelectionModel().setRowSelectionAllowed(false);
85          _tbv.getSelectionModel().setIntervalSelectionAllowed(false);
86          _tbv.getSelectionModel().setMultipleSelectionAllowed(false); // also disable rect selection
87  
88          // setup the y axis
89          _tbv.setYAxisWidth(20);
90          _tbv.setHeaderRenderer(new LineChartHeaderRenderer());
91  
92          // disable grid rendering since it is not needed
93          _tbv.setGridRenderer(null);
94          
95          // allow dragging to replace scrolling
96          TimeScaleDragSupport tsds = new TimeScaleDragSupport(_tbv, true);
97  
98          // use the box timescale renderer
99          _tbv.setTimeScaleRenderer(new BoxTimeScaleRenderer());
100 
101         // we will only render one row. This row should always be scaled to match the height of the diagram rectangle.
102         // the row height strategy will ensure that
103         _tbv.getTimeBarViewState().setUseVariableRowHeights(true);
104         _tbv.getTimeBarViewState().setRowHeightStrategy(new IRowHeightStrategy() {
105 
106             public int calculateRowHeight(TimeBarViewerDelegate delegate, ITimeBarViewState timeBarViewState,
107                     TimeBarRow row) {
108                 return delegate.getDiagramRect().height;
109             }
110 
111             public boolean overrideDefault() {
112                 return true;
113             }
114 
115         });
116 
117         
118         // register the renderer for the line chart itself
119         _tbv.registerTimeBarRenderer(LineChartInterval.class, new LineChartRenderer());
120 
121         // add the control panel for a scale slider
122         LineChartControlPanel ctrlPanel = new LineChartControlPanel(parent, SWT.NULL, _tbv);
123 
124         return _tbv;
125     }
126 
127     protected void configureShell(Shell shell) {
128         super.configureShell(shell);
129         shell.setText(getClass().getName());
130         shell.setSize(new Point(1000, 600));
131     }
132 
133     public static void main(String[] args) {
134         LineChartExample test = new LineChartExample();
135         test.setBlockOnOpen(true);
136         test.open();
137     }
138 
139 }