java - 如何使用Jfreechart在Eclipse插件 View 中绘制XY折线图

标签 java eclipse eclipse-plugin jfreechart

我试图在 Eclipse 插件 View 中绘制 XY 图表,但我的代码中有一个错误,我找不到它在哪里。

我使用了此链接中的示例 http://www.codejava.net/java-se/graphics/using-jfreechart-to-draw-xy-line-chart-with-xydataset

package Views;

import java.awt.Color; 
import java.awt.BasicStroke; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

class XYplot extends ViewPart
{
    public XYplot() 
    {
    }

    @Override
    public void createPartControl(Composite parent) 
    {
          XYLineChart_AWT chart = new XYLineChart_AWT("Browser Usage Statistics", "Which Browser are you using?");
          chart.pack( );          
          RefineryUtilities.centerFrameOnScreen( chart );          
          chart.setVisible( true );
    }

    private void createActions() 
    {
        // Create the actions
    }

    private void initializeToolBar() 
    {
        IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
    }

    private void initializeMenu()
    {
        IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
    }

    @Override
    public void setFocus() 
    {
        // Set the focus
    }


    class XYLineChart_AWT extends ApplicationFrame 
    {
       public XYLineChart_AWT( String applicationTitle, String chartTitle )
       {
          super(applicationTitle);
          JFreeChart xylineChart = ChartFactory.createXYLineChart(
             chartTitle ,
             "Category" ,
             "Score" ,
             createDataset() ,
             PlotOrientation.VERTICAL ,
             true , true , false);

          ChartPanel chartPanel = new ChartPanel( xylineChart );
          chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
          final XYPlot plot = xylineChart.getXYPlot( );
          XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
          renderer.setSeriesPaint( 0 , Color.RED );
          renderer.setSeriesPaint( 1 , Color.GREEN );
          renderer.setSeriesPaint( 2 , Color.YELLOW );
          renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
          renderer.setSeriesStroke( 1 , new BasicStroke( 3.0f ) );
          renderer.setSeriesStroke( 2 , new BasicStroke( 2.0f ) );
          plot.setRenderer( renderer ); 
          setContentPane( chartPanel ); 
       }

       private XYDataset createDataset( )
       {
          final XYSeries firefox = new XYSeries( "Firefox" );          
          firefox.add( 1.0 , 1.0 );          
          firefox.add( 2.0 , 4.0 );          
          firefox.add( 3.0 , 3.0 );          
          final XYSeries chrome = new XYSeries( "Chrome" );          
          chrome.add( 1.0 , 4.0 );          
          chrome.add( 2.0 , 5.0 );          
          chrome.add( 3.0 , 6.0 );          
          final XYSeries iexplorer = new XYSeries( "InternetExplorer" );          
          iexplorer.add( 3.0 , 4.0 );          
          iexplorer.add( 4.0 , 5.0 );          
          iexplorer.add( 5.0 , 4.0 );          
          final XYSeriesCollection dataset = new XYSeriesCollection( );          
          dataset.addSeries( firefox );          
          dataset.addSeries( chrome );          
          dataset.addSeries( iexplorer );
          return dataset;
       }

    }
}

the output

.log文件内容

!SESSION 2016-06-13 16:10:18.594 -----------------------------------------------
eclipse.buildId=4.5.1.M20150904-0015
java.version=1.8.0_66
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.committers.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.committers.product

!ENTRY org.eclipse.epp.logging.aeri.ui 2 19 2016-06-13 16:11:14.840
!MESSAGE Updating the index from remote failed. Version: 1.0.1.v20150913-0716
!STACK 0
java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at java.util.zip.ZipInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at com.google.common.io.ByteStreams.copy(ByteStreams.java:169)
    at com.google.common.io.ByteSink.writeFrom(ByteSink.java:134)
    at org.eclipse.epp.internal.logging.aeri.ui.utils.Zips.unzip(Zips.java:83)
    at org.eclipse.epp.internal.logging.aeri.ui.log.ProblemsDatabaseUpdateJob.run(ProblemsDatabaseUpdateJob.java:71)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

!ENTRY org.eclipse.egit.ui 2 0 2016-06-13 16:11:35.087
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Amr'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.

!ENTRY org.eclipse.ui.monitoring 2 0 2016-06-13 16:11:45.478
!MESSAGE UI freeze of 0.71s at 16:11:44.762

!ENTRY org.eclipse.ui.monitoring 2 0 2016-06-13 16:11:47.920
!MESSAGE UI freeze of 0.58s at 16:11:47.334

!ENTRY org.eclipse.ui.monitoring 2 0 2016-06-13 16:11:52.904
!MESSAGE UI freeze of 0.63s at 16:11:52.256

控制台输出

!SESSION 2016-06-13 16:23:58.435 -----------------------------------------------
eclipse.buildId=4.5.1.M20150904-0015
java.version=1.8.0_66
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments:  -product org.eclipse.platform.ide
Command-line arguments:  -product org.eclipse.platform.ide -data E:\Intense\GitHub/runtime-NatTableExamples -dev file:E:/Intense/GitHub/.metadata/.plugins/org.eclipse.pde.core/NatTableExamples/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog

!ENTRY org.eclipse.ui 4 4 2016-06-13 16:24:07.361
!MESSAGE Unable to create part

!ENTRY org.eclipse.ui 4 1 2016-06-13 16:24:07.366
!MESSAGE Plug-in "diagnostictool" was unable to instantiate class "Views.XYplot".
!STACK 1
org.eclipse.core.runtime.CoreException: Plug-in "diagnostictool" was unable to instantiate class "Views.XYplot".
    at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.throwException(RegistryStrategyOSGI.java:194)
    at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:188)
    at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:905)
    at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243)
    at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:55)
    at org.eclipse.ui.internal.registry.ViewDescriptor.createView(ViewDescriptor.java:58)

最佳答案

createPartControl()的具体实现“为此工作台部分创建 SWT 控件。”您正在创建一个ApplicationFrameJFrame 的子类,它是一个 Swing top-level container 。您可能想要创建一个 ChartComposite。尽管我没有尝试过,但发行版中的 swt/org/jfree/experimental 目录中有一些示例。

关于java - 如何使用Jfreechart在Eclipse插件 View 中绘制XY折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790607/

相关文章:

java - 将超链接插入 XWPFTableRow

java - 当模拟器启动并尝试连接到数据库时,应用程序意外停止

java - eclim 不在 vim 中进行自动完成

eclipse - 如何解决 Gradle 插件依赖?

java - Solr - 匹配以特定单词开头的句子

java - 极其紧凑的 UUID(使用所有字母数字字符)

eclipse - Eclipse 中是否有相当于 "Add existing file"(用于项目)的内容?

java - 如何使用 eclipse 在 J2ME 中使用 bouncy caSTLe API?

eclipse-plugin - 使用指向本地 p2 存储库的 tycho 构建本地 eclipse 插件

eclipse-plugin - 创建类似 GUI 的网格(类似于 Microsoft excel 电子表格)