public class CreateChartReport implements PaintListener{
public static final String BIRT_HOME = "D:/DeveloperTools/birt-runtime-2_3_1/ReportEngine";
private IDeviceRenderer iDeviceRenderer;
private Chart chart;
public CreateChartReport() {
final PluginSettings pluginSettings = PluginSettings.instance();
try {
iDeviceRenderer = pluginSettings.getDevice("dv.SWT");
}catch (Exception e) {
e.printStackTrace();
}
chart = createMyChart();
}
public static Chart createMyChart() {
ChartWithAxes chartWithAxes = ChartWithAxesImpl.create();
chartWithAxes.getBlock().setBackground(ColorDefinitionImpl.WHITE());
chartWithAxes.getBlock().getOutline().setVisible(true);
chartWithAxes.setDimension(ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL);
//customize the plot
Plot plot = chartWithAxes.getPlot();
plot.getClientArea().setBackground(ColorDefinitionImpl.create(255, 255,255));
plot.getOutline().setVisible(false);
chartWithAxes.getTitle().getLabel().getCaption().setValue("Simple Bar Chart");
//customize the legend
Legend legend = chartWithAxes.getLegend();
legend.getText().getFont().setSize(16);
legend.getInsets().set(10, 5, 0, 0);
legend.setAnchor(Anchor.NORTH_LITERAL);
//customize the x-axis
Axis xAxisPrimary = chartWithAxes.getPrimaryBaseAxes()[0];
xAxisPrimary.setType(AxisType.TEXT_LITERAL);
xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
xAxisPrimary.getOrigin().setType(IntersectionType.VALUE_LITERAL);
xAxisPrimary.getTitle().setVisible(false);
Axis yAxisPrimary = chartWithAxes.getPrimaryOrthogonalAxis(xAxisPrimary);
yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
yAxisPrimary.setType(AxisType.LINEAR_LITERAL);
yAxisPrimary.getLabel().getCaption().getFont().setRotation(90);
//initialize a collection with the x-series data
Vector vs = new Vector();
vs.add("zero");
vs.add("one");
vs.add("two");
vs.add("three");
TextDataSet categoryValues = TextDataSetImpl.create(vs);
//initialize a collectioin with the y-serise data
ArrayList vn1 = new ArrayList();
vn1.add(new Double(25));
vn1.add(new Double(35));
vn1.add(new Double(-45));
vn1.add(new Double(60));
NumberDataSet orthoValue1 = NumberDataSetImpl.create(vn1);
//create the category base series;
Series seCategory = SeriesImpl.create();
seCategory.setDataSet(categoryValues);
//create the value orthogonal series
BarSeries bs1 = (BarSeries)BarSeriesImpl.create();
bs1.setSeriesIdentifier("My Bar Series");
bs1.setDataSet(orthoValue1);
bs1.setRiserOutline(null);
bs1.getLabel().setVisible(true);
bs1.setLabelPosition(Position.INSIDE_LITERAL);
//create the value orthogonal series
// BarSeries bs2 = (BarSeries)BarSeriesImpl.create();
// bs2.setSeriesIdentifier("My Bar Series 2");
// bs2.setDataSet(orthoValue1);
// bs2.setRiserOutline(null);
// bs2.getLabel().setVisible(true);
// bs2.setLabelPosition(Position.BELOW_LITERAL);
//wrap the base series in the x-axis series definiation
SeriesDefinition sdx = SeriesDefinitionImpl.create();
sdx.getSeriesPalette().shift(0);
xAxisPrimary.getSeriesDefinitions().add(sdx);
sdx.getSeries().add(seCategory);
//wrap the orthogonal series in the x-axis series definal
SeriesDefinition sdy = SeriesDefinitionImpl.create();
sdy.getSeriesPalette().shift(1);
yAxisPrimary.getSeriesDefinitions().add(sdy);
sdy.getSeries().add(bs1);
return chartWithAxes;
}
@Override
public void paintControl(PaintEvent pe) {
iDeviceRenderer.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT, pe.gc);
Composite composite = (Composite)pe.getSource();
Rectangle rect = composite.getClientArea();
Bounds bounds = BoundsImpl.create(rect.x, rect.y, rect.width, rect.height);
bounds.scale(72d/iDeviceRenderer.getDisplayServer().getDpiResolution());
Generator generator = Generator.instance();
try {
generator.render(iDeviceRenderer, generator.build(iDeviceRenderer.getDisplayServer(), chart, null, bounds, null));
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CreateChartReport chartReport = new CreateChartReport();
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns =1;
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setSize(900, 700);
shell.setLayout(gridLayout);
shell.setText(chartReport.getClass().getName()+"[device="+chartReport.iDeviceRenderer.getClass().getName()+"]");
GridData gridData = new GridData(GridData.FILL_BOTH);
Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.setLayoutData(gridData);
canvas.addPaintListener(chartReport);
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
}
}
执行结果如下: