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
73 // ____________________
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() );
140 // ________________________________________
142 public ChartInCalc( XChartDocument aChartDoc
)
144 maChartDocument
= aChartDoc
;
145 maDiagram
= maChartDocument
.getDiagram();
148 // ____________________
150 public void lockControllers()
151 throws RuntimeException
153 UnoRuntime
.queryInterface( XModel
.class, maChartDocument
).lockControllers();
156 // ____________________
158 public void unlockControllers()
159 throws RuntimeException
161 UnoRuntime
.queryInterface( XModel
.class, maChartDocument
).unlockControllers();
164 // ____________________
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", new Boolean( true ));
177 // change attributes for all series
178 // set line width to 0.5mm
179 aDiaProp
.setPropertyValue( "LineWidth", new Integer( 50 ));
183 // ____________________
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", new Integer(
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", new Integer( ChartSymbolType
.BITMAPURL
));
213 aSeriesProp
.setPropertyValue( "SymbolBitmapURL", sUrl
.toString() );
217 aSeriesProp
.setPropertyValue( "SymbolType", new Integer( 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
);
228 // ____________________
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
240 XChartDataArray aDataArray
= UnoRuntime
.queryInterface(
241 XChartDataArray
.class, maChartDocument
.getData());
242 double aData
[][] = aDataArray
.getData();
245 double fMax
= aData
[ 0 ][ 1 ];
246 for( i
= 1; i
< aData
.length
; i
++ )
248 if( aData
[ i
][ 1 ] > fMax
)
250 fMax
= aData
[ i
][ 1 ];
255 // first parameter is the index of the point, the second one is the series
256 XPropertySet aPointProp
= maDiagram
.getDataPointProperties( 0, 1 );
258 // set a different, larger symbol
259 aPointProp
.setPropertyValue( "SymbolType", new Integer( ChartSymbolType
.SYMBOL6
));
260 aPointProp
.setPropertyValue( "SymbolSize", new Size( 600, 600 ));
262 // add a label text with bold font, bordeaux red 14pt
263 aPointProp
.setPropertyValue( "DataCaption", new Integer( ChartDataCaption
.VALUE
));
264 aPointProp
.setPropertyValue( "CharHeight", new Float( 14.0 ));
265 aPointProp
.setPropertyValue( "CharColor", new Integer( 0x993366 ));
266 aPointProp
.setPropertyValue( "CharWeight", new Float( FontWeight
.BOLD
));
268 catch( IndexOutOfBoundsException ex
)
270 System
.out
.println( "Oops, there not enough data points or series for setting properties: " + ex
);
274 // ____________________
276 public void testArea()
277 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
278 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
280 XPropertySet aArea
= maChartDocument
.getArea();
284 // change background color of entire chart
285 aArea
.setPropertyValue( "FillStyle", FillStyle
.SOLID
);
286 aArea
.setPropertyValue( "FillColor", new Integer( 0xeeeeee ));
290 // ____________________
292 public void testWall()
293 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
294 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
296 XPropertySet aWall
= UnoRuntime
.queryInterface(
297 X3DDisplay
.class, maDiagram
).getWall();
299 // change background color of area
300 aWall
.setPropertyValue( "FillStyle", FillStyle
.SOLID
);
301 aWall
.setPropertyValue( "FillColor", new Integer( 0xcccccc ));
304 // ____________________
306 public void testTitle()
307 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
308 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
311 XPropertySet aDocProp
= UnoRuntime
.queryInterface(
312 XPropertySet
.class, maChartDocument
);
313 aDocProp
.setPropertyValue( "HasMainTitle", new Boolean( true ));
315 XShape aTitle
= maChartDocument
.getTitle();
316 XPropertySet aTitleProp
= UnoRuntime
.queryInterface( XPropertySet
.class, aTitle
);
319 if( aTitleProp
!= null )
321 aTitleProp
.setPropertyValue( "String", "Random Scatter Chart" );
322 aTitleProp
.setPropertyValue( "CharHeight", new Float(14.0) );
325 // align title with y axis
326 XShape aAxis
= UnoRuntime
.queryInterface(
327 XShape
.class, UnoRuntime
.queryInterface(
328 XAxisYSupplier
.class, maDiagram
).getYAxis() );
333 Point aPos
= aTitle
.getPosition();
334 aPos
.X
= ( aAxis
.getPosition() ).X
;
335 aTitle
.setPosition( aPos
);
339 // ____________________
341 public void testAxis()
342 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
343 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
,
344 MalformedNumberFormatException
347 XPropertySet aAxisProp
= UnoRuntime
.queryInterface(
348 XAxisXSupplier
.class, maDiagram
).getXAxis();
349 if( aAxisProp
!= null )
351 aAxisProp
.setPropertyValue( "Max", new Integer( 24 ));
352 aAxisProp
.setPropertyValue( "StepMain", new Integer( 3 ));
355 // change number format for y axis
356 aAxisProp
= UnoRuntime
.queryInterface(
357 XAxisYSupplier
.class, maDiagram
).getYAxis();
359 // add a new custom number format and get the new key
360 int nNewNumberFormat
= 0;
361 XNumberFormatsSupplier aNumFmtSupp
= UnoRuntime
.queryInterface(
362 XNumberFormatsSupplier
.class, maChartDocument
);
364 if( aNumFmtSupp
!= null )
366 XNumberFormats aFormats
= aNumFmtSupp
.getNumberFormats();
367 Locale aLocale
= new Locale( "de", "DE", "de" );
369 String aFormatStr
= aFormats
.generateFormat( nNewNumberFormat
, aLocale
, true, true, (short)3, (short)1 );
370 nNewNumberFormat
= aFormats
.addNew( aFormatStr
, aLocale
);
373 if( aAxisProp
!= null )
375 aAxisProp
.setPropertyValue( "NumberFormat", new Integer( nNewNumberFormat
));
379 // ____________________
381 public void testGrid()
382 throws RuntimeException
, UnknownPropertyException
, PropertyVetoException
,
383 com
.sun
.star
.lang
.IllegalArgumentException
, WrappedTargetException
386 XPropertySet aGridProp
= UnoRuntime
.queryInterface(
388 UnoRuntime
.queryInterface(
389 XAxisYSupplier
.class, maDiagram
).getYMainGrid());
391 if( aGridProp
!= null )
393 LineDash aDash
= new LineDash();
394 aDash
.Style
= DashStyle
.ROUND
;
399 aDash
.Distance
= 100;
401 aGridProp
.setPropertyValue( "LineColor", new Integer( 0x999999 ));
402 aGridProp
.setPropertyValue( "LineStyle", LineStyle
.DASH
);
403 aGridProp
.setPropertyValue( "LineDash", aDash
);
404 aGridProp
.setPropertyValue( "LineWidth", new Integer( 30 ));
409 // ______________________________
412 // ______________________________
414 private XChartDocument maChartDocument
;
415 private XDiagram maDiagram
;