1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 // __________ Imports __________
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.lang
.*;
41 import com
.sun
.star
.beans
.*;
43 // application specific classes
44 import com
.sun
.star
.chart
.*;
45 import com
.sun
.star
.drawing
.*;
47 import com
.sun
.star
.table
.CellRangeAddress
;
48 import com
.sun
.star
.table
.XCellRange
;
49 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
51 import com
.sun
.star
.frame
.XModel
;
52 import com
.sun
.star
.util
.XNumberFormatsSupplier
;
53 import com
.sun
.star
.util
.XNumberFormats
;
55 // base graphics things
56 import com
.sun
.star
.awt
.Point
;
57 import com
.sun
.star
.awt
.Size
;
58 import com
.sun
.star
.awt
.FontWeight
;
60 import com
.sun
.star
.uno
.Exception
;
61 import com
.sun
.star
.uno
.RuntimeException
;
62 import com
.sun
.star
.beans
.UnknownPropertyException
;
63 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
64 import com
.sun
.star
.util
.MalformedNumberFormatException
;
67 // __________ Implementation __________
69 // Create a spreadsheet add some data and add a chart
71 public class ChartInCalc
75 public static void main( String args
[] )
77 Helper aHelper
= new Helper( args
);
79 CalcHelper aCalcHelper
= new CalcHelper( aHelper
.createSpreadsheetDocument() );
81 // insert a cell range with 4 columns and 24 rows filled with random numbers
82 XCellRange aRange
= aCalcHelper
.insertRandomRange( 4, 24 );
83 CellRangeAddress aRangeAddress
= UnoRuntime
.queryInterface(
84 XCellRangeAddressable
.class, aRange
).getRangeAddress();
86 // change view to sheet containing the chart
87 aCalcHelper
.raiseChartSheet();
89 // the unit for measures is 1/100th of a millimeter
90 // position at (1cm, 1cm)
91 Point aPos
= new Point( 1000, 1000 );
93 // size of the chart is 15cm x 9.271cm
94 Size aExtent
= new Size( 15000, 9271 );
96 // insert a new chart into the "Chart" sheet of the
97 // spreadsheet document
98 XChartDocument aChartDoc
= aCalcHelper
.insertChart(
103 "com.sun.star.chart.XYDiagram" );
105 // instantiate test class with newly created chart
106 ChartInCalc aTest
= new ChartInCalc( aChartDoc
);
110 aTest
.lockControllers();
119 // show an intermediate state, ...
120 aTest
.unlockControllers();
121 aTest
.lockControllers();
123 // ..., because the following takes a while:
124 // an internet URL has to be resolved
125 aTest
.testDataRowProperties();
126 aTest
.testDataPointProperties();
128 aTest
.unlockControllers();
130 catch( Exception ex
)
132 System
.out
.println( "UNO Exception caught: " + ex
);
133 System
.out
.println( "Message: " + ex
.getMessage() );
142 public ChartInCalc( XChartDocument aChartDoc
)
144 maChartDocument
= aChartDoc
;
145 maDiagram
= maChartDocument
.getDiagram();
150 public void lockControllers()
151 throws RuntimeException
153 UnoRuntime
.queryInterface( XModel
.class, maChartDocument
).lockControllers();
158 public void unlockControllers()
159 throws RuntimeException
161 UnoRuntime
.queryInterface( XModel
.class, maChartDocument
).unlockControllers();
166 public void testDiagram()
167 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
168 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
170 XPropertySet aDiaProp
= UnoRuntime
.queryInterface( XPropertySet
.class, maDiagram
);
172 if( aDiaProp
!= null )
175 aDiaProp
.setPropertyValue( "Lines", Boolean
.TRUE
);
177 // change attributes for all series
178 // set line width to 0.5mm
179 aDiaProp
.setPropertyValue( "LineWidth", Integer
.valueOf( 50 ));
185 public void testDataRowProperties()
186 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
187 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
189 // change properties of the data series
192 XPropertySet aSeriesProp
;
193 for( int i
= 1; i
<= 3; i
++ )
195 aSeriesProp
= maDiagram
.getDataRowProperties( i
);
196 aSeriesProp
.setPropertyValue( "LineColor", Integer
.valueOf(
199 0x0000ff - 0x40 * i
));
202 StringBuffer sUrl
= new StringBuffer("file:///");
204 /* for use without net it's easier to load a local graphic */
205 java
.io
.File sourceFile
= new java
.io
.File("bullet.gif");
206 sUrl
.append(sourceFile
.getCanonicalPath().replace('\\', '/'));
207 } catch (java
.io
.IOException e
) {
208 sUrl
= new StringBuffer("http://graphics.openoffice.org/chart/bullet1.gif");
211 // set a bitmap via URL as symbol for the first series
212 aSeriesProp
.setPropertyValue( "SymbolType", Integer
.valueOf( ChartSymbolType
.BITMAPURL
));
213 aSeriesProp
.setPropertyValue( "SymbolBitmapURL", sUrl
.toString() );
217 aSeriesProp
.setPropertyValue( "SymbolType", Integer
.valueOf( ChartSymbolType
.SYMBOL1
));
218 aSeriesProp
.setPropertyValue( "SymbolSize", new Size( 250, 250 ));
222 catch( IndexOutOfBoundsException ex
)
224 System
.out
.println( "Oops, there not enough series for setting properties: " + ex
);
230 public void testDataPointProperties()
231 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
232 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
234 // set properties for a single data point
237 // determine the maximum value of the first series
239 XChartDataArray aDataArray
= UnoRuntime
.queryInterface(
240 XChartDataArray
.class, maChartDocument
.getData());
241 double aData
[][] = aDataArray
.getData();
244 double fMax
= aData
[ 0 ][ 1 ];
245 for( i
= 1; i
< aData
.length
; i
++ )
247 if( aData
[ i
][ 1 ] > fMax
)
249 fMax
= aData
[ i
][ 1 ];
253 // first parameter is the index of the point, the second one is the series
254 XPropertySet aPointProp
= maDiagram
.getDataPointProperties( 0, 1 );
256 // set a different, larger symbol
257 aPointProp
.setPropertyValue( "SymbolType", Integer
.valueOf( ChartSymbolType
.SYMBOL6
));
258 aPointProp
.setPropertyValue( "SymbolSize", new Size( 600, 600 ));
260 // add a label text with bold font, bordeaux red 14pt
261 aPointProp
.setPropertyValue( "DataCaption", Integer
.valueOf( ChartDataCaption
.VALUE
));
262 aPointProp
.setPropertyValue( "CharHeight", new Float( 14.0 ));
263 aPointProp
.setPropertyValue( "CharColor", Integer
.valueOf( 0x993366 ));
264 aPointProp
.setPropertyValue( "CharWeight", new Float( FontWeight
.BOLD
));
266 catch( IndexOutOfBoundsException ex
)
268 System
.out
.println( "Oops, there not enough data points or series for setting properties: " + ex
);
274 public void testArea()
275 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
276 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
278 XPropertySet aArea
= maChartDocument
.getArea();
282 // change background color of entire chart
283 aArea
.setPropertyValue( "FillStyle", FillStyle
.SOLID
);
284 aArea
.setPropertyValue( "FillColor", Integer
.valueOf( 0xeeeeee ));
290 public void testWall()
291 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
292 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
294 XPropertySet aWall
= UnoRuntime
.queryInterface(
295 X3DDisplay
.class, maDiagram
).getWall();
297 // change background color of area
298 aWall
.setPropertyValue( "FillStyle", FillStyle
.SOLID
);
299 aWall
.setPropertyValue( "FillColor", Integer
.valueOf( 0xcccccc ));
304 public void testTitle()
305 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
306 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
309 XPropertySet aDocProp
= UnoRuntime
.queryInterface(
310 XPropertySet
.class, maChartDocument
);
311 aDocProp
.setPropertyValue( "HasMainTitle", Boolean
.TRUE
);
313 XShape aTitle
= maChartDocument
.getTitle();
314 XPropertySet aTitleProp
= UnoRuntime
.queryInterface( XPropertySet
.class, aTitle
);
317 if( aTitleProp
!= null )
319 aTitleProp
.setPropertyValue( "String", "Random Scatter Chart" );
320 aTitleProp
.setPropertyValue( "CharHeight", new Float(14.0) );
323 // align title with y axis
324 XShape aAxis
= UnoRuntime
.queryInterface(
325 XShape
.class, UnoRuntime
.queryInterface(
326 XAxisYSupplier
.class, maDiagram
).getYAxis() );
331 Point aPos
= aTitle
.getPosition();
332 aPos
.X
= ( aAxis
.getPosition() ).X
;
333 aTitle
.setPosition( aPos
);
339 public void testAxis()
340 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
341 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
,
342 MalformedNumberFormatException
345 XPropertySet aAxisProp
= UnoRuntime
.queryInterface(
346 XAxisXSupplier
.class, maDiagram
).getXAxis();
347 if( aAxisProp
!= null )
349 aAxisProp
.setPropertyValue( "Max", Integer
.valueOf( 24 ));
350 aAxisProp
.setPropertyValue( "StepMain", Integer
.valueOf( 3 ));
353 // change number format for y axis
354 aAxisProp
= UnoRuntime
.queryInterface(
355 XAxisYSupplier
.class, maDiagram
).getYAxis();
357 // add a new custom number format and get the new key
358 int nNewNumberFormat
= 0;
359 XNumberFormatsSupplier aNumFmtSupp
= UnoRuntime
.queryInterface(
360 XNumberFormatsSupplier
.class, maChartDocument
);
362 if( aNumFmtSupp
!= null )
364 XNumberFormats aFormats
= aNumFmtSupp
.getNumberFormats();
365 Locale aLocale
= new Locale( "de", "DE", "de" );
367 String aFormatStr
= aFormats
.generateFormat( nNewNumberFormat
, aLocale
, true, true, (short)3, (short)1 );
368 nNewNumberFormat
= aFormats
.addNew( aFormatStr
, aLocale
);
371 if( aAxisProp
!= null )
373 aAxisProp
.setPropertyValue( "NumberFormat", Integer
.valueOf( nNewNumberFormat
));
379 public void testGrid()
380 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
381 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
384 XPropertySet aGridProp
= UnoRuntime
.queryInterface(
386 UnoRuntime
.queryInterface(
387 XAxisYSupplier
.class, maDiagram
).getYMainGrid());
389 if( aGridProp
!= null )
391 LineDash aDash
= new LineDash();
392 aDash
.Style
= DashStyle
.ROUND
;
397 aDash
.Distance
= 100;
399 aGridProp
.setPropertyValue( "LineColor", Integer
.valueOf( 0x999999 ));
400 aGridProp
.setPropertyValue( "LineStyle", LineStyle
.DASH
);
401 aGridProp
.setPropertyValue( "LineDash", aDash
);
402 aGridProp
.setPropertyValue( "LineWidth", Integer
.valueOf( 30 ));
412 private final XChartDocument maChartDocument
;
413 private final XDiagram maDiagram
;