KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > pdf > ChartModels


1 /***********************************************************************
2  * Copyright (c) 2006 IBM Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ***********************************************************************/

11
12 package org.eclipse.birt.chart.examples.api.pdf;
13
14 import org.eclipse.birt.chart.model.Chart;
15 import org.eclipse.birt.chart.model.ChartWithAxes;
16 import org.eclipse.birt.chart.model.attribute.Anchor;
17 import org.eclipse.birt.chart.model.attribute.AxisType;
18 import org.eclipse.birt.chart.model.attribute.IntersectionType;
19 import org.eclipse.birt.chart.model.attribute.LegendItemType;
20 import org.eclipse.birt.chart.model.attribute.LineAttributes;
21 import org.eclipse.birt.chart.model.attribute.LineStyle;
22 import org.eclipse.birt.chart.model.attribute.Position;
23 import org.eclipse.birt.chart.model.attribute.TickStyle;
24 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
25 import org.eclipse.birt.chart.model.component.Axis;
26 import org.eclipse.birt.chart.model.component.Series;
27 import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
28 import org.eclipse.birt.chart.model.data.NumberDataSet;
29 import org.eclipse.birt.chart.model.data.SeriesDefinition;
30 import org.eclipse.birt.chart.model.data.TextDataSet;
31 import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
32 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
33 import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
34 import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
35 import org.eclipse.birt.chart.model.layout.Legend;
36 import org.eclipse.birt.chart.model.layout.Plot;
37 import org.eclipse.birt.chart.model.type.BarSeries;
38 import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
39
40 public class ChartModels
41 {
42     /**
43      * Creates a simple horizontal stack bar chart model.
44      * @return chart model
45      */

46     protected static final Chart createHSChart( )
47     {
48         ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
49         cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
50
51         Plot p = cwaBar.getPlot( );
52         p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255,
53                 255,
54                 225 ) );
55         cwaBar.getTitle( )
56                 .getLabel( )
57                 .getCaption( )
58                 .setValue( "Simple Bar Chart" ); //$NON-NLS-1$
59
cwaBar.setUnitSpacing( 20 );
60
61         Legend lg = cwaBar.getLegend( );
62         LineAttributes lia = lg.getOutline( );
63         lg.getText( ).getFont( ).setSize( 16 );
64         lia.setStyle( LineStyle.SOLID_LITERAL );
65         lg.getInsets( ).set( 10, 5, 0, 0 );
66         lg.getOutline( ).setVisible( false );
67         lg.setAnchor( Anchor.NORTH_LITERAL );
68         lg.setItemType( LegendItemType.CATEGORIES_LITERAL );
69
70         // X-Axis
71
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
72
73         xAxisPrimary.setType( AxisType.TEXT_LITERAL );
74         xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
75         xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
76         xAxisPrimary.getTitle( ).setVisible( true );
77
78         // Y-Axis
79
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
80         yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
81         yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
82         yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 90 );
83
84         // Data Set
85
TextDataSet categoryValues = TextDataSetImpl.create( new String JavaDoc[]{
86                 "Item 1", "Item 2", "Item 3"} ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
87
NumberDataSet orthoValues = NumberDataSetImpl.create( new double[]{
88                 25, 35, 15
89         } );
90
91         // X-Series
92
Series seCategory = SeriesImpl.create( );
93         seCategory.setDataSet( categoryValues );
94
95         SeriesDefinition sdX = SeriesDefinitionImpl.create( );
96         sdX.getSeriesPalette( ).update( 0 );
97         xAxisPrimary.getSeriesDefinitions( ).add( sdX );
98         sdX.getSeries( ).add( seCategory );
99
100         // Y-Series
101
BarSeries bs = (BarSeries) BarSeriesImpl.create( );
102         bs.setDataSet( orthoValues );
103         bs.setRiserOutline( null );
104         bs.setSeriesIdentifier( "Highlight" ); //$NON-NLS-1$
105
bs.getLabel( ).setVisible( true );
106         bs.setLabelPosition( Position.INSIDE_LITERAL );
107         SeriesDefinition sdY = SeriesDefinitionImpl.create( );
108         yAxisPrimary.getSeriesDefinitions( ).add( sdY );
109         sdY.getSeries( ).add( bs );
110
111         return cwaBar;
112     }
113
114 }
115
Popular Tags