当我在Eclipse RCP插件的视图中使用Jfreechart显示条形图时,条形图标题、域轴名称和值轴名称不可见。
以下是在视图中显示条形图的代码:
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartComposite frame = new ChartComposite(barchartComposite,SWT.NONE,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
frame.pack();
frame.setVisible(true);
frame.setChart(chart);
frame.forceRedraw();
barchartComposite.getLayout();
private CategoryDataset createDataset() {
// row keys...
final String series1 = "First";
// column keys...
final String category1 = "error";
final String category2 = "info";
final String category3 = "warning";
final String category4 = "critical";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10, series1, category1);
dataset.addValue(5, series1, category2);
dataset.addValue(6, series1, category3);
dataset.addValue(9, series1, category4);
return dataset;
}
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Priority BarChart", // chart title
"priority", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
final BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.lightGray
);
renderer.setSeriesPaint(0, gp0);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
以上代码在eclipse RCP插件的视图上创建了一个条形图。 createdataset()方法生成图表数据 createChart()方法生成图表。