1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // __________ Imports __________
25 
26 // base classes
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.lang.*;
30 
31 // property access
32 import com.sun.star.beans.*;
33 
34 // application specific classes
35 import com.sun.star.chart.*;
36 import com.sun.star.drawing.*;
37 
38 import com.sun.star.table.CellRangeAddress;
39 import com.sun.star.table.XCellRange;
40 import com.sun.star.sheet.XCellRangeAddressable;
41 
42 import com.sun.star.frame.XModel;
43 import com.sun.star.frame.XController;
44 
45 import com.sun.star.util.XNumberFormatsSupplier;
46 import com.sun.star.util.XNumberFormats;
47 
48 // base graphics things
49 import com.sun.star.awt.Point;
50 import com.sun.star.awt.Size;
51 import com.sun.star.awt.Rectangle;
52 import com.sun.star.awt.FontWeight;
53 import com.sun.star.awt.FontRelief;
54 
55 // Exceptions
56 import com.sun.star.uno.Exception;
57 import com.sun.star.uno.RuntimeException;
58 import com.sun.star.beans.UnknownPropertyException;
59 import com.sun.star.lang.IndexOutOfBoundsException;
60 import com.sun.star.util.MalformedNumberFormatException;
61 
62 
63 // __________ Implementation __________
64 
65 /** Create a spreadsheet add some data and add a chart
66     @author Björn Milcke
67  */
68 public class ChartInCalc
69 {
70     // ____________________
71 
main( String args[] )72     public static void main( String args[] )
73     {
74         Helper aHelper = new Helper( args );
75 
76         CalcHelper aCalcHelper = new CalcHelper( aHelper.createSpreadsheetDocument() );
77 
78         // insert a cell range with 4 columns and 24 rows filled with random numbers
79         XCellRange aRange = aCalcHelper.insertRandomRange( 4, 24 );
80         CellRangeAddress aRangeAddress = ((XCellRangeAddressable) UnoRuntime.queryInterface(
81             XCellRangeAddressable.class, aRange)).getRangeAddress();
82 
83         // change view to sheet containing the chart
84         aCalcHelper.raiseChartSheet();
85 
86         // the unit for measures is 1/100th of a millimeter
87         // position at (1cm, 1cm)
88         Point aPos    = new Point( 1000, 1000 );
89 
90         // size of the chart is 15cm x 9.271cm
91         Size  aExtent = new Size( 15000, 9271 );
92 
93         // insert a new chart into the "Chart" sheet of the
94         // spreadsheet document
95         XChartDocument aChartDoc = aCalcHelper.insertChart(
96             "ScatterChart",
97             aRangeAddress,
98             aPos,
99             aExtent,
100             "com.sun.star.chart.XYDiagram" );
101 
102         // instantiate test class with newly created chart
103         ChartInCalc aTest   = new ChartInCalc( aChartDoc );
104 
105         try
106         {
107             aTest.lockControllers();
108 
109             aTest.testDiagram();
110             aTest.testArea();
111             aTest.testWall();
112             aTest.testTitle();
113             aTest.testAxis();
114             aTest.testGrid();
115 
116             // show an intermediate state, ...
117             aTest.unlockControllers();
118             aTest.lockControllers();
119 
120             // ..., because the following takes a while:
121             // an internet URL has to be resolved
122             aTest.testDataRowProperties();
123             aTest.testDataPointProperties();
124 
125             aTest.unlockControllers();
126         }
127         catch( Exception ex )
128         {
129             System.out.println( "UNO Exception caught: " + ex );
130             System.out.println( "Message: " + ex.getMessage() );
131         }
132 
133         System.exit( 0 );
134     }
135 
136 
137     // ________________________________________
138 
ChartInCalc( XChartDocument aChartDoc )139     public ChartInCalc( XChartDocument aChartDoc )
140     {
141         maChartDocument = aChartDoc;
142         maDiagram       = maChartDocument.getDiagram();
143     }
144 
145     // ____________________
146 
lockControllers()147     public void lockControllers()
148         throws RuntimeException
149     {
150         ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).lockControllers();
151     }
152 
153     // ____________________
154 
unlockControllers()155     public void unlockControllers()
156         throws RuntimeException
157     {
158         ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).unlockControllers();
159     }
160 
161     // ____________________
162 
testDiagram()163     public void testDiagram()
164         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
165                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
166     {
167         XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, maDiagram );
168 
169         if( aDiaProp != null )
170         {
171             // change chart type
172             aDiaProp.setPropertyValue( "Lines", new Boolean( true ));
173 
174             // change attributes for all series
175             // set line width to 0.5mm
176             aDiaProp.setPropertyValue( "LineWidth",  new Integer( 50 ));
177         }
178     }
179 
180     // ____________________
181 
testDataRowProperties()182     public void testDataRowProperties()
183         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
184                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
185     {
186         // change properties of the data series
187         try
188         {
189             XPropertySet aSeriesProp;
190             for( int i = 1; i <= 3; i++ )
191             {
192                 aSeriesProp = maDiagram.getDataRowProperties( i );
193                 aSeriesProp.setPropertyValue( "LineColor", new Integer(
194                                                   0x400000 * i +
195                                                   0x005000 * i +
196                                                   0x0000ff - 0x40 * i ));
197                 if( 1 == i )
198                 {
199                     StringBuffer sUrl = new StringBuffer("file:///");
200                     try {
201                         /* for use without net it's easier to load a local graphic */
202                         java.io.File sourceFile = new java.io.File("bullet.gif");
203                         sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
204                     } catch (java.io.IOException e) {
205                         sUrl = new StringBuffer("http://graphics.openoffice.org/chart/bullet1.gif");
206                     }
207 
208                     // set a bitmap via URL as symbol for the first series
209                     aSeriesProp.setPropertyValue( "SymbolType", new Integer( ChartSymbolType.BITMAPURL ));
210                     aSeriesProp.setPropertyValue( "SymbolBitmapURL", sUrl.toString() );
211                 }
212                 else
213                 {
214                     aSeriesProp.setPropertyValue( "SymbolType", new Integer( ChartSymbolType.SYMBOL1 ));
215                     aSeriesProp.setPropertyValue( "SymbolSize", new Size( 250, 250 ));
216                 }
217             }
218         }
219         catch( IndexOutOfBoundsException ex )
220         {
221             System.out.println( "Oops, there not enough series for setting properties: " + ex );
222         }
223     }
224 
225     // ____________________
226 
testDataPointProperties()227     public void testDataPointProperties()
228         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
229                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
230     {
231         // set properties for a single data point
232         try
233         {
234             // determine the maximum value of the first series
235             int nMaxIndex = 0;
236 
237             XChartDataArray aDataArray = (XChartDataArray) UnoRuntime.queryInterface(
238                 XChartDataArray.class, maChartDocument.getData());
239             double aData[][] = aDataArray.getData();
240 
241             int i;
242             double fMax = aData[ 0 ][ 1 ];
243             for( i = 1; i < aData.length; i++ )
244             {
245                 if( aData[ i ][ 1 ] > fMax )
246                 {
247                     fMax = aData[ i ][ 1 ];
248                     nMaxIndex = i;
249                 }
250             }
251 
252             // first parameter is the index of the point, the second one is the series
253             XPropertySet aPointProp = maDiagram.getDataPointProperties( 0, 1 );
254 
255             // set a different, larger symbol
256             aPointProp.setPropertyValue( "SymbolType", new Integer( ChartSymbolType.SYMBOL6 ));
257             aPointProp.setPropertyValue( "SymbolSize", new Size( 600, 600 ));
258 
259             // add a label text with bold font, bordeaux red 14pt
260             aPointProp.setPropertyValue( "DataCaption", new Integer( ChartDataCaption.VALUE ));
261             aPointProp.setPropertyValue( "CharHeight",  new Float( 14.0 ));
262             aPointProp.setPropertyValue( "CharColor",   new Integer( 0x993366 ));
263             aPointProp.setPropertyValue( "CharWeight",  new Float( FontWeight.BOLD ));
264         }
265         catch( IndexOutOfBoundsException ex )
266         {
267             System.out.println( "Oops, there not enough data points or series for setting properties: " + ex );
268         }
269     }
270 
271     // ____________________
272 
testArea()273     public void testArea()
274         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
275                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
276     {
277         XPropertySet   aArea = maChartDocument.getArea();
278 
279         if( aArea != null )
280         {
281             // change background color of entire chart
282             aArea.setPropertyValue( "FillStyle", FillStyle.SOLID );
283             aArea.setPropertyValue( "FillColor", new Integer( 0xeeeeee ));
284         }
285     }
286 
287     // ____________________
288 
testWall()289     public void testWall()
290         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
291                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
292     {
293         XPropertySet aWall = ((X3DDisplay) UnoRuntime.queryInterface(
294                                   X3DDisplay.class, maDiagram )).getWall();
295 
296         // change background color of area
297         aWall.setPropertyValue( "FillStyle", FillStyle.SOLID );
298         aWall.setPropertyValue( "FillColor", new Integer( 0xcccccc ));
299     }
300 
301     // ____________________
302 
testTitle()303     public void testTitle()
304         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
305                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
306     {
307         // change main title
308         XPropertySet aDocProp = (XPropertySet) UnoRuntime.queryInterface(
309             XPropertySet.class, maChartDocument );
310         aDocProp.setPropertyValue( "HasMainTitle", new Boolean( true ));
311 
312         XShape aTitle = maChartDocument.getTitle();
313         XPropertySet aTitleProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aTitle );
314 
315         // set new text
316         if( aTitleProp != null )
317         {
318             aTitleProp.setPropertyValue( "String", "Random Scatter Chart" );
319             aTitleProp.setPropertyValue( "CharHeight", new Float(14.0) );
320         }
321 
322         // align title with y axis
323         XShape aAxis = (XShape) UnoRuntime.queryInterface(
324             XShape.class, ((XAxisYSupplier) UnoRuntime.queryInterface(
325                 XAxisYSupplier.class, maDiagram )).getYAxis() );
326 
327         if( aAxis != null &&
328             aTitle != null )
329         {
330             Point aPos = aTitle.getPosition();
331             aPos.X = ( aAxis.getPosition() ).X;
332             aTitle.setPosition( aPos );
333         }
334     }
335 
336     // ____________________
337 
testAxis()338     public void testAxis()
339         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
340                com.sun.star.lang.IllegalArgumentException, WrappedTargetException,
341                MalformedNumberFormatException
342     {
343         // x axis
344         XPropertySet aAxisProp = ((XAxisXSupplier) UnoRuntime.queryInterface(
345                                       XAxisXSupplier.class, maDiagram )).getXAxis();
346         if( aAxisProp != null )
347         {
348             aAxisProp.setPropertyValue( "Max",      new Integer( 24 ));
349             aAxisProp.setPropertyValue( "StepMain", new Integer( 3 ));
350         }
351 
352         // change number format for y axis
353         aAxisProp = ((XAxisYSupplier) UnoRuntime.queryInterface(
354                          XAxisYSupplier.class, maDiagram )).getYAxis();
355 
356         // add a new custom number format and get the new key
357         int nNewNumberFormat = 0;
358         XNumberFormatsSupplier aNumFmtSupp = (XNumberFormatsSupplier) UnoRuntime.queryInterface(
359             XNumberFormatsSupplier.class, maChartDocument );
360 
361         if( aNumFmtSupp != null )
362         {
363             XNumberFormats aFormats = aNumFmtSupp.getNumberFormats();
364             Locale aLocale = new Locale( "de", "DE", "de" );
365 
366             String aFormatStr = aFormats.generateFormat( nNewNumberFormat, aLocale, true, true, (short)3, (short)1 );
367             nNewNumberFormat = aFormats.addNew( aFormatStr, aLocale );
368         }
369 
370         if( aAxisProp != null )
371         {
372             aAxisProp.setPropertyValue( "NumberFormat", new Integer( nNewNumberFormat ));
373         }
374     }
375 
376     // ____________________
377 
testGrid()378     public void testGrid()
379         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
380                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
381     {
382         // y major grid
383         XPropertySet aGridProp = (XPropertySet) UnoRuntime.queryInterface(
384             XPropertySet.class,
385             ( (XAxisYSupplier) UnoRuntime.queryInterface(
386                 XAxisYSupplier.class, maDiagram )).getYMainGrid());
387 
388         if( aGridProp != null )
389         {
390             LineDash aDash = new LineDash();
391             aDash.Style    = DashStyle.ROUND;
392             aDash.Dots     = 2;
393             aDash.DotLen   = 10;
394             aDash.Dashes   = 1;
395             aDash.DashLen  = 200;
396             aDash.Distance = 100;
397 
398             aGridProp.setPropertyValue( "LineColor", new Integer( 0x999999 ));
399             aGridProp.setPropertyValue( "LineStyle", LineStyle.DASH );
400             aGridProp.setPropertyValue( "LineDash", aDash );
401             aGridProp.setPropertyValue( "LineWidth", new Integer( 30 ));
402         }
403     }
404 
405 
406     // ______________________________
407     //
408     // private members
409     // ______________________________
410 
411     private XChartDocument maChartDocument;
412     private XDiagram       maDiagram;
413 }
414