大梦想家

5年开发工程师,2年实施经理,X年售前顾问,......
数据加载中……
SWT中使用JFreechart(例子)
  1package com.glnpu.dmp.test;
  2
  3import java.awt.Color;
  4import java.awt.Font;
  5import java.awt.Frame;
  6import java.util.Calendar;
  7import java.util.Date;
  8
  9import org.eclipse.jface.window.ApplicationWindow;
 10import org.eclipse.swt.SWT;
 11import org.eclipse.swt.awt.SWT_AWT;
 12import org.eclipse.swt.graphics.Point;
 13import org.eclipse.swt.widgets.Composite;
 14import org.eclipse.swt.widgets.Control;
 15import org.eclipse.swt.widgets.Display;
 16import org.eclipse.swt.widgets.Shell;
 17import org.eclipse.swt.widgets.TabFolder;
 18import org.eclipse.swt.widgets.TabItem;
 19import org.jfree.chart.ChartFactory;
 20import org.jfree.chart.ChartPanel;
 21import org.jfree.chart.JFreeChart;
 22import org.jfree.chart.plot.PiePlot;
 23import org.jfree.chart.title.TextTitle;
 24import org.jfree.data.category.IntervalCategoryDataset;
 25import org.jfree.data.gantt.Task;
 26import org.jfree.data.gantt.TaskSeries;
 27import org.jfree.data.gantt.TaskSeriesCollection;
 28import org.jfree.data.general.DefaultPieDataset;
 29
 30public class Test extends ApplicationWindow {
 31
 32    /**
 33     * Create the application window
 34     */

 35    public Test() {
 36        super(null);
 37        addToolBar(SWT.FLAT | SWT.WRAP);
 38        addMenuBar();
 39        addStatusLine();
 40    }

 41
 42    /**
 43     * Create contents of the application window
 44     * 
 45     * @param parent
 46     */

 47    @Override
 48    protected Control createContents(Composite parent) {
 49        TabFolder tf = new TabFolder(parent, SWT.TOP);
 50        TabItem ti = new TabItem(tf, SWT.NULL);
 51        ti.setText("分类");
 52        Composite composite = new Composite(tf, SWT.NO_BACKGROUND
 53                | SWT.EMBEDDED);
 54        Frame frame = SWT_AWT.new_Frame(composite);
 55        frame.add(new ChartPanel(createBarChart()));
 56        ti.setControl(composite);
 57        TabItem ti1 = new TabItem(tf, SWT.NULL);
 58        ti1.setText("项目组");
 59        Composite composite1 = new Composite(tf, SWT.NO_BACKGROUND
 60                | SWT.EMBEDDED);
 61        Frame frame1 = SWT_AWT.new_Frame(composite1);
 62        frame1.add(new ChartPanel(createGanttChart()));
 63        ti1.setControl(composite1);
 64        tf.setSelection(0);
 65        return tf;
 66    }

 67
 68    /**
 69     * 方法名称: 内容摘要:
 70     * 
 71     * @return
 72     * @return JFreeChart
 73     * @throws
 74     */

 75    private JFreeChart createGanttChart() {
 76        String title = "Gantt测试";
 77        IntervalCategoryDataset dataset = createSampleDataset();
 78
 79        JFreeChart jfc = ChartFactory.createGanttChart(title, "项目各阶段详细实施计划",
 80                "项目周期", dataset, falsefalsefalse);
 81
 82        return jfc;
 83    }

 84
 85    /**
 86      * 方法名称:
 87      * 内容摘要:创建gantt内容
 88      *
 89      * @return
 90      * @return IntervalCategoryDataset
 91      * @throws
 92     */

 93    private IntervalCategoryDataset createSampleDataset() {
 94        TaskSeries s1 = new TaskSeries("日程表");
 95
 96        Task t1 = new Task("项目立项", date(1, Calendar.APRIL, 2001), date(5,
 97                Calendar.APRIL, 2001));
 98        t1.setPercentComplete(1.00);
 99        
100        Task t2 = new Task("项目立项讨论", date(6, Calendar.APRIL, 2001), date(19,
101                Calendar.APRIL, 2001));
102        
103        s1.add(t1);
104        s1.add(t2);
105        
106        
107        final Task t3 = new Task(
108                "需求分析"
109                date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001)
110            );
111            final Task st31 = new Task(
112                "需求1"
113                date(10, Calendar.APRIL, 2001), date(25, Calendar.APRIL, 2001)
114            );
115            st31.setPercentComplete(1.0);
116            final Task st32 = new Task(
117                "需求2"
118                date(1, Calendar.MAY, 2001), date(5, Calendar.MAY, 2001)
119            );
120            st32.setPercentComplete(1.0);
121            t3.addSubtask(st31);
122            t3.addSubtask(st32);
123            s1.add(t3);
124        
125        
126        
127        final TaskSeriesCollection collection = new TaskSeriesCollection();
128        collection.add(s1);
129
130        return collection;
131    }

