在Eclipse RCP中使用JFreeChart指南

14年前
这是一篇文章介绍了如何在Eclipse Rich Client Platform (RCP)应用程序使用JFreeChart制图组件。用到了JFreeChart SWT实现,最新的Eclipse3.6(Helios)开发工具和JFreeChart1.0.13版本。

1.生成饼状图:
import java.awt.Font;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {

public void createPartControl(Composite parent) {
JFreeChart chart = createChart(createDataset());
new ChartComposite(parent, SWT.NONE,
chart, true);
}

public void setFocus() {
}

/**
* Creates the Dataset for the Pie chart
*/
private PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("One", new Double(43.2));
dataset.setValue("Two", new Double(10.0));
dataset.setValue("Three", new Double(27.5));
dataset.setValue("Four", new Double(17.5));
dataset.setValue("Five", new Double(11.0));
dataset.setValue("Six", new Double(19.4));
return dataset;
}

/**
* Creates the Chart based on a dataset
*/
private JFreeChart createChart(PieDataset dataset) {

JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart
// title
dataset, // data
true, // include legend
true, false);

PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setNoDataMessage("No data available");
plot.setCircular(false);
plot.setLabelGap(0.02);
return chart;

}
}


2.生成柱状图
import java.awt.Color;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {

private CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

// row keys...
String series1 = "First";
String series2 = "Second";

// column keys...
String category1 = "Label 1";
String category2 = "Label 2";
String category3 = "Label 3";

dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);

dataset.addValue(5.0, series2, category1);
dataset.addValue(7.0, series2, category2);
dataset.addValue(6.0, series2, category3);

return dataset;
}

/**
* Creates a chart.
*
* @param dataset
* dataset.
*
* @return A chart.
*/
private JFreeChart createChart(CategoryDataset dataset) {

JFreeChart chart = ChartFactory.createBarChart("Bar Chart", // chart
// title
"Labels", // domain axis label
"Values", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);

CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
return chart;

}

public void createPartControl(Composite parent) {
JFreeChart chart = createChart(createDataset());
final ChartComposite frame = new ChartComposite(parent, SWT.NONE,
chart, true);
}

public void setFocus() {
}
}

英文原文地址:http://www.vogella.de/articles/EclipseJFreeChart/article.html