1 /*************************************************************************
3 * $RCSfile: ChartInCalc.java,v $
7 * last change: $Author: hr $ $Date: 2007-07-31 13:53:35 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
44 import com
.sun
.star
.uno
.XInterface
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.lang
.*;
49 import com
.sun
.star
.beans
.*;
51 // application specific classes
52 import com
.sun
.star
.chart
.*;
53 import com
.sun
.star
.drawing
.*;
55 import com
.sun
.star
.table
.CellRangeAddress
;
56 import com
.sun
.star
.table
.XCellRange
;
57 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
59 import com
.sun
.star
.frame
.XModel
;
60 import com
.sun
.star
.frame
.XController
;
62 import com
.sun
.star
.util
.XNumberFormatsSupplier
;
63 import com
.sun
.star
.util
.XNumberFormats
;
65 // base graphics things
66 import com
.sun
.star
.awt
.Point
;
67 import com
.sun
.star
.awt
.Size
;
68 import com
.sun
.star
.awt
.Rectangle
;
69 import com
.sun
.star
.awt
.FontWeight
;
70 import com
.sun
.star
.awt
.FontRelief
;
73 import com
.sun
.star
.uno
.Exception
;
74 import com
.sun
.star
.uno
.RuntimeException
;
75 import com
.sun
.star
.beans
.UnknownPropertyException
;
76 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
77 import com
.sun
.star
.util
.MalformedNumberFormatException
;
80 // __________ Implementation __________
82 /** Create a spreadsheet add some data and add a chart
83 @author Björn Milcke
85 public class ChartInCalc
87 // ____________________
89 public static void main( String args
[] )
91 Helper aHelper
= new Helper( args
);
93 CalcHelper aCalcHelper
= new CalcHelper( aHelper
.createSpreadsheetDocument() );
95 // insert a cell range with 4 columns and 24 rows filled with random numbers
96 XCellRange aRange
= aCalcHelper
.insertRandomRange( 4, 24 );
97 CellRangeAddress aRangeAddress
= ((XCellRangeAddressable
) UnoRuntime
.queryInterface(
98 XCellRangeAddressable
.class, aRange
)).getRangeAddress();
100 // change view to sheet containing the chart
101 aCalcHelper
.raiseChartSheet();
103 // the unit for measures is 1/100th of a millimeter
104 // position at (1cm, 1cm)
105 Point aPos
= new Point( 1000, 1000 );
107 // size of the chart is 15cm x 9.271cm
108 Size aExtent
= new Size( 15000, 9271 );
110 // insert a new chart into the "Chart" sheet of the
111 // spreadsheet document
112 XChartDocument aChartDoc
= aCalcHelper
.insertChart(
117 "com.sun.star.chart.XYDiagram" );
119 // instantiate test class with newly created chart
120 ChartInCalc aTest
= new ChartInCalc( aChartDoc
);
124 aTest
.lockControllers();
133 // show an intermediate state, ...
134 aTest
.unlockControllers();
135 aTest
.lockControllers();
137 // ..., because the following takes a while:
138 // an internet URL has to be resolved
139 aTest
.testDataRowProperties();
140 aTest
.testDataPointProperties();
142 aTest
.unlockControllers();
144 catch( Exception ex
)
146 System
.out
.println( "UNO Exception caught: " + ex
);
147 System
.out
.println( "Message: " + ex
.getMessage() );
154 // ________________________________________
156 public ChartInCalc( XChartDocument aChartDoc
)
158 maChartDocument
= aChartDoc
;
159 maDiagram
= maChartDocument
.getDiagram();
162 // ____________________
164 public void lockControllers()
165 throws RuntimeException
167 ((XModel
) UnoRuntime
.queryInterface( XModel
.class, maChartDocument
)).lockControllers();
170 // ____________________
172 public void unlockControllers()
173 throws RuntimeException
175 ((XModel
) UnoRuntime
.queryInterface( XModel
.class, maChartDocument
)).unlockControllers();
178 // ____________________
180 public void testDiagram()
181 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
182 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
184 XPropertySet aDiaProp
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, maDiagram
);
186 if( aDiaProp
!= null )
189 aDiaProp
.setPropertyValue( "Lines", new Boolean( true ));
191 // change attributes for all series
192 // set line width to 0.5mm
193 aDiaProp
.setPropertyValue( "LineWidth", new Integer( 50 ));
197 // ____________________
199 public void testDataRowProperties()
200 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
201 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
203 // change properties of the data series
206 XPropertySet aSeriesProp
;
207 for( int i
= 1; i
<= 3; i
++ )
209 aSeriesProp
= maDiagram
.getDataRowProperties( i
);
210 aSeriesProp
.setPropertyValue( "LineColor", new Integer(
213 0x0000ff - 0x40 * i
));
216 StringBuffer sUrl
= new StringBuffer("file:///");
218 /* for use without net it's easier to load a local graphic */
219 java
.io
.File sourceFile
= new java
.io
.File("bullet.gif");
220 sUrl
.append(sourceFile
.getCanonicalPath().replace('\\', '/'));
221 } catch (java
.io
.IOException e
) {
222 sUrl
= new StringBuffer("http://graphics.openoffice.org/chart/bullet1.gif");
225 // set a bitmap via URL as symbol for the first series
226 aSeriesProp
.setPropertyValue( "SymbolType", new Integer( ChartSymbolType
.BITMAPURL
));
227 aSeriesProp
.setPropertyValue( "SymbolBitmapURL", sUrl
.toString() );
231 aSeriesProp
.setPropertyValue( "SymbolType", new Integer( ChartSymbolType
.SYMBOL1
));
232 aSeriesProp
.setPropertyValue( "SymbolSize", new Size( 250, 250 ));
236 catch( IndexOutOfBoundsException ex
)
238 System
.out
.println( "Oops, there not enough series for setting properties: " + ex
);
242 // ____________________
244 public void testDataPointProperties()
245 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
246 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
248 // set properties for a single data point
251 // determine the maximum value of the first series
254 XChartDataArray aDataArray
= (XChartDataArray
) UnoRuntime
.queryInterface(
255 XChartDataArray
.class, maChartDocument
.getData());
256 double aData
[][] = aDataArray
.getData();
259 double fMax
= aData
[ 0 ][ 1 ];
260 for( i
= 1; i
< aData
.length
; i
++ )
262 if( aData
[ i
][ 1 ] > fMax
)
264 fMax
= aData
[ i
][ 1 ];
269 // first parameter is the index of the point, the second one is the series
270 XPropertySet aPointProp
= maDiagram
.getDataPointProperties( 0, 1 );
272 // set a different, larger symbol
273 aPointProp
.setPropertyValue( "SymbolType", new Integer( ChartSymbolType
.SYMBOL6
));
274 aPointProp
.setPropertyValue( "SymbolSize", new Size( 600, 600 ));
276 // add a label text with bold font, bordeaux red 14pt
277 aPointProp
.setPropertyValue( "DataCaption", new Integer( ChartDataCaption
.VALUE
));
278 aPointProp
.setPropertyValue( "CharHeight", new Float( 14.0 ));
279 aPointProp
.setPropertyValue( "CharColor", new Integer( 0x993366 ));
280 aPointProp
.setPropertyValue( "CharWeight", new Float( FontWeight
.BOLD
));
282 catch( IndexOutOfBoundsException ex
)
284 System
.out
.println( "Oops, there not enough data points or series for setting properties: " + ex
);
288 // ____________________
290 public void testArea()
291 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
292 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
294 XPropertySet aArea
= maChartDocument
.getArea();
298 // change background color of entire chart
299 aArea
.setPropertyValue( "FillStyle", FillStyle
.SOLID
);
300 aArea
.setPropertyValue( "FillColor", new Integer( 0xeeeeee ));
304 // ____________________
306 public void testWall()
307 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
308 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
310 XPropertySet aWall
= ((X3DDisplay
) UnoRuntime
.queryInterface(
311 X3DDisplay
.class, maDiagram
)).getWall();
313 // change background color of area
314 aWall
.setPropertyValue( "FillStyle", FillStyle
.SOLID
);
315 aWall
.setPropertyValue( "FillColor", new Integer( 0xcccccc ));
318 // ____________________
320 public void testTitle()
321 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
322 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
325 XPropertySet aDocProp
= (XPropertySet
) UnoRuntime
.queryInterface(
326 XPropertySet
.class, maChartDocument
);
327 aDocProp
.setPropertyValue( "HasMainTitle", new Boolean( true ));
329 XShape aTitle
= maChartDocument
.getTitle();
330 XPropertySet aTitleProp
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, aTitle
);
333 if( aTitleProp
!= null )
335 aTitleProp
.setPropertyValue( "String", "Random Scatter Chart" );
336 aTitleProp
.setPropertyValue( "CharHeight", new Float(14.0) );
339 // align title with y axis
340 XShape aAxis
= (XShape
) UnoRuntime
.queryInterface(
341 XShape
.class, ((XAxisYSupplier
) UnoRuntime
.queryInterface(
342 XAxisYSupplier
.class, maDiagram
)).getYAxis() );
347 Point aPos
= aTitle
.getPosition();
348 aPos
.X
= ( aAxis
.getPosition() ).X
;
349 aTitle
.setPosition( aPos
);
353 // ____________________
355 public void testAxis()
356 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
357 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
,
358 MalformedNumberFormatException
361 XPropertySet aAxisProp
= ((XAxisXSupplier
) UnoRuntime
.queryInterface(
362 XAxisXSupplier
.class, maDiagram
)).getXAxis();
363 if( aAxisProp
!= null )
365 aAxisProp
.setPropertyValue( "Max", new Integer( 24 ));
366 aAxisProp
.setPropertyValue( "StepMain", new Integer( 3 ));
369 // change number format for y axis
370 aAxisProp
= ((XAxisYSupplier
) UnoRuntime
.queryInterface(
371 XAxisYSupplier
.class, maDiagram
)).getYAxis();
373 // add a new custom number format and get the new key
374 int nNewNumberFormat
= 0;
375 XNumberFormatsSupplier aNumFmtSupp
= (XNumberFormatsSupplier
) UnoRuntime
.queryInterface(
376 XNumberFormatsSupplier
.class, maChartDocument
);
378 if( aNumFmtSupp
!= null )
380 XNumberFormats aFormats
= aNumFmtSupp
.getNumberFormats();
381 Locale aLocale
= new Locale( "de", "DE", "de" );
383 String aFormatStr
= aFormats
.generateFormat( nNewNumberFormat
, aLocale
, true, true, (short)3, (short)1 );
384 nNewNumberFormat
= aFormats
.addNew( aFormatStr
, aLocale
);
387 if( aAxisProp
!= null )
389 aAxisProp
.setPropertyValue( "NumberFormat", new Integer( nNewNumberFormat
));
393 // ____________________
395 public void testGrid()
396 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
397 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
400 XPropertySet aGridProp
= (XPropertySet
) UnoRuntime
.queryInterface(
402 ( (XAxisYSupplier
) UnoRuntime
.queryInterface(
403 XAxisYSupplier
.class, maDiagram
)).getYMainGrid());
405 if( aGridProp
!= null )
407 LineDash aDash
= new LineDash();
408 aDash
.Style
= DashStyle
.ROUND
;
413 aDash
.Distance
= 100;
415 aGridProp
.setPropertyValue( "LineColor", new Integer( 0x999999 ));
416 aGridProp
.setPropertyValue( "LineStyle", LineStyle
.DASH
);
417 aGridProp
.setPropertyValue( "LineDash", aDash
);
418 aGridProp
.setPropertyValue( "LineWidth", new Integer( 30 ));
423 // ______________________________
426 // ______________________________
428 private XChartDocument maChartDocument
;
429 private XDiagram maDiagram
;