Home » Archived » BIRT » chart engine API questions
chart engine API questions [message #685953] |
Mon, 20 June 2011 22:50  |
Eclipse User |
|
|
|
Use chart engine API in web application,
1. how to show percentage besides number in pie chart?
2. how to add interactivity in chart for web application? for example,
show tooltip,
drill down sales for country, state, city, etc.
Thanks for any help.
Dave
|
|
| |
Re: chart engine API questions [message #686055 is a reply to message #685969] |
Tue, 21 June 2011 10:24  |
Eclipse User |
|
|
|
For interactivity you need to have the UPDATE_NOTIFIER property set and you have to get the image map
for the chart and embed in your output,
String im = ((IImageMapEmitter) dRenderer).getImageMap();
Take a look at the attached example. It also shows how to do pie chart percentage.
Jason
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import org.eclipse.birt.chart.api.ChartEngine;
import org.eclipse.birt.chart.device.EmptyUpdateNotifier;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.device.IDisplayServer;
import org.eclipse.birt.chart.device.IImageMapEmitter;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.IGenerator;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.attribute.ActionValue;
import org.eclipse.birt.chart.model.attribute.Anchor;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.Bounds;
import org.eclipse.birt.chart.model.attribute.ChartDimension;
import org.eclipse.birt.chart.model.attribute.DataPointComponent;
import org.eclipse.birt.chart.model.attribute.DataPointComponentType;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LeaderLineStyle;
import org.eclipse.birt.chart.model.attribute.LegendItemType;
import org.eclipse.birt.chart.model.attribute.LineStyle;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.TriggerCondition;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.DataPointComponentImpl;
import org.eclipse.birt.chart.model.attribute.impl.GradientImpl;
import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl;
import org.eclipse.birt.chart.model.attribute.impl.LineAttributesImpl;
import org.eclipse.birt.chart.model.attribute.impl.TooltipValueImpl;
import org.eclipse.birt.chart.model.attribute.impl.URLValueImpl;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.Action;
import org.eclipse.birt.chart.model.data.BaseSampleData;
import org.eclipse.birt.chart.model.data.DataFactory;
import org.eclipse.birt.chart.model.data.NullDataSet;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
import org.eclipse.birt.chart.model.data.SampleData;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.TextDataSet;
import org.eclipse.birt.chart.model.data.Trigger;
import org.eclipse.birt.chart.model.data.impl.ActionImpl;
import org.eclipse.birt.chart.model.data.impl.NullDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
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.BarSeries;
import org.eclipse.birt.chart.model.type.PieSeries;
import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
import org.eclipse.birt.chart.model.type.impl.PieSeriesImpl;
import org.eclipse.birt.core.framework.PlatformConfig;
/**
* Test decription:
* </p>
* Chart script: BeforeDrawLegendItem()
* </p>
*/
public class StandaloneChart {
private static String OUTPUT = "output/Standalone.png"; //$NON-NLS-1$
private static String OUTPUT_HTML = "output/Standalone.html"; //$NON-NLS-1$
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
/**
* A chart model instance
*/
private Chart cm = null;
/**
* The swing rendering device
*/
private IDeviceRenderer dRenderer = null;
private IDisplayServer dServer = null;
private GeneratedChartState gcs = null;
/**
* execute application
*
* @param args
*/
public static void main(String[] args) {
new StandaloneChart();
System.out.println("Finished");
}
/**
* Constructor
*/
public StandaloneChart() {
PlatformConfig pf = new PlatformConfig();
pf.setProperty("STANDALONE", true);
//Returns a singleton instance of the Chart Engine
ChartEngine ce = ChartEngine.instance(pf);
//Returns a singleton instance of the Generator
IGenerator 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();
}
//cm = StandaloneChart.createStackedBar();
cm = StandaloneChart.showTooltip_PieChart();
BufferedImage img = new BufferedImage(600, 600,
BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
Graphics2D g2d = (Graphics2D) g;
//Look at IDeviceRenderer.java for all properties
//like DPI_RESOLUTION
//FILE_IDENTIFIER
//FORMAT_IDENTIFIER
//UPDATE_NOTIFIER
dRenderer.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT, g2d);
dRenderer.setProperty(IDeviceRenderer.FILE_IDENTIFIER, OUTPUT); //$NON-NLS-1$
//Set the bounds for the entire chart
Bounds bo = BoundsImpl.create(0, 0, 600, 600);
bo.scale(72d / dRenderer.getDisplayServer().getDpiResolution());
try {
gcs = gr.build(dServer, cm, bo, null, null, null);
//gcs.getRunTimeContext().setActionRenderer( new MyActionRenderer());
//have to have this line if you want the image map generated
dRenderer.setProperty(IDeviceRenderer.UPDATE_NOTIFIER,
new EmptyUpdateNotifier(cm, gcs.getChartModel()));
gr.render(dRenderer, gcs);
String im = ((IImageMapEmitter) dRenderer).getImageMap();
BufferedWriter out = new BufferedWriter(new FileWriter(OUTPUT_HTML));
out.write("<html>");
out.newLine();
out.write("<body>");
out.newLine();
out.write("<div>");
out.newLine();
if( im != null){
out.write("<map name='testmap'>");
out.write(im);
out.write("</map>");
out.newLine();
}
out
.write("<img id=myimage src='standalone.png' usemap='#testmap'></img>");
out.newLine();
out.write("</div>");
out.newLine();
out.write("</body>");
out.newLine();
out.write("</html>");
out.newLine();
out.close();
System.out.println(im);
} catch (Exception e) {
e.printStackTrace();
}
}
public static final Chart createStackedBar()
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create();
cwaBar.setType("Bar Chart"); //$NON-NLS-1$
cwaBar.setSubType("Stacked"); //$NON-NLS-1$
// Plot
cwaBar.getBlock().setBackground(ColorDefinitionImpl.WHITE());
cwaBar.getBlock().getOutline().setVisible(true);
Plot p = cwaBar.getPlot();
p.getClientArea().setBackground(ColorDefinitionImpl.create(255,
255,
225));
// Title
cwaBar.getTitle()
.getLabel()
.getCaption()
.setValue("Stacked Bar Chart"); //$NON-NLS-1$
// Legend
Legend lg = cwaBar.getLegend();
lg.setItemType(LegendItemType.CATEGORIES_LITERAL);
//Add Script
cwaBar
.setScript("function beforeGeneration( cm, icsc )"
+ "{importPackage(Packages.org.eclipse.birt.chart.model.attribute); "
+ " importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl); "
+ " cm.getLegend().getOutline( ).setStyle( LineStyle.DASH_DOTTED_LITERAL );"
+ " cm.getLegend().getOutline( ).setColor( ColorDefinitionImpl.GREEN() );"
+ " cm.getLegend().getOutline( ).setVisible( true );} ");
// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes()[0];
xAxisPrimary.setType(AxisType.LINEAR_LITERAL);
xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
xAxisPrimary.getOrigin().setType(IntersectionType.MIN_LITERAL);
xAxisPrimary.getLabel().getCaption().getFont().setRotation(90);
// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis(xAxisPrimary);
yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
yAxisPrimary.setType(AxisType.LINEAR_LITERAL);
yAxisPrimary.setLabelPosition(Position.RIGHT_LITERAL);
// Data Set
TextDataSet categoryValues = TextDataSetImpl.create(new String[] {
"Item 1a", "Item 2", "Item 3", "Item 4", "Item 5" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
NumberDataSet orthoValues1 = NumberDataSetImpl.create(new double[] {
25, 35, 15, 5, 20
});
NumberDataSet orthoValues2 = NumberDataSetImpl.create(new double[] {
5, 10, 25, 10, 5
});
NullDataSet ortho = NullDataSetImpl.create(5);
SampleData sd = DataFactory.eINSTANCE.createSampleData();
BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData();
sdBase.setDataSetRepresentation("");//$NON-NLS-1$
sd.getBaseSampleData().add(sdBase);
OrthogonalSampleData sdOrthogonal1 = DataFactory.eINSTANCE
.createOrthogonalSampleData();
sdOrthogonal1.setDataSetRepresentation("");//$NON-NLS-1$
sdOrthogonal1.setSeriesDefinitionIndex(0);
sd.getOrthogonalSampleData().add(sdOrthogonal1);
OrthogonalSampleData sdOrthogonal2 = DataFactory.eINSTANCE
.createOrthogonalSampleData();
sdOrthogonal2.setDataSetRepresentation("");//$NON-NLS-1$
sdOrthogonal2.setSeriesDefinitionIndex(1);
sd.getOrthogonalSampleData().add(sdOrthogonal2);
cwaBar.setSampleData(sd);
// X-Series
Series seCategory = SeriesImpl.create();
seCategory.setDataSet(orthoValues2);
SeriesDefinition sdX = SeriesDefinitionImpl.create();
xAxisPrimary.getSeriesDefinitions().add(sdX);
sdX.getSeries().add(seCategory);
sdX.getSeriesPalette().shift(0);
// Y-Series
BarSeries bs1 = (BarSeries) BarSeriesImpl.create();
bs1.setDataSet(orthoValues1);
//bs1.setDataSet(ortho);
bs1.setStacked(true);
bs1.getLabel().setVisible(true);
bs1.setLabelPosition(Position.INSIDE_LITERAL);
bs1.setRiserOutline(ColorDefinitionImpl.TRANSPARENT());
Trigger tr1 = TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(200, null)));
Trigger tr2 = TriggerImpl.create(TriggerCondition.ONCLICK_LITERAL,
ActionImpl.create(ActionType.URL_REDIRECT_LITERAL, URLValueImpl
.create("https://www.google.com", null, "component",
"value", "")));
bs1.getTriggers().add(tr1);
bs1.getTriggers().add(tr2);
SeriesDefinition sdY = SeriesDefinitionImpl.create();
sdY.getSeriesPalette().shift(0);
yAxisPrimary.getSeriesDefinitions().add(sdY);
sdY.getSeries().add(bs1);
return cwaBar;
}
public static final Chart showTooltip_PieChart( )
{
ChartWithoutAxesImpl cwoaPie = (ChartWithoutAxesImpl)ChartWithoutAxesImpl.create( );
// Chart Type
cwoaPie.setType( "Pie Chart" );
cwoaPie
.setDimension( ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL );
// Title
cwoaPie.getTitle( ).getLabel( ).getCaption( ).setValue(
"Sample Pie Chart" );
cwoaPie.getBlock( ).setBounds( BoundsImpl.create( 0, 0, 252, 288 ) );
cwoaPie.getBlock( ).getOutline( ).setVisible( true );
// Plot
cwoaPie.getPlot( ).getClientArea( ).getOutline( ).setVisible( false );
cwoaPie.getPlot( ).getClientArea( ).setBackground(
ColorDefinitionImpl.create( 255, 255, 225 ) );
// Legend
Legend lg = cwoaPie.getLegend( );
lg.getText( ).getFont( ).setSize( 16 );
lg.getInsets( ).set( 10, 5, 0, 0 );
lg.getOutline( ).setStyle( LineStyle.DASH_DOTTED_LITERAL );
lg.getOutline( ).setColor( ColorDefinitionImpl.create( 214, 100, 12 ) );
lg.getOutline( ).setVisible( true );
lg
.setBackground( GradientImpl.create( ColorDefinitionImpl
.create( 225, 225, 255 ), ColorDefinitionImpl.create(
255,
255,
225 ), -35, false ) );
lg.setAnchor( Anchor.EAST_LITERAL );
lg.setItemType( LegendItemType.CATEGORIES_LITERAL );
lg.getClientArea( ).setBackground( ColorDefinitionImpl.ORANGE( ) );
lg.setPosition( Position.LEFT_LITERAL );
//lg.setOrientation( Orientation.VERTICAL_LITERAL );
// Data Set
TextDataSet dsStringValue = TextDataSetImpl.create( new String[]{
"Keyboards", "Moritors", "Printers", "Mortherboards"} );
NumberDataSet dsNumericValues1 = NumberDataSetImpl
.create( new double[]{143.26, 156.55, 95.25, 47.56} );
// Series
Series seCategory = SeriesImpl.create( );
seCategory.setDataSet( dsStringValue );
SeriesDefinition series = SeriesDefinitionImpl.create( );
cwoaPie.getSeriesDefinitions( ).add( series );
series.getSeries( ).add( seCategory );
PieSeries ps = (PieSeries) PieSeriesImpl.create( );
ps.getLabel( ).getCaption( ).setColor( ColorDefinitionImpl.RED( ) );
ps.getLabel( ).setBackground( ColorDefinitionImpl.CYAN( ) );
ps.getLabel( ).setVisible( true );
ps.setSeriesIdentifier( "Actuate" );
ps.setDataSet( dsNumericValues1 );
ps.setLeaderLineAttributes( LineAttributesImpl.create(
ColorDefinitionImpl.create( 239, 33, 3 ),
LineStyle.DASH_DOTTED_LITERAL,
3 ) );
ps.setLeaderLineStyle( LeaderLineStyle.FIXED_LENGTH_LITERAL );
ps.setExplosion( 0 );
ps.setSliceOutline( ColorDefinitionImpl.BLACK( ) );
ActionValue actionValue = TooltipValueImpl.create(0, null);
Action action = ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL, actionValue);
Trigger mouseOver = TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,action);
ps.getTriggers().add(mouseOver);
SeriesDefinition seGroup1 = SeriesDefinitionImpl.create( );
//series.getSeriesPalette( ).update( -2 );
series.getSeriesDefinitions( ).add( seGroup1 );
seGroup1.getSeries( ).add( ps );
DataPointComponent dpc = DataPointComponentImpl.create( DataPointComponentType.PERCENTILE_ORTHOGONAL_VALUE_LITERAL,
JavaNumberFormatSpecifierImpl.create( "##.##%" )); //$NON-NLS-1$
ps.getDataPoint( ).getComponents( ).clear( );
ps.getDataPoint( ).getComponents( ).add( dpc );
return cwoaPie;
}
}
|
|
|
Goto Forum:
Current Time: Wed Jul 23 19:19:03 EDT 2025
Powered by FUDForum. Page generated in 0.44169 seconds
|