How to use JFreeChart with SWT (includes sample code)
Re: How to use JFreeChart with SWT (includes sample code)
thanks ,use these code ,i can draw chart in swt window when click a buttion,but when i draw new chart the second time,the chart not changed ,what should i do to refresh panel or frame?Robert Gash wrote:-Robert Gash, Software Developer, Enkia CorporationCode: Select all
/// STEP 1 //////////////////////////////////////////////// // create a composite suitable for displaying all of our chart data Composite chartComposite = new Composite(/* options */); chartComposite.setLayoutData(/* set the layout options */); /// STEP 2 //////////////////////////////////////////////// // create the dataset for the pie chart of the distribution DefaultPieDataset pieData = new DefaultPieDataset(); /* populate the pieData here */ JFreeChart chart = ChartFactory.createPie3DChart(/* some options */); // OPTIONAL: tinkering with plot options here /// STEP 3 //////////////////////////////////////////////// // Grab the background color from the SWT Composite // so our AWT panel "matches" the SWT Composite Color backgroundColor = chartComposite.getBackground(); // create the panel Panel chartPanel = SWT_AWT.new_Panel(chartComposite); // set the panel's background (defaults to pure white) chartPanel.setBackground(new java.awt.Color(backgroundColor.getRed(), backgroundColor.getGreen(), backgroundColor.getBlue())); // set the AWT layout manager chartPanel.setLayout(/* some AWT/Swing layout manager like BoxLayout */); /// STEP 4 //////////////////////////////////////////////// ChartPanel jfreeChartPanel = new ChartPanel(chart); chartPanel.add(jfreeChartPanel);
gashalot <at> enkia.com
i have search the forum ,it seems that chartpanel.setRefreshBuffer(true) could do; but in fact it is no effect.
This is a nice solution, thanks for posting the code, Til.
However, I am still searching for a way to get JFreeChart to have full functionality in my Eclipse RCP application. Has anyone done this?
I was thinking of rebuilding JFreeChart after replacing Swing and AWT imports with the corresponding SwingWT imports. Has anyone tried this or is this not a reasonable thing to do?
However, I am still searching for a way to get JFreeChart to have full functionality in my Eclipse RCP application. Has anyone done this?
I was thinking of rebuilding JFreeChart after replacing Swing and AWT imports with the corresponding SwingWT imports. Has anyone tried this or is this not a reasonable thing to do?
Hi,
I would doubt that this is reasonable at all (or even needed).
The major part of JFreeChart deals with getting the chart stuff into a Graphics2D object. As long as you can display Graphics2D content in SWT, you dont have to touch that code at all. (And it should be possible at least by using a BufferedImage or by converting the image into a PNG and then displaying that image. As I'm not a SWT user for religious reasons (100% pure Java
), I cant tell you how this could be done.)
The stuff you might have to convert is the whole Swing layer working with the ChartPanel.
As a fast solution, the SwingSWT project jumps in mind, which should provide a Swing-Implementation that uses the SWT as backend.
If that does not work, you have to go the dirty way of reimplementing that GUI stuff (namely the property dialogs, the context menus etc.). It shouldn't be hard, just time consuming.
As SWT is not Swing, you will have to change the underlying implementation, as SWT violates the Java way of letting the Garbage Collector deal with clean ups. So you have to implement some sort of resource management, or things will become a mess.
Regards,
Thomas
I would doubt that this is reasonable at all (or even needed).
The major part of JFreeChart deals with getting the chart stuff into a Graphics2D object. As long as you can display Graphics2D content in SWT, you dont have to touch that code at all. (And it should be possible at least by using a BufferedImage or by converting the image into a PNG and then displaying that image. As I'm not a SWT user for religious reasons (100% pure Java

The stuff you might have to convert is the whole Swing layer working with the ChartPanel.
As a fast solution, the SwingSWT project jumps in mind, which should provide a Swing-Implementation that uses the SWT as backend.
If that does not work, you have to go the dirty way of reimplementing that GUI stuff (namely the property dialogs, the context menus etc.). It shouldn't be hard, just time consuming.
As SWT is not Swing, you will have to change the underlying implementation, as SWT violates the Java way of letting the Garbage Collector deal with clean ups. So you have to implement some sort of resource management, or things will become a mess.
Regards,
Thomas
Using JFreechart in Eclipse RCP application
Hi All,
I am trying to use JFreeChart in my eclipse RCP application.
the source code is as follows: -
/*
* Created on Sep 28, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/* -------------------
* LineChartDemo1.java
* -------------------
* (C) Copyright 2002-2005, by Object Refinery Limited.
*
*/
package edu.wustl.lims.ui.pAssayView.samplesAssPrepView;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
/**
* A simple demonstration application showing how to create a line chart using
* data from a {@link CategoryDataset}.
*/
public class LineChartDemo1 extends TitleAreaDialog {
CategoryDataset dataset = null;
JFreeChart chart = null;
/**
* Creates a new demo.
*
* @param title the frame title.
*/
public LineChartDemo1(Shell parent) {
super(parent);
//dataset = createDataset();
//chart = createChart(dataset);
// ChartPanel chartPanel = new ChartPanel(chart);
// chartPanel.setPreferredSize(new Dimension(500, 270));
//setContentPane(chartPanel);
}
protected Control createDialogArea(Composite parent)
{
Display display = new Display();
Shell shell = new Shell(display);
Composite chartComposite = new Composite(shell, SWT.EMBEDDED);
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
//Creating a frame
java.awt.Frame frame = SWT_AWT.new_Frame(chartComposite);
javax.swing.JPanel panel = new javax.swing.JPanel( );
// //Creating a Chart Panel
ChartPanel chartPanel = new ChartPanel(chart);
// chartPanel.setPreferredSize(new Dimension(500, 270));
//
// //Adding a Chart Panel to a frame
frame.add(chartPanel);
return parent;
}
/**
* Creates a sample dataset.
*
* @return The dataset.
*/
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(212, "Classes", "JDK 1.0");
dataset.addValue(504, "Classes", "JDK 1.1");
dataset.addValue(1520, "Classes", "SDK 1.2");
dataset.addValue(1842, "Classes", "SDK 1.3");
dataset.addValue(2991, "Classes", "SDK 1.4");
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset a dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(CategoryDataset dataset) {
//
String arg0 ="Java Standard Class Library";
String arg1 = "Release";
String arg2 = "Class Count";
//create the chart...
JFreeChart chart = ChartFactory.createLineChart(
arg0, // chart title
arg1, // domain axis label
arg2, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
chart.addSubtitle(new TextTitle("Number of Classes By Release"));
TextTitle source = new TextTitle(
"Source: Java In A Nutshell (4th Edition) "
+ "by David Flanagan (O’Reilly)"
);
source.setFont(new Font("SansSerif", Font.PLAIN, 10));
source.setPosition(RectangleEdge.BOTTOM);
source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
chart.addSubtitle(source);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// customise the renderer...
LineAndShapeRenderer renderer
= (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
return chart;
}
}
When I try to invoeke this dialog box I get the following error
Unhandled event loop exception
Reason:
org/jfree/chart/title/Title
Does anyone has any idea why this is happening ?
I am trying to use JFreeChart in my eclipse RCP application.
the source code is as follows: -
/*
* Created on Sep 28, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/* -------------------
* LineChartDemo1.java
* -------------------
* (C) Copyright 2002-2005, by Object Refinery Limited.
*
*/
package edu.wustl.lims.ui.pAssayView.samplesAssPrepView;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
/**
* A simple demonstration application showing how to create a line chart using
* data from a {@link CategoryDataset}.
*/
public class LineChartDemo1 extends TitleAreaDialog {
CategoryDataset dataset = null;
JFreeChart chart = null;
/**
* Creates a new demo.
*
* @param title the frame title.
*/
public LineChartDemo1(Shell parent) {
super(parent);
//dataset = createDataset();
//chart = createChart(dataset);
// ChartPanel chartPanel = new ChartPanel(chart);
// chartPanel.setPreferredSize(new Dimension(500, 270));
//setContentPane(chartPanel);
}
protected Control createDialogArea(Composite parent)
{
Display display = new Display();
Shell shell = new Shell(display);
Composite chartComposite = new Composite(shell, SWT.EMBEDDED);
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
//Creating a frame
java.awt.Frame frame = SWT_AWT.new_Frame(chartComposite);
javax.swing.JPanel panel = new javax.swing.JPanel( );
// //Creating a Chart Panel
ChartPanel chartPanel = new ChartPanel(chart);
// chartPanel.setPreferredSize(new Dimension(500, 270));
//
// //Adding a Chart Panel to a frame
frame.add(chartPanel);
return parent;
}
/**
* Creates a sample dataset.
*
* @return The dataset.
*/
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(212, "Classes", "JDK 1.0");
dataset.addValue(504, "Classes", "JDK 1.1");
dataset.addValue(1520, "Classes", "SDK 1.2");
dataset.addValue(1842, "Classes", "SDK 1.3");
dataset.addValue(2991, "Classes", "SDK 1.4");
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset a dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(CategoryDataset dataset) {
//
String arg0 ="Java Standard Class Library";
String arg1 = "Release";
String arg2 = "Class Count";
//create the chart...
JFreeChart chart = ChartFactory.createLineChart(
arg0, // chart title
arg1, // domain axis label
arg2, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
chart.addSubtitle(new TextTitle("Number of Classes By Release"));
TextTitle source = new TextTitle(
"Source: Java In A Nutshell (4th Edition) "
+ "by David Flanagan (O’Reilly)"
);
source.setFont(new Font("SansSerif", Font.PLAIN, 10));
source.setPosition(RectangleEdge.BOTTOM);
source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
chart.addSubtitle(source);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// customise the renderer...
LineAndShapeRenderer renderer
= (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
return chart;
}
}
When I try to invoeke this dialog box I get the following error
Unhandled event loop exception
Reason:
org/jfree/chart/title/Title
Does anyone has any idea why this is happening ?
-
- Posts: 3
- Joined: Wed Apr 04, 2007 2:15 pm
Robert Gash wrote:Jon,Jon wrote: swt Composite, and is constructed using a WEmbeddedFrame from Sun's
internal package sun.awt.windows. Sun has changed this internal code
in jdk 1.4, and so this undocumented Eclipse mechanism does not work
with that version.
Any on e have any suggestions on how I can get graphs to display inside my SWT application?
I actually just tested a new demo using SWT 3.0M8, J2SDK 1.4.2_03, and JFreeChart 0.9.17 on my Windows XP system (SP1). I was able to use JFreeChart without any problems.
The sample code can be found at http://www.gashalot.com/software/JFreeChartSWTDemo.java.
Let me know if you have any questions.
-R
I tried to use the sample code you provided, but eclipse gives me errors on the imports.
I didnt add "org.eclipse.swt" , but I'm sure I added "org.jfree.chart" to the eclipse plugin folder. But both give me errors.
1- Where can I get all those imports?
2- How do I set them up?
Thanks
Hi,
I just used Tils way to get a SWT-Image, but I got a bad performance. I use a modified plot of JFreeChart and when the range of values is over 100000 it takes a few seconds (1-2
) to buffer the chart. I used the way creating a PNG and using the bytearray etc. It seems to be a problem to buffer a chart with high values. Do you have any idea?
I just used Tils way to get a SWT-Image, but I got a bad performance. I use a modified plot of JFreeChart and when the range of values is over 100000 it takes a few seconds (1-2

Re: How to use JFreeChart with SWT (includes sample code)
Hello all,
i think i'm getting same two errors as you are Ricardo. If you solved or then you will solve this matter, please share your information, i am really in need of it. Waiting your answer.
Thanks, micky Simulation pret
i think i'm getting same two errors as you are Ricardo. If you solved or then you will solve this matter, please share your information, i am really in need of it. Waiting your answer.
Thanks, micky Simulation pret
-
- Posts: 3
- Joined: Tue Jan 03, 2012 12:39 pm
- antibot: No, of course not.
Re: How to use JFreeChart with SWT (includes sample code)
Hi Guys,
I don't want to mix swt and awt for creating graph.
Will you please suggest me any other alternate solution so that i can achieve the graph creation only using swt.
Thanks
I don't want to mix swt and awt for creating graph.
Will you please suggest me any other alternate solution so that i can achieve the graph creation only using swt.
Thanks
-
- Posts: 8
- Joined: Thu Feb 07, 2019 5:49 pm
- antibot: No, of course not.
Re: How to use JFreeChart with SWT (includes sample code)
Hi!There isn't a method new_panel for SWT_AWT. I'm using org.eclipse.swt.gtk.linixx86_64_3.108etc...
-
- Posts: 1
- Joined: Wed Feb 27, 2019 12:00 pm
- antibot: No, of course not.
Re: How to use JFreeChart with SWT (includes sample code)
I am an italian engineer and I don't pass on english extraordinary, miserable. I have seen your message yet i'm no longer in a circumstance to put into apply your answer in light of the way that darken instruct me that the system SWT_AWT.New_Composite now not exists. How am I prepared to do?