KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004 Actuate 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  * Actuate Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.birt.chart.examples.api.preference;
12
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.AxisType;
17 import org.eclipse.birt.chart.model.attribute.LegendItemType;
18 import org.eclipse.birt.chart.model.attribute.Position;
19 import org.eclipse.birt.chart.model.attribute.TickStyle;
20 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
21 import org.eclipse.birt.chart.model.component.Axis;
22 import org.eclipse.birt.chart.model.component.Series;
23 import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
24 import org.eclipse.birt.chart.model.data.NumberDataSet;
25 import org.eclipse.birt.chart.model.data.SeriesDefinition;
26 import org.eclipse.birt.chart.model.data.TextDataSet;
27 import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
28 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
29 import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
30 import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
31 import org.eclipse.birt.chart.model.layout.Legend;
32 import org.eclipse.birt.chart.model.layout.Plot;
33 import org.eclipse.birt.chart.model.type.BarSeries;
34 import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
35
36 public class ChartModels
37 {
38
39     /**
40      * Creates a simple bar chart model
41      *
42      * @return An instance of the simulated runtime chart model (containing
43      * filled datasets)
44      */

45     protected static Chart createBarChart( )
46     {
47         ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
48
49         // Plot
50
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
51         cwaBar.getBlock( ).getOutline( ).setVisible( true );
52         Plot p = cwaBar.getPlot( );
53         p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255,
54                 255,
55                 225 ) );
56         p.getOutline( ).setVisible( false );
57
58         // Title
59
cwaBar.getTitle( ).getLabel( ).getCaption( ).setValue( "Bar Chart" ); //$NON-NLS-1$
60

61         // Legend
62
Legend lg = cwaBar.getLegend( );
63         lg.setItemType( LegendItemType.CATEGORIES_LITERAL );
64
65         // X-Axis
66
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
67
68         xAxisPrimary.setType( AxisType.TEXT_LITERAL );
69         xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
70         xAxisPrimary.getTitle( ).setVisible( true );
71
72         // Y-Axis
73
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
74         yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
75         yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
76         yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 90 );
77
78         // Data Set
79
TextDataSet categoryValues = TextDataSetImpl.create( new String JavaDoc[]{
80                 "Item 1", "Item 2", "Item 3"} ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
81
NumberDataSet orthoValues = NumberDataSetImpl.create( new double[]{
82                 25, 35, 15
83         } );
84
85         // X-Series
86
Series seCategory = SeriesImpl.create( );
87         seCategory.setDataSet( categoryValues );
88
89         SeriesDefinition sdX = SeriesDefinitionImpl.create( );
90         sdX.getSeriesPalette( ).update( 0 );
91         xAxisPrimary.getSeriesDefinitions( ).add( sdX );
92         sdX.getSeries( ).add( seCategory );
93
94         // Y-Series
95
BarSeries bs = (BarSeries) BarSeriesImpl.create( );
96         bs.setDataSet( orthoValues );
97         bs.setRiserOutline( null );
98         bs.getLabel( ).setVisible( true );
99         bs.setLabelPosition( Position.INSIDE_LITERAL );
100
101         SeriesDefinition sdY = SeriesDefinitionImpl.create( );
102         yAxisPrimary.getSeriesDefinitions( ).add( sdY );
103         sdY.getSeries( ).add( bs );
104
105         return cwaBar;
106     }
107 }
108
Popular Tags