SWT中使用JFreeChart(无需SWT_AWT)

    好像从1.03开始Jfc就已经提供了在SWT中使用JFC的专用包和类,只是没有人写这些东西而已~今天我就贴一些Demo,以后再也不用SWT_AWT了~
  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* ===========================================================
  2InBlock.gif * JFreeChart : a free chart library for the Java(tm) platform
  3InBlock.gif * ===========================================================
  4InBlock.gif *
  5InBlock.gif * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
  6InBlock.gif *
  7InBlock.gif * Project Info:  http://www.jfree.org/jfreechart/index.html
  8InBlock.gif *
  9InBlock.gif * This library is free software; you can redistribute it and/or modify it 
 10InBlock.gif * under the terms of the GNU Lesser General Public License as published by 
 11InBlock.gif * the Free Software Foundation; either version 2.1 of the License, or 
 12InBlock.gif * (at your option) any later version.
 13InBlock.gif *
 14InBlock.gif * This library is distributed in the hope that it will be useful, but 
 15InBlock.gif * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 16InBlock.gif * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 17InBlock.gif * License for more details.
 18InBlock.gif *
 19InBlock.gif * You should have received a copy of the GNU Lesser General Public
 20InBlock.gif * License along with this library; if not, write to the Free Software
 21InBlock.gif * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 22InBlock.gif * USA.  
 23InBlock.gif *
 24InBlock.gif * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 25InBlock.gif * in the United States and other countries.]
 26InBlock.gif *
 27InBlock.gif * ---------------------
 28InBlock.gif * SWTBarChartDemo1.java
 29InBlock.gif * ---------------------
 30InBlock.gif * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
 31InBlock.gif *
 32InBlock.gif * Original Author:  David Gilbert (for Object Refinery Limited);
 33InBlock.gif * Contributor(s):
 34InBlock.gif *
 35InBlock.gif * Changes
 36InBlock.gif * -------
 37InBlock.gif * 23-Aug-2006 : New class (DG);
 38InBlock.gif * 
 39ExpandedBlockEnd.gif */

 40 None.gif
 41 None.gif package  org.jfree.experimental.chart.swt.demo;
 42 None.gif
 43 None.gif import  java.awt.Color;
 44 None.gif
 45 None.gif import  org.eclipse.swt.SWT;
 46 None.gif import  org.eclipse.swt.layout.FillLayout;
 47 None.gif import  org.eclipse.swt.widgets.Display;
 48 None.gif import  org.eclipse.swt.widgets.Shell;
 49 None.gif import  org.jfree.chart.ChartFactory;
 50 None.gif import  org.jfree.chart.JFreeChart;
 51 None.gif import  org.jfree.chart.axis.CategoryAxis;
 52 None.gif import  org.jfree.chart.axis.CategoryLabelPositions;
 53 None.gif import  org.jfree.chart.axis.NumberAxis;
 54 None.gif import  org.jfree.chart.plot.CategoryPlot;
 55 None.gif import  org.jfree.chart.plot.PlotOrientation;
 56 None.gif import  org.jfree.chart.renderer.category.BarRenderer;
 57 None.gif import  org.jfree.data.category.CategoryDataset;
 58 None.gif import  org.jfree.data.category.DefaultCategoryDataset;
 59 None.gif import  org.jfree.experimental.chart.swt.ChartComposite;
 60 None.gif
 61 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
 62InBlock.gif * An SWT demo.
 63ExpandedBlockEnd.gif */

 64 ExpandedBlockStart.gifContractedBlock.gif public   class  SWTBarChartDemo1  dot.gif {
 65InBlock.gif    
 66ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 67InBlock.gif     * Returns a sample dataset.
 68InBlock.gif     * 
 69InBlock.gif     * @return The dataset.
 70ExpandedSubBlockEnd.gif     */

 71ExpandedSubBlockStart.gifContractedSubBlock.gif    private static CategoryDataset createDataset() dot.gif{
 72InBlock.gif        
 73InBlock.gif        // row keysdot.gif
 74InBlock.gif        String series1 = "First";
 75InBlock.gif        String series2 = "Second";
 76InBlock.gif        String series3 = "Third";
 77InBlock.gif
 78InBlock.gif        // column keysdot.gif
 79InBlock.gif        String category1 = "Category 1";
 80InBlock.gif        String category2 = "Category 2";
 81InBlock.gif        String category3 = "Category 3";
 82InBlock.gif        String category4 = "Category 4";
 83InBlock.gif        String category5 = "Category 5";
 84InBlock.gif
 85InBlock.gif        // create the datasetdot.gif
 86InBlock.gif        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 87InBlock.gif
 88InBlock.gif        dataset.addValue(1.0, series1, category1);
 89InBlock.gif        dataset.addValue(4.0, series1, category2);
 90InBlock.gif        dataset.addValue(3.0, series1, category3);
 91InBlock.gif        dataset.addValue(5.0, series1, category4);
 92InBlock.gif        dataset.addValue(5.0, series1, category5);
 93InBlock.gif
 94InBlock.gif        dataset.addValue(5.0, series2, category1);
 95InBlock.gif        dataset.addValue(7.0, series2, category2);
 96InBlock.gif        dataset.addValue(6.0, series2, category3);
 97InBlock.gif        dataset.addValue(8.0, series2, category4);
 98InBlock.gif        dataset.addValue(4.0, series2, category5);
 99InBlock.gif
100InBlock.gif        dataset.addValue(4.0, series3, category1);
101InBlock.gif        dataset.addValue(3.0, series3, category2);
102InBlock.gif        dataset.addValue(2.0, series3, category3);
103InBlock.gif        dataset.addValue(3.0, series3, category4);
104InBlock.gif        dataset.addValue(6.0, series3, category5);
105InBlock.gif        
106InBlock.gif        return dataset;
107InBlock.gif        
108ExpandedSubBlockEnd.gif    }

109InBlock.gif    
110ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
111InBlock.gif     * Creates a sample chart.
112InBlock.gif     * 
113InBlock.gif     * @param dataset  the dataset.
114InBlock.gif     * 
115InBlock.gif     * @return The chart.
116ExpandedSubBlockEnd.gif     */

117ExpandedSubBlockStart.gifContractedSubBlock.gif    private static JFreeChart createChart(CategoryDataset dataset) dot.gif{
118InBlock.gif        
119InBlock.gif        // create the chartdot.gif
120InBlock.gif        JFreeChart chart = ChartFactory.createBarChart(
121InBlock.gif            "Bar Chart Demo",         // chart title
122InBlock.gif            "Category",               // domain axis label
123InBlock.gif            "Value",                  // range axis label
124InBlock.gif            dataset,                  // data
125InBlock.gif            PlotOrientation.VERTICAL, // orientation
126InBlock.gif            true,                     // include legend
127InBlock.gif            true,                     // tooltips?
128InBlock.gif            false                     // URLs?
129InBlock.gif        );
130InBlock.gif
131InBlock.gif        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHARTdot.gif
132InBlock.gif
133InBlock.gif        // set the background color for the chartdot.gif
134InBlock.gif        chart.setBackgroundPaint(Color.white);
135InBlock.gif
136InBlock.gif        // get a reference to the plot for further customisationdot.gif
137InBlock.gif        CategoryPlot plot = (CategoryPlot) chart.getPlot();
138InBlock.gif        plot.setBackgroundPaint(Color.lightGray);
139InBlock.gif        plot.setDomainGridlinePaint(Color.white);
140InBlock.gif        plot.setDomainGridlinesVisible(true);
141InBlock.gif        plot.setRangeGridlinePaint(Color.white);
142InBlock.gif
143InBlock.gif        // set the range axis to display integers onlydot.gif
144InBlock.gif        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
145InBlock.gif        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
146InBlock.gif
147InBlock.gif        // disable bar outlinesdot.gif
148InBlock.gif        BarRenderer renderer = (BarRenderer) plot.getRenderer();
149InBlock.gif        renderer.setDrawBarOutline(false);
150InBlock.gif
151InBlock.gif        CategoryAxis domainAxis = plot.getDomainAxis();
152InBlock.gif        domainAxis.setCategoryLabelPositions(
153InBlock.gif            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
154InBlock.gif        );
155InBlock.gif        // OPTIONAL CUSTOMISATION COMPLETED.
156InBlock.gif        
157InBlock.gif        return chart;
158InBlock.gif        
159ExpandedSubBlockEnd.gif    }

160InBlock.gif    
161ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
162InBlock.gif     * Starting point for the demonstration application.
163InBlock.gif     *
164InBlock.gif     * @param args  ignored.
165ExpandedSubBlockEnd.gif     */

166InBlock.gif    public static void main( String[] args ) 
167ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
168InBlock.gif        JFreeChart chart = createChart(createDataset());
169InBlock.gif        Display display = new Display();
170InBlock.gif        Shell shell = new Shell(display);
171InBlock.gif        shell.setSize(600300);
172InBlock.gif        shell.setLayout(new FillLayout());
173InBlock.gif        shell.setText("Test for jfreechart running with SWT");
174InBlock.gif        final ChartComposite frame = new ChartComposite(shell, SWT.NONE, chart,
175InBlock.gif                true);
176InBlock.gif        frame.pack();
177InBlock.gif        shell.open();
178ExpandedSubBlockStart.gifContractedSubBlock.gif        while (!shell.isDisposed()) dot.gif{
179InBlock.gif            if (!display.readAndDispatch())
180InBlock.gif                display.sleep();
181ExpandedSubBlockEnd.gif        }

182
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值