Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[birt-news] strange error when using chart engine

Hi everybody,

I'm using the chart engine embedded in an eclipse plugin.
In this I create a line chart (ChartWithAxes) and I encounter a quite strange behaviour. When the chart data values in the y-axis approximate, the chart generation freezes (which means it uses almost 100% of the CPU).

I generate the data values from Points, like:
Point point1 = new Point(1179839372937l, 346.0);
pointList.add(point1);
Point point2 = new Point(1279839383156l, 340.0);
pointList.add(point2);

I can reproduce this error. When I decrease the second value of point2 to 330.0 (and below) everything works fine. The same is when I put the second value to 350.0 it crashes, whereas with 360.0 (and above) it works.

Anybody got an idea what to do?

Best regards,

Abid

The Chart is generated by this code:
public static final Chart createLineChart() {

	ChartWithAxes lineChart = ChartWithAxesImpl.create();
	// Plot
	lineChart.getBlock().setBackground(ColorDefinitionImpl.WHITE());
	Plot p = lineChart.getPlot();
	p.getClientArea().setBackground(
			ColorDefinitionImpl.create(255, 255, 225));

	// Title
lineChart.getTitle().getLabel().getCaption().setValue("History");

	// Legend
	lineChart.getLegend().setVisible(false);

	// X-Axis
	Axis xAxisPrimary = lineChart.getPrimaryBaseAxes()[0];
	// xAxisPrimary.setType(AxisType.TEXT_LITERAL);
		xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
	// xAxisPrimary.getMajorGrid().setTickCount(3);
		xAxisPrimary.getOrigin().setType(IntersectionType.VALUE_LITERAL);
	xAxisPrimary.getTitle().getCaption().setValue("Time");
	xAxisPrimary.getTitle().setVisible(true);

	// Y-Axis
	Axis yAxisPrimary = lineChart.getPrimaryOrthogonalAxis(xAxisPrimary);
		yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
		yAxisPrimary.getOrigin().setType(IntersectionType.VALUE_LITERAL);
		// yAxisPrimary.getTitle().getCaption().setValue(history.getModule());
		yAxisPrimary.getTitle().getCaption().setValue("Battery");
	yAxisPrimary.getTitle().setVisible(true);

	Collection<Point> pointList = new ArrayList<Point>();
	Point point1 = new Point(1179839372937l, 346.0);
	pointList.add(point1);
	Point point2 = new Point(1279839383156l, 340.0);
	pointList.add(point2);

	String[] xValues = new String[pointList.size()];
	double[] yValues = new double[pointList.size()];

	int j = 0;
	for (Point point : pointList) {
		xValues[j] = DateFormat.getDateTimeInstance().format(
				new Date(point.getDate()));
		yValues[j] = point.getValue();
		j++;
	}

	logger.debug("Values (" + pointList + ") initialized.");

	// Data Set
	TextDataSet xDataSet = TextDataSetImpl.create(xValues);
	NumberDataSet yDataSet = NumberDataSetImpl.create(yValues);

	// X-Series
	Series xSeries = SeriesImpl.create();
	xSeries.setDataSet(xDataSet);
	SeriesDefinition xSeriesDefinition = SeriesDefinitionImpl.create();
		xAxisPrimary.getSeriesDefinitions().add(xSeriesDefinition);
	xSeriesDefinition.getSeries().add(xSeries);

	// Y-Series
	LineSeries ySeries = (LineSeries) LineSeriesImpl.create();
	ySeries.setDataSet(yDataSet);
		ySeries.getLineAttributes().setColor(ColorDefinitionImpl.CREAM());
	for (int i = 0; i < ySeries.getMarkers().size(); i++) {
		((Marker) ySeries.getMarkers().get(i))
				.setType(MarkerType.RECTANGLE_LITERAL);
	}
	ySeries.getLabel().setVisible(true);

	SeriesDefinition ySeriesDefinition = SeriesDefinitionImpl.create();
	ySeriesDefinition.getSeriesPalette().update(-2);
		yAxisPrimary.getSeriesDefinitions().add(ySeriesDefinition);
	ySeriesDefinition.getSeries().add(ySeries);
		return lineChart;
}

The Chart is painted by this code (The line marked with '-->' lets the application crash):

public void paintControl(PaintEvent e) {

	Chart chart = createLineChart();	
	Rectangle rectangle = this.getClientArea();
	Image imgChart = new Image(this.getDisplay(), rectangle);
	GC gcImage = new GC(imgChart);
	
	deviceRenderer.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT,
					gcImage);

	Bounds bounds = BoundsImpl.create(0, 0, rectangle.width,
					rectangle.height);
	bounds.scale(72d / deviceRenderer.getDisplayServer()
					.getDpiResolution());
			
	Generator generator = Generator.instance();
			
	try {

--> gcs = generator.build(deviceRenderer.getDisplayServer(), chart, bounds, null, null, null);
	
		generator.render(deviceRenderer, gcs);
	
		GC gc = e.gc;
		gc.drawImage(imgChart, rectangle.x, rectangle.y);
			
	} catch (ChartException gex) {
		logger.error("Couldn't create chart.", gex);
	}
}


--

Abid Hussain
Mail: abid.hussain@xxxxxxxxx
Web: http://www.abid76.de


Back to the top