Search code examples
javaeclipseeclipse-pluginjfreechart

how to use Jfreechart to draw XY line chart in Eclipse plug-in view


I am trying to draw an XY chart in eclipse plugin view but there is a mistake in my code and I can't find where it is.

I used the example in this link 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 file content

!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

console output

!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)

Solution

  • A concrete implementatoin of createPartControl() "Creates the SWT controls for this workbench part." You're creating an ApplicationFrame, a subclass of JFrame, which is a Swing top-level container. You probably want to create a ChartComposite. Although I've not tried them, there are some examples in the swt/org/jfree/experimental directory included in the distribution.