1 11 12 package org.eclipse.birt.chart.examples.api.data; 13 14 import org.eclipse.birt.chart.model.Chart; 15 import org.eclipse.birt.chart.model.ChartWithAxes; 16 import org.eclipse.birt.chart.model.ChartWithoutAxes; 17 import org.eclipse.birt.chart.model.attribute.Anchor; 18 import org.eclipse.birt.chart.model.attribute.AxisType; 19 import org.eclipse.birt.chart.model.attribute.Fill; 20 import org.eclipse.birt.chart.model.attribute.IntersectionType; 21 import org.eclipse.birt.chart.model.attribute.LegendItemType; 22 import org.eclipse.birt.chart.model.attribute.LineAttributes; 23 import org.eclipse.birt.chart.model.attribute.LineStyle; 24 import org.eclipse.birt.chart.model.attribute.Marker; 25 import org.eclipse.birt.chart.model.attribute.MarkerType; 26 import org.eclipse.birt.chart.model.attribute.Position; 27 import org.eclipse.birt.chart.model.attribute.TickStyle; 28 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl; 29 import org.eclipse.birt.chart.model.attribute.impl.GradientImpl; 30 import org.eclipse.birt.chart.model.component.Axis; 31 import org.eclipse.birt.chart.model.component.Series; 32 import org.eclipse.birt.chart.model.component.impl.AxisImpl; 33 import org.eclipse.birt.chart.model.component.impl.SeriesImpl; 34 import org.eclipse.birt.chart.model.data.NumberDataSet; 35 import org.eclipse.birt.chart.model.data.SeriesDefinition; 36 import org.eclipse.birt.chart.model.data.TextDataSet; 37 import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl; 38 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl; 39 import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl; 40 import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl; 41 import org.eclipse.birt.chart.model.impl.ChartWithoutAxesImpl; 42 import org.eclipse.birt.chart.model.layout.Legend; 43 import org.eclipse.birt.chart.model.layout.Plot; 44 import org.eclipse.birt.chart.model.type.BarSeries; 45 import org.eclipse.birt.chart.model.type.LineSeries; 46 import org.eclipse.birt.chart.model.type.PieSeries; 47 import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl; 48 import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl; 49 import org.eclipse.birt.chart.model.type.impl.PieSeriesImpl; 50 51 public class DataCharts 52 { 53 54 protected static final Chart createMinSliceChart( ) 55 { 56 ChartWithoutAxes cwoaPie = ChartWithoutAxesImpl.create( ); 57 cwoaPie.getBlock( ).setBackground( ColorDefinitionImpl.PINK( ) ); 58 59 Plot p = cwoaPie.getPlot( ); 61 p.getClientArea( ).setBackground( ColorDefinitionImpl.PINK( ) ); 62 p.getClientArea( ).getOutline( ).setVisible( false ); 63 p.getOutline( ).setVisible( false ); 64 65 Legend lg = cwoaPie.getLegend( ); 67 lg.setItemType( LegendItemType.CATEGORIES_LITERAL ); 68 lg.getClientArea( ).getOutline( ).setVisible( true ); 69 lg.getTitle( ).setVisible( false ); 70 71 cwoaPie.getTitle( ) 73 .getLabel( ) 74 .getCaption( ) 75 .setValue( "Explosion & Min Slice" ); cwoaPie.getTitle( ).getOutline( ).setVisible( false ); 77 78 TextDataSet categoryValues = TextDataSetImpl.create( new String []{ 80 "New York", "Boston", "Chicago", "San Francisco", "Dallas", "Miami" } ); 82 NumberDataSet seriesOneValues = NumberDataSetImpl.create( new double[]{ 83 24, 9, 30, 36, 8, 51 84 } ); 85 86 SeriesDefinition sd = SeriesDefinitionImpl.create( ); 88 cwoaPie.getSeriesDefinitions( ).add( sd ); 89 90 Series seCategory = (Series) SeriesImpl.create( ); 91 92 final Fill[] fiaBase = { 93 ColorDefinitionImpl.ORANGE( ), 94 GradientImpl.create( ColorDefinitionImpl.create( 225, 225, 255 ), 95 ColorDefinitionImpl.create( 255, 255, 225 ), 96 -35, 97 false ), 98 ColorDefinitionImpl.CREAM( ), 99 ColorDefinitionImpl.RED( ), 100 ColorDefinitionImpl.GREEN( ), 101 ColorDefinitionImpl.BLUE( ).brighter( ), 102 ColorDefinitionImpl.CYAN( ).darker( ), 103 }; 104 sd.getSeriesPalette( ).getEntries( ).clear( ); 105 for ( int i = 0; i < fiaBase.length; i++ ) 106 { 107 sd.getSeriesPalette( ).getEntries( ).add( fiaBase[i] ); 108 } 109 110 seCategory.setDataSet( categoryValues ); 111 sd.getSeries( ).add( seCategory ); 112 113 SeriesDefinition sdCity = SeriesDefinitionImpl.create( ); 115 sd.getSeriesDefinitions( ).add( sdCity ); 116 117 PieSeries sePie = (PieSeries) PieSeriesImpl.create( ); 118 sePie.setDataSet( seriesOneValues ); 119 sePie.setLabelPosition( Position.INSIDE_LITERAL ); 120 sePie.setSeriesIdentifier( "Cities" ); 122 sePie.setExplosion( 30 ); 124 sePie.setExplosionExpression( "valueData<20 ||valueData>50" ); 126 sdCity.getSeries( ).add( sePie ); 127 128 cwoaPie.setMinSlice( 10 ); 130 cwoaPie.setMinSlicePercent( false ); 131 cwoaPie.setMinSliceLabel( "Others" ); 133 return cwoaPie; 134 } 135 136 protected static final Chart createMultiYAxisChart( ) 137 { 138 ChartWithAxes cwaBar = ChartWithAxesImpl.create( ); 139 140 cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) ); 142 Plot p = cwaBar.getPlot( ); 143 p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255, 144 245, 145 255 ) ); 146 147 cwaBar.getTitle( ) 149 .getLabel( ) 150 .getCaption( ) 151 .setValue( "Line Chart with Multiple Y Axis" ); 153 Legend lg = cwaBar.getLegend( ); 155 LineAttributes lia = lg.getOutline( ); 156 lg.getText( ).getFont( ).setSize( 16 ); 157 lia.setStyle( LineStyle.SOLID_LITERAL ); 158 lg.getInsets( ).set( 10, 5, 0, 0 ); 159 lg.getOutline( ).setVisible( false ); 160 lg.setAnchor( Anchor.NORTH_LITERAL ); 161 162 Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0]; 164 xAxisPrimary.setType( AxisType.TEXT_LITERAL ); 165 xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL ); 166 xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL ); 167 xAxisPrimary.getTitle( ).setVisible( false ); 168 169 Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary ); 171 yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL ); 172 yAxisPrimary.getTitle( ) 173 .getCaption( ) 174 .setValue( "Sales Growth ($Million)" ); 176 Axis yAxis = AxisImpl.create( Axis.ORTHOGONAL ); 178 yAxis.setType( AxisType.LINEAR_LITERAL ); 179 yAxis.getMajorGrid( ).setTickStyle( TickStyle.RIGHT_LITERAL ); 180 yAxis.setLabelPosition( Position.RIGHT_LITERAL ); 181 xAxisPrimary.getAssociatedAxes( ).add( yAxis ); 182 183 TextDataSet categoryValues = TextDataSetImpl.create( new String []{ 185 "March", "April", "May", "June", "July"} ); NumberDataSet orthoValues1 = NumberDataSetImpl.create( new double[]{ 187 12.5, 19.6, 18.3, 13.2, 26.5 188 } ); 189 NumberDataSet orthoValues2 = NumberDataSetImpl.create( new double[]{ 190 22.7, 23.6, 38.3, 43.2, 40.5 191 } ); 192 193 Series seCategory = SeriesImpl.create( ); 195 seCategory.setDataSet( categoryValues ); 196 197 SeriesDefinition sdX = SeriesDefinitionImpl.create( ); 198 xAxisPrimary.getSeriesDefinitions( ).add( sdX ); 199 sdX.getSeries( ).add( seCategory ); 200 201 LineSeries ls1 = (LineSeries) LineSeriesImpl.create( ); 203 ls1.setSeriesIdentifier( "A Corp." ); ls1.setDataSet( orthoValues1 ); 205 ls1.getLineAttributes( ).setColor( ColorDefinitionImpl.CREAM( ) ); 206 for ( int i = 0; i < ls1.getMarkers( ).size( ); i++ ) 207 { 208 ( (Marker) ls1.getMarkers( ).get( i ) ).setType( MarkerType.TRIANGLE_LITERAL ); 209 ( (Marker) ls1.getMarkers( ).get( i ) ).setSize( 10 ); 210 } 211 ls1.getLabel( ).setVisible( true ); 212 213 SeriesDefinition sdY1 = SeriesDefinitionImpl.create( ); 214 sdY1.getSeriesPalette( ).update( -2 ); 215 yAxisPrimary.getSeriesDefinitions( ).add( sdY1 ); 216 sdY1.getSeries( ).add( ls1 ); 217 218 LineSeries ls2 = (LineSeries) LineSeriesImpl.create( ); 220 ls2.setSeriesIdentifier( "B Corp." ); ls2.setDataSet( orthoValues2 ); 222 ls2.getLineAttributes( ).setColor( ColorDefinitionImpl.CREAM( ) ); 223 for ( int i = 0; i < ls2.getMarkers( ).size( ); i++ ) 224 { 225 ( (Marker) ls2.getMarkers( ).get( i ) ).setType( MarkerType.CIRCLE_LITERAL ); 226 ( (Marker) ls2.getMarkers( ).get( i ) ).setSize( 10 ); 227 } 228 ls2.getLabel( ).setVisible( true ); 229 230 SeriesDefinition sdY2 = SeriesDefinitionImpl.create( ); 231 sdY2.getSeriesPalette( ).update( -3 ); 232 yAxis.getSeriesDefinitions( ).add( sdY2 ); 233 sdY2.getSeries( ).add( ls2 ); 234 235 return cwaBar; 236 } 237 238 protected static final Chart createMulitYSeriesChart( ) 239 { 240 ChartWithAxes cwaBar = ChartWithAxesImpl.create( ); 241 242 cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) ); 244 Plot p = cwaBar.getPlot( ); 245 p.getClientArea( ) 246 .setBackground( GradientImpl.create( ColorDefinitionImpl.create( 225, 247 225, 248 255 ), 249 ColorDefinitionImpl.create( 255, 255, 225 ), 250 -35, 251 false ) ); 252 p.getOutline( ).setVisible( true ); 253 254 cwaBar.getTitle( ) 256 .getLabel( ) 257 .getCaption( ) 258 .setValue( "Bar Chart with Multiple Y Series" ); 260 Legend lg = cwaBar.getLegend( ); 262 lg.getText( ).getFont( ).setSize( 16 ); 263 lg.getInsets( ).set( 10, 5, 0, 0 ); 264 lg.setAnchor( Anchor.NORTH_LITERAL ); 265 266 Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0]; 268 xAxisPrimary.setType( AxisType.TEXT_LITERAL ); 269 xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL ); 270 xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL ); 271 xAxisPrimary.getTitle( ).getCaption( ).setValue( "Regional Markets" ); xAxisPrimary.setLabelPosition( Position.BELOW_LITERAL ); 273 xAxisPrimary.setTitlePosition( Position.BELOW_LITERAL ); 274 275 Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary ); 277 yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL ); 278 yAxisPrimary.getTitle( ) 279 .getCaption( ) 280 .setValue( "Sales vs. Net Profit ($Million)" ); 282 TextDataSet categoryValues = TextDataSetImpl.create( new String []{ 284 "Europe", "Asia", "North America"} ); NumberDataSet orthoValues1 = NumberDataSetImpl.create( new double[]{ 286 26.17, 34.21, 21.5 287 } ); 288 NumberDataSet orthoValues2 = NumberDataSetImpl.create( new double[]{ 289 4.81, 3.55, -5.26 290 } ); 291 292 Series seCategory = SeriesImpl.create( ); 294 seCategory.setDataSet( categoryValues ); 295 296 SeriesDefinition sdX = SeriesDefinitionImpl.create( ); 297 xAxisPrimary.getSeriesDefinitions( ).add( sdX ); 298 sdX.getSeries( ).add( seCategory ); 299 300 BarSeries bs = (BarSeries) BarSeriesImpl.create( ); 302 bs.setSeriesIdentifier( "Sales" ); bs.setDataSet( orthoValues1 ); 304 bs.setRiserOutline( null ); 305 bs.getLabel( ).setVisible( true ); 306 bs.setLabelPosition( Position.INSIDE_LITERAL ); 307 308 SeriesDefinition sdY1 = SeriesDefinitionImpl.create( ); 309 sdY1.getSeriesPalette( ).update( -2 ); 310 yAxisPrimary.getSeriesDefinitions( ).add( sdY1 ); 311 sdY1.getSeries( ).add( bs ); 312 313 BarSeries bs2 = (BarSeries) BarSeriesImpl.create( ); 315 bs2.setSeriesIdentifier( "Net Profit" ); bs2.setDataSet( orthoValues2 ); 317 bs2.setRiserOutline( null ); 318 bs2.getLabel( ).setVisible( true ); 319 bs2.setLabelPosition( Position.INSIDE_LITERAL ); 320 321 SeriesDefinition sdY2 = SeriesDefinitionImpl.create( ); 322 sdY2.getSeriesPalette( ).update( -3 ); 323 yAxisPrimary.getSeriesDefinitions( ).add( sdY2 ); 324 sdY2.getSeries( ).add( bs2 ); 325 326 return cwaBar; 327 } 328 329 } 330 | Popular Tags |