Page perso [Jmini]
>
Eclipse BIRT
>
Charting Engine (ECE)
>
BIRT Charts Gallery
> BIRT Chart Legend Format
Legend Format
code
raw source code
/***********************************************************************
* Copyright (c) 2004, 2005 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation
***********************************************************************/
package org.eclipse.birt.chart.examples.view.models;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.Anchor;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
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.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.GradientImpl;
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.BaseSampleData;
import org.eclipse.birt.chart.model.data.DataFactory;
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.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.impl.ChartWithAxesImpl;
import org.eclipse.birt.chart.model.layout.Legend;
import org.eclipse.birt.chart.model.layout.TitleBlock;
import org.eclipse.birt.chart.model.type.BarSeries;
import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
public class LegendFormat
{
public static final Chart createLegendFormat( )
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
cwaBar.setType( "Bar Chart" ); //$NON-NLS-1$
cwaBar.setSubType( "Side-by-side" ); //$NON-NLS-1$
// Plot
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
cwaBar.getBlock( ).getOutline( ).setVisible( true );
// Title
TitleBlock tb = cwaBar.getTitle( );
tb.setBackground( GradientImpl.create( ColorDefinitionImpl.create( 200,
200,
244 ), ColorDefinitionImpl.create( 250, 122, 253 ), 0, false ) );
tb.getOutline( ).setStyle( LineStyle.DASHED_LITERAL );
tb.getOutline( ).setColor( ColorDefinitionImpl.create( 0, 100, 245 ) );
tb.getOutline( ).setThickness( 1 );
tb.getOutline( ).setVisible( true );
tb.getLabel( )
.getCaption( )
.setValue( "Formatted Legend and Title Chart" ); //$NON-NLS-1$
tb.getLabel( ).getCaption( ).setColor( ColorDefinitionImpl.GREEN( ) );
tb.getLabel( ).setShadowColor( ColorDefinitionImpl.YELLOW( ) );
tb.getLabel( ).getOutline( ).setVisible( true );
// Legend
Legend lg = cwaBar.getLegend( );
lg.getText( ).getFont( ).setSize( 10 );
lg.getText( ).getFont( ).setBold( true );
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.NORTH_LITERAL );
lg.setItemType( LegendItemType.CATEGORIES_LITERAL );
lg.getClientArea( ).setBackground( ColorDefinitionImpl.ORANGE( ) );
lg.setPosition( Position.LEFT_LITERAL );
// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
xAxisPrimary.setType( AxisType.TEXT_LITERAL );
xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
xAxisPrimary.getOrigin( ).setType( IntersectionType.MIN_LITERAL );
// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 90 );
// Data Set
TextDataSet categoryValues = TextDataSetImpl.create( new String[]{
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"} );//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
NumberDataSet orthoValues = NumberDataSetImpl.create( new double[]{
14.3, 20.9, 7.6, -2.0, 9.5
} );
SampleData sd = DataFactory.eINSTANCE.createSampleData( );
BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( );
sdBase.setDataSetRepresentation( "" );//$NON-NLS-1$
sd.getBaseSampleData( ).add( sdBase );
OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE.createOrthogonalSampleData( );
sdOrthogonal.setDataSetRepresentation( "" );//$NON-NLS-1$
sdOrthogonal.setSeriesDefinitionIndex( 0 );
sd.getOrthogonalSampleData( ).add( sdOrthogonal );
cwaBar.setSampleData( sd );
// X-Series
Series seCategory = SeriesImpl.create( );
seCategory.setDataSet( categoryValues );
SeriesDefinition sdX = SeriesDefinitionImpl.create( );
sdX.getSeriesPalette( ).shift( -4 );
xAxisPrimary.getSeriesDefinitions( ).add( sdX );
sdX.getSeries( ).add( seCategory );
// Y-Series
BarSeries bs = (BarSeries) BarSeriesImpl.create( );
bs.setDataSet( orthoValues );
bs.getLabel( ).setVisible( true );
bs.setTranslucent( true );
bs.setLabelPosition( Position.INSIDE_LITERAL );
SeriesDefinition sdY = SeriesDefinitionImpl.create( );
sdY.getSeriesPalette( ).shift( -1 );
yAxisPrimary.getSeriesDefinitions( ).add( sdY );
sdY.getSeries( ).add( bs );
return cwaBar;
}
}