132
133    /** */
134    /**
135     * Utility method for creating <code>Date</code> objects.
136     * 
137     * @param day
138     *            日
139     * @param month
140     *            月
141     * @param year
142     *            年
143     * 
144     * @return a date.
145     */

146    private static Date date(final int day, final int month, final int year) {
147
148        final Calendar calendar = Calendar.getInstance();
149        calendar.set(year, month, day);
150
151        final Date result = calendar.getTime();
152        return result;
153
154    }

155
156    /**
157     * 方法名称: 内容摘要:饼图测试
158     * 
159     * @return
160     * @return JFreeChart
161     * @throws
162     */

163    private JFreeChart createBarChart() {
164        String title = "空调2002年市场占有率";
165        DefaultPieDataset piedata = new DefaultPieDataset();
166        piedata.setValue("联想"27.3);
167        piedata.setValue("长城"12.2);
168        piedata.setValue("海尔"5.5);
169        piedata.setValue("美的"17.1);
170        piedata.setValue("松下"9.0);
171        piedata.setValue("科龙"19.0);
172        JFreeChart chart = ChartFactory.createPieChart(title, piedata, true,
173                truetrue);
174        chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
175        chart.addSubtitle(new TextTitle("2002财年分析"new Font("隶书", Font.ITALIC,
176                12)));
177        chart.setBackgroundPaint(Color.white);
178        PiePlot pie = (PiePlot) chart.getPlot();
179        pie.setBackgroundPaint(Color.white);
180        pie.setBackgroundAlpha(0.6f);
181        pie.setForegroundAlpha(0.90f);
182        return chart;
183    }

184
185    /**
186     * Launch the application
187     * 
188     * @param args
189     */

190    public static void main(String args[]) {
191        try {
192            Test window = new Test();
193            window.setBlockOnOpen(true);
194            window.open();
195            Display.getCurrent().dispose();
196        }
 catch (Exception e) {
197            e.printStackTrace();
198        }

199    }

200
201    /**
202     * Configure the shell
203     * 
204     * @param newShell
205     */

206    @Override
207    protected void configureShell(Shell newShell) {
208        super.configureShell(newShell);
209        newShell.setText("New Application");
210    }

211
212    /**
213     * Return the initial size of the window
214     */

215    @Override
216    protected Point getInitialSize() {
217        return new Point(500375);
218    }

219
220}

221
核心在:
Composite composite = new Composite(tf, SWT.NO_BACKGROUND
    | SWT.EMBEDDED);
  Frame frame = SWT_AWT.new_Frame(composite);
使用SWT_AWT进行桥连接~速度有点慢!

客户虐我千百遍,我待客户如初恋!

posted on 2007-08-20 15:59 阿南 阅读(3479) 评论(5)  编辑  收藏 所属分类: 西安java用户群Eclipse-SWT

评论

# re: SWT中使用JFreechart(例子) 2007-08-20 22:59 交口称赞

我以前写过一个view,用在eclipse rcp里面
可以用来显示jfc的报表。

  回复  更多评论    

# re: SWT中使用JFreechart(例子) 2007-08-21 22:12 三告习习

jfreechart里面有个jar包例子,不过要看源码的话就要反编译过来。
其中有个类是管理其他所有例子的。以前曾经常查看过,记录了每个之间的不同用法。但不知丢哪去了...
  回复  更多评论    

# re: SWT中使用JFreechart(例子) 2007-08-21 22:33 阿南

用我以前推荐的代码搜索引擎,一个一个都找出来,慢慢看!
  回复  更多评论    

# re: SWT中使用JFreechart(例子) 2008-07-21 23:43 何敏··

还看不懂


  回复  更多评论    

# re: SWT中使用JFreechart(例子)[未登录] 2012-01-12 10:02 steven

咋就不能贴个图呢...楼主
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: