i am doing eclipse plugin and need to implement a linechart in a View. My problem as the subject states, i dont see the labels in the y and x axis neither the legend labels. Below is the working code as a java app then follows my code "almost the same" but the chart is to be shown in a View. I used org.jfree.experimental.chart.swt.ChartComposite to contain the line chart. thank you for any help!
working snippet using ApplicationFrame http://goo.gl/Klt9F
Code: Select all
public LineChartDemo1(final String title) {
super(title);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
private CategoryDataset createDataset() {
// row keys...
final String series1 = "First";
final String series2 = "Second";
//final String series3 = "Third";
// column keys...
final String type1 = "Type 1";
final String type2 = "Type 2";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(5.0, series2, type1);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
return dataset;
}
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
"Line Chart Demo 1", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
// customise the range axis...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0, 100);
rangeAxis.setTickUnit(new NumberTickUnit(10));
return chart;
}
public static void main(final String[] args) {
final LineChartDemo1 demo = new LineChartDemo1("Line Chart Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Code: Select all
public class QualityView extends ViewPart {
private DefaultCategoryDataset dataset;
private JFreeChart chart;
private ChartComposite chartComposite;
SashForm sashForm;
public QualityView() {
// TODO Auto-generated constructor stub
}
private DefaultCategoryDataset createCategoryDataSet(String analysis){
//row keys
final String series1 = "Precision";
final String series2 = "Recall";
final String type1 = "Type 1";
final String type2 = "Type 2";
final String type3 = "Type 3";
final String type4 = "Type 4";
final String type5 = "Type 5";
final String type6 = "Type 6";
final String type7 = "Type 7";
final String type8 = "Type 8";
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10.0, series1, type1);
dataset.addValue(40.0, series1, type2);
dataset.addValue(30.0, series1, type3);
dataset.addValue(50.0, series1, type4);
dataset.addValue(50.0, series1, type5);
dataset.addValue(70.0, series1, type6);
dataset.addValue(70.0, series1, type7);
dataset.addValue(80.0, series1, type8);
dataset.addValue(50.0, series2, type1);
dataset.addValue(70.0, series2, type2);
dataset.addValue(60.0, series2, type3);
dataset.addValue(80.0, series2, type4);
dataset.addValue(40.0, series2, type5);
dataset.addValue(40.0, series2, type6);
dataset.addValue(20.0, series2, type7);
dataset.addValue(10.0, series2, type8);
return dataset;
}
private JFreeChart createLineChart(final CategoryDataset dataset){
final JFreeChart chart = ChartFactory.createLineChart("Analysis Quality", "Time", "Quality",
dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
// customise the range axis...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0, 100);
rangeAxis.setTickUnit(new NumberTickUnit(10));
return chart;
}
synchronized public void updateLineChart(final String analysisName){
UIUtils.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
CategoryDataset dataset = createCategoryDataSet(analysisName);
JFreeChart chart = createLineChart(dataset);
chartComposite.setChart(chart);
chartComposite.chartChanged(new ChartChangeEvent(chart));
}
});
}
@Override
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(4, false));
@Override
public void widgetSelected(SelectionEvent e) {
updateLineChart("string");
}
});
sashForm = new SashForm(parent, SWT.VERTICAL|SWT.BORDER|SWT.SMOOTH);
GridData gd_sashForm = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_sashForm.horizontalSpan = 4;
sashForm.setLayoutData(gd_sashForm);
sashForm.SASH_WIDTH = 1;
// TODO Auto-generated method stub
chartComposite = new ChartComposite(sashForm, SWT.NONE,null,true);
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}