| [birt-charting-dev] data autobinding & piechart rendering |
Exception in thread "main"
java.lang.NullPointerExceptionat org.eclipse.birt.chart.internal.datafeed.GroupingLookupHelper.addLookupForBaseSeries(
GroupingLookupHelper.java:262)at org.eclipse.birt.chart.internal.datafeed.GroupingLookupHelper.initRowExpressions(
GroupingLookupHelper.java:419)at org.eclipse.birt.chart.internal.datafeed.GroupingLookupHelper.<init>(
GroupingLookupHelper.java:95)at org.eclipse.birt.chart.internal.datafeed.DataProcessor.mapToChartResultSet(
DataProcessor.java:318)at org.eclipse.birt.chart.internal.datafeed.DataProcessor.generateRuntimeSeries(
DataProcessor.java:496)at org.eclipse.birt.chart.factory.Generator.bindData(
Generator.java:642)at org.eclipse.birt.chart.factory.Generator.bindData(
Generator.java:594)at PieChartBuilder.createImg(
PieChartBuilder.java:128)at PieChartBuilder.build(
PieChartBuilder.java:78)at PieChartBuilder.main(
PieChartBuilder.java:63)String[] set = {
"Items" };Object[][] data = { {
new Integer(7), new Integer(2), new Integer(5) } };IDataRowExpressionEvaluator dree =
new DataRowExpressionEvaluator(set,data); try { gr.bindData(dree, chart, new RunTimeContext()); // protected IGenerator gr = null;...
import
java.awt.Graphics;import
java.awt.Graphics2D;import
java.awt.image.BufferedImage;import
java.io.File;import
java.io.IOException;import
javax.imageio.ImageIO;import
org.eclipse.birt.chart.api.ChartEngine;import
org.eclipse.birt.chart.device.IDeviceRenderer;import
org.eclipse.birt.chart.exception.ChartException;import
org.eclipse.birt.chart.factory.Generator;import
org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator;import
org.eclipse.birt.chart.factory.RunTimeContext;import
org.eclipse.birt.chart.model.ChartWithoutAxes;import
org.eclipse.birt.chart.model.attribute.Bounds;import
org.eclipse.birt.chart.model.attribute.ChartDimension;import
org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;import
org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;import
org.eclipse.birt.chart.model.component.Series;import
org.eclipse.birt.chart.model.component.impl.SeriesImpl;import
org.eclipse.birt.chart.model.data.NumberDataSet;import
org.eclipse.birt.chart.model.data.Query;import
org.eclipse.birt.chart.model.data.SeriesDefinition;import
org.eclipse.birt.chart.model.data.TextDataSet;import
org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;import
org.eclipse.birt.chart.model.data.impl.QueryImpl;import
org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;import
org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;import
org.eclipse.birt.chart.model.impl.ChartWithoutAxesImpl;import
org.eclipse.birt.chart.model.layout.Legend;import
org.eclipse.birt.chart.model.layout.Plot;import
org.eclipse.birt.chart.model.type.PieSeries;import
org.eclipse.birt.chart.model.type.impl.PieSeriesImpl;import
org.eclipse.birt.core.framework.PlatformConfig;import
com.ibm.icu.util.ULocale;/**
* Builds pie chart. * * @author Qi Liang */public
class PieChartBuilder extends AbstractChartBuilder {SeriesDefinition
sdX = null; /** * Constructor. * * @param dataSet * data for chart */ public PieChartBuilder(DataSet dataSet) { super(dataSet); title = "Pie Chart";}
public static void main(String[] args) {PieChartBuilder p =
new PieChartBuilder(DataSet.getInstance());p.build();
}
/** * Builds one chart. */ public void build() {createChart();
buildPlot();
buildLegend();
buildTitle();
buildXAxis();
// buildYAxis();
buildXSeries();
// buildYSeries();
createImg();
}
public void createImg() {PlatformConfig pf =
new PlatformConfig();pf.setProperty(
"STANDALONE", true); // Returns a singleton instance of the Chart EngineChartEngine ce = ChartEngine.instance(pf);
// Returns a singleton instance of the Generator gr = ce.getGenerator(); try { // device renderers for dv.SWT, dv.PNG, dv.JPG // dv.PDF, dv.SVG, dv.SWING, dv.PNG24, div.BMP dRenderer = ce.getRenderer("dv.PNG"); dServer = dRenderer.getDisplayServer();}
catch (Exception ex) {ex.printStackTrace();
}
BufferedImage img =
new BufferedImage(600, 600,BufferedImage.
TYPE_INT_ARGB);Graphics g = img.getGraphics();
Graphics2D g2d = (Graphics2D) g;
dRenderer.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT, g2d); dRenderer.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "output/DataRowEvaluator.png"); //$NON-NLS-1$ // dRenderer.setProperty(IDeviceRenderer.CACHED_IMAGE, img); //$NON-NLS-1$Bounds bo = BoundsImpl.create(0, 0, 600, 600);
bo.scale(72d /
dRenderer.getDisplayServer().getDpiResolution());// String[] set = { "Items", "Amounts" };//$NON-NLS-1$ //$NON-NLS-2$
String[] set = {
"Items" };//$NON-NLS-1$ //$NON-NLS-2$// Object[][] data = { { "A", "B", "C" },
// { new Integer(7), new Integer(2), new Integer(5) } };
Object[][] data = { {
new Integer(7), new Integer(2), new Integer(5) } }; // Binds data to chart modelIDataRowExpressionEvaluator dree =
new DataRowExpressionEvaluator(set,data);
try { gr.bindData(dree, chart, new RunTimeContext()); gcs = gr.build(dServer, chart, bo, null, null, null); gr.render(dRenderer, gcs);ImageIO.write(img,
"png", new File("output/DataRowEvaluator.png")); // System.out.println(img.getGraphics().toString()); // // O P E N // ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // // try { // // W R I T E // ImageIO.write(img, "png", baos); // // // C L O S E // baos.flush(); // this.iceImage = baos.toByteArray(); // // baos.close(); // // // System.out.println(getIceImage()); // // } catch (IOException e) { // e.printStackTrace(); // }}
catch (ChartException e) {e.printStackTrace();
}
catch (IOException e) {e.printStackTrace();
}
}
/** (non-Javadoc)
*
* @see
* com.ibm.examples.chart.widget.chart.AbstractChartBuilder#createChart()
*/
protected void createChart() { chart = ChartWithoutAxesImpl.create(); chart.setDimension( ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL ); chart.setType("Pie Chart"); chart.setSubType("Standard Pie Chart");}
/** (non-Javadoc)
*
* @see com.ibm.examples.chart.widget.AbstractChartBuilder#buildPlot()
*/
protected void buildPlot() { chart.setSeriesThickness(25); chart.getBlock().setBackground(ColorDefinitionImpl.WHITE());Plot p =
chart.getPlot();p.getClientArea().setBackground(
null);p.getClientArea().getOutline().setVisible(
true);p.getOutline().setVisible(
true);}
/** (non-Javadoc)
*
* @see com.ibm.examples.chart.widget.AbstractChartBuilder#buildLegend()
*/
protected void buildLegend() {Legend lg =
chart.getLegend();lg.getText().getFont().setSize(16);
lg.getOutline().setVisible(
true);}
/** (non-Javadoc)
*
* @see com.ibm.examples.chart.widget.AbstractChartBuilder#buildXSeries()
*/
protected void buildXSeries() { // CREATE THE CATEGORY SERIESSeries seCategory = SeriesImpl.create();
TextDataSet categoryValues = TextDataSetImpl
.create(
new String[]{"A","B","C"});seCategory.setDataSet(categoryValues);
// CREATE THE PRIMARY DATASETPieSeries ls = (PieSeries) PieSeriesImpl.create();
ls.setSeriesIdentifier(
"My Pie Series");ls.setSliceOutline(ColorDefinitionImpl.CREAM());
ls.getLabel().setVisible(
true);seCategory.getDataDefinition().add(QueryImpl.create(
"G"));SeriesDefinition sdX = SeriesDefinitionImpl.create();
sdX.getSeriesPalette().update(0);
SeriesDefinition sdY = SeriesDefinitionImpl.create();
sdY.getSeriesPalette().update(1);
sdY.setQuery(QueryImpl.create(
"Items"));sdX.getSeriesDefinitions().add(sdY);
sdX.getSeries().add(seCategory);
sdY.getSeries().add(ls);
((ChartWithoutAxesImpl)
chart).getSeriesDefinitions().add(sdX);// addSampleData(cwoaPie);
//
//
//
//
//
//
//// TextDataSet categoryValues = TextDataSetImpl
//// .create(new String[]{"A","B","C"});
//
// Series seCategory = SeriesImpl.create();
// // Base Series
//// Query xQ = QueryImpl.create( "Items" );//$NON-NLS-1$
//
//
//
//
// // Apply the color palette
// sdX = SeriesDefinitionImpl.create();
// ((ChartWithoutAxes)chart).getSeriesDefinitions().add(sdX);
//
// seCategory.getDataDefinition( ).add( xQ );
//
//// seCategory.setDataSet(categoryValues);
// seCategory.setSeriesIdentifier("Cities");
//
//
//// sdX.getSeriesPalette( ).shift( 0 );
// sdX.getSeries( ).add( seCategory );
//
//
//// sdX.getSeriesPalette().update(1);
}
/** (non-Javadoc)
*
* @see
* com.ibm.examples.chart.widget.chart.AbstractChartBuilder#buildYSeries()
*/
protected void buildYSeries() {// NumberDataSet orthoValuesDataSet = NumberDataSetImpl.create(dataSet
// .getTechnitians());
// Orthogonal SeriesQuery qY = QueryImpl.create(
"Y" );//$NON-NLS-1$PieSeries sePie = (PieSeries) PieSeriesImpl.create();
sePie.getDataDefinition( ).add( qY );
// sePie.setDataSet(orthoValuesDataSet);
// sePie.setSeriesIdentifier("Amounts");
SeriesDefinition sdY = SeriesDefinitionImpl.create();
sdX.getSeriesDefinitions().add(sdY);sdY.getSeries().add(sePie);
}
}
****
import java.util.HashMap;
import java.util.Map;
import org.eclipse.birt.chart.factory.DataRowExpressionEvaluatorAdapter;
public class DataRowExpressionEvaluator extends
DataRowExpressionEvaluatorAdapter {
private int k = 0;
private Object[] column;
private Map map;
public DataRowExpressionEvaluator(String[] set, Object[][] data) {
if (set == null) {
throw new IllegalArgumentException();
}
map = new HashMap();
for (int i = 0; i < set.length; i++) {
map.put(set[i], data[i]);
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator#evaluate(java
* .lang.String)
*/
public Object evaluate(String _expression_) {
column = (Object[]) map.get(_expression_);
return column[k];
}
/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator#first()
*/
public boolean first() {
k = 0;
if (map.size() > 0) {
column = (Object[]) map.values().iterator().next();
if (column != null && k < column.length) {
return true;
}
}
return false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator#next()
*/
public boolean next() {
if (column != null && k < (column.length - 1)) {
k++;
return true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator#close()
*/
public void close() {
// Doing nothing.
}
}
*****
import
java.awt.Graphics;import
java.awt.Graphics2D;import
java.awt.image.BufferedImage;import
java.io.ByteArrayOutputStream;import
java.io.File;import
java.io.IOException;import
javax.imageio.ImageIO;import
org.eclipse.birt.chart.api.ChartEngine;import
org.eclipse.birt.chart.device.IDeviceRenderer;import
org.eclipse.birt.chart.device.IDisplayServer;import
org.eclipse.birt.chart.exception.ChartException;import
org.eclipse.birt.chart.factory.GeneratedChartState;import
org.eclipse.birt.chart.factory.Generator;import
org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator;import
org.eclipse.birt.chart.factory.IGenerator;import
org.eclipse.birt.chart.factory.RunTimeContext;import
org.eclipse.birt.chart.model.Chart;import
org.eclipse.birt.chart.model.attribute.Bounds;import
org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;import
org.eclipse.birt.core.framework.PlatformConfig;import
com.ibm.icu.util.ULocale;
/**
* Provides the common members and the framework to build one chart. * * @author Qi Liang */public
abstract class AbstractChartBuilder { protected byte[] iceImage; /** * The swing rendering device */ protected IDeviceRenderer dRenderer = null; protected IDisplayServer dServer = null; protected GeneratedChartState gcs = null; protected IGenerator gr = null; /** * Font name for all titles, labels, and values. */ protected final static String FONT_NAME = "MS Sans Serif"; /** * Provides data for chart. */ protected DataSet dataSet = null; /** * Chart instance. */ protected Chart chart = null; /** * Chart title. */ protected String title = null; /** * Constructs one chart builder and associate it to one data set. * * @param dataSet * data set */ public AbstractChartBuilder(DataSet dataSet) { this.dataSet = dataSet;}
/** * Builds one chart. */ public void build() {createChart();
buildPlot();
buildLegend();
buildTitle();
buildXAxis();
buildYAxis();
buildXSeries();
buildYSeries();
}
public byte[] getIceImage() { return iceImage;
}
/** * Creates chart instance. */ protected abstract void createChart(); /** * Builds plot. */ protected void buildPlot() {}
/** * Builds X axis. */ protected void buildXAxis() {}
/** * Builds Y axis. */ protected void buildYAxis() {}
/** * Builds X series. */ protected void buildXSeries() {}
/** * Builds Y series. */ protected void buildYSeries() {}
/** * Builds legend. * */ protected void buildLegend() {}
/** * Builds the chart title. */ protected void buildTitle() { chart.getTitle().getLabel().getCaption().setValue(title); chart.getTitle().getLabel().getCaption().getFont().setSize(14); chart.getTitle().getLabel().getCaption().getFont().setName(FONT_NAME);}
/** * Returns the chart instance. * * @return the chart instance */ public Chart getChart() { return chart;}
}