5.4、使用BIRT API创建Chart

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();
			}
		}
	}
}

 

执行结果如下:



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值