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.frame.XModel;
40 import com.sun.star.frame.XController;
41 
42 import com.sun.star.util.XNumberFormatsSupplier;
43 import com.sun.star.util.XNumberFormats;
44 
45 // base graphics things
46 import com.sun.star.awt.Point;
47 import com.sun.star.awt.Size;
48 import com.sun.star.awt.Rectangle;
49 import com.sun.star.awt.FontWeight;
50 import com.sun.star.awt.FontRelief;
51 
52 // Exceptions
53 import com.sun.star.uno.Exception;
54 import com.sun.star.uno.RuntimeException;
55 import com.sun.star.beans.UnknownPropertyException;
56 import com.sun.star.lang.IndexOutOfBoundsException;
57 import com.sun.star.util.MalformedNumberFormatException;
58 
59 
60 // __________ Implementation __________
61 
62 /** Create a spreadsheet add some data and add a chart
63     @author Björn Milcke
64  */
65 public class ChartInDraw
66 {
67     // ____________________
68 
main( String args[] )69     public static void main( String args[] )
70     {
71         Helper aHelper = new Helper( args );
72 
73         ChartHelper aChartHelper = new ChartHelper( aHelper.createDrawingDocument());
74 
75         // the unit for measures is 1/100th of a millimeter
76         // position at (1cm, 1cm)
77         Point aPos    = new Point( 1000, 1000 );
78 
79         // size of the chart is 15cm x 12cm
80         Size  aExtent = new Size( 15000, 13000 );
81 
82         // insert a new chart into the "Chart" sheet of the
83         // spreadsheet document
84         XChartDocument aChartDoc = aChartHelper.insertOLEChartInDraw(
85             "BarChart",
86             aPos,
87             aExtent,
88             "com.sun.star.chart.BarDiagram" );
89 
90         // instantiate test class with newly created chart
91         ChartInDraw aTest   = new ChartInDraw( aChartDoc );
92 
93         try
94         {
95             aTest.lockControllers();
96 
97             aTest.testArea();
98             aTest.testWall();
99             aTest.testTitle();
100             aTest.testLegend();
101             aTest.testThreeD();
102 
103             aTest.unlockControllers();
104         }
105         catch( Exception ex )
106         {
107             System.out.println( "UNO Exception caught: " + ex );
108             System.out.println( "Message: " + ex.getMessage() );
109         }
110 
111         System.exit( 0 );
112     }
113 
114 
115     // ________________________________________
116 
ChartInDraw( XChartDocument aChartDoc )117     public ChartInDraw( XChartDocument aChartDoc )
118     {
119         maChartDocument = aChartDoc;
120         maDiagram       = maChartDocument.getDiagram();
121     }
122 
123     // ____________________
124 
lockControllers()125     public void lockControllers()
126         throws RuntimeException
127     {
128         ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).lockControllers();
129     }
130 
131     // ____________________
132 
unlockControllers()133     public void unlockControllers()
134         throws RuntimeException
135     {
136         ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).unlockControllers();
137     }
138 
139     // ____________________
140 
testArea()141     public void testArea()
142         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
143                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
144     {
145         XPropertySet   aArea = maChartDocument.getArea();
146 
147         if( aArea != null )
148         {
149             // change background color of entire chart
150             aArea.setPropertyValue( "FillStyle", FillStyle.SOLID );
151             aArea.setPropertyValue( "FillColor", new Integer( 0xeeeeee ));
152         }
153     }
154 
155     // ____________________
156 
testWall()157     public void testWall()
158         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
159                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
160     {
161         XPropertySet aWall = ((X3DDisplay) UnoRuntime.queryInterface(
162                                   X3DDisplay.class, maDiagram )).getWall();
163 
164         // change background color of area
165         aWall.setPropertyValue( "FillColor", new Integer( 0xcccccc ));
166         aWall.setPropertyValue( "FillStyle",  FillStyle.SOLID );
167     }
168 
169     // ____________________
170 
testTitle()171     public void testTitle()
172         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
173                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
174     {
175         // change main title
176         XPropertySet aDocProp = (XPropertySet) UnoRuntime.queryInterface(
177             XPropertySet.class, maChartDocument );
178         aDocProp.setPropertyValue( "HasMainTitle", new Boolean( true ));
179 
180         XShape aTitle = maChartDocument.getTitle();
181         XPropertySet aTitleProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aTitle );
182 
183         // set new text
184         if( aTitleProp != null )
185         {
186             aTitleProp.setPropertyValue( "String", "Bar Chart in a Draw Document" );
187         }
188     }
189 
190     // ____________________
191 
testLegend()192     public void testLegend()
193         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
194                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
195     {
196         XShape aLegend = maChartDocument.getLegend();
197         XPropertySet aLegendProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aLegend );
198 
199         aLegendProp.setPropertyValue( "Alignment", ChartLegendPosition.LEFT );
200         aLegendProp.setPropertyValue( "FillStyle", FillStyle.SOLID );
201         aLegendProp.setPropertyValue( "FillColor", new Integer( 0xeeddee ));
202     }
203 
204     // ____________________
205 
testThreeD()206     public void testThreeD()
207         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
208         com.sun.star.lang.IllegalArgumentException, WrappedTargetException,
209         com.sun.star.lang.IndexOutOfBoundsException
210     {
211         XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, maDiagram );
212         Boolean aTrue = new Boolean( true );
213 
214         aDiaProp.setPropertyValue( "Dim3D", aTrue );
215         aDiaProp.setPropertyValue( "Deep", aTrue );
216         // from Chart3DBarProperties:
217         aDiaProp.setPropertyValue( "SolidType", new Integer( ChartSolidType.CYLINDER ));
218 
219         // change floor color to Magenta6
220         XPropertySet aFloor = ((X3DDisplay) UnoRuntime.queryInterface(
221                                    X3DDisplay.class, maDiagram )).getFloor();
222         aFloor.setPropertyValue( "FillColor", new Integer( 0x6b2394 ));
223 
224         // apply changes to get a 3d scene
225         unlockControllers();
226         lockControllers();
227 
228 
229         // rotate scene to a different angle
230         HomogenMatrix aMatrix = new HomogenMatrix();
231         HomogenMatrixLine aLines[] = new HomogenMatrixLine[]
232             {
233                 new HomogenMatrixLine( 1.0, 0.0, 0.0, 0.0 ),
234                 new HomogenMatrixLine( 0.0, 1.0, 0.0, 0.0 ),
235                 new HomogenMatrixLine( 0.0, 0.0, 1.0, 0.0 ),
236                 new HomogenMatrixLine( 0.0, 0.0, 0.0, 1.0 )
237             };
238 
239         aMatrix.Line1 = aLines[ 0 ];
240         aMatrix.Line2 = aLines[ 1 ];
241         aMatrix.Line3 = aLines[ 2 ];
242         aMatrix.Line4 = aLines[ 3 ];
243 
244         // rotate 10 degrees along the x axis
245         double fAngle = 10.0;
246         double fCosX = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
247         double fSinX = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
248 
249         // rotate -20 degrees along the y axis
250         fAngle = -20.0;
251         double fCosY = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
252         double fSinY = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
253 
254         // rotate -5 degrees along the z axis
255         fAngle = -5.0;
256         double fCosZ = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
257         double fSinZ = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
258 
259         aMatrix.Line1.Column1 =  fCosY *  fCosZ;
260         aMatrix.Line1.Column2 =  fCosY * -fSinZ;
261         aMatrix.Line1.Column3 =  fSinY;
262 
263         aMatrix.Line2.Column1 =  fSinX *  fSinY *  fCosZ +  fCosX *  fSinZ;
264         aMatrix.Line2.Column2 = -fSinX *  fSinY *  fSinZ +  fCosX *  fCosZ;
265         aMatrix.Line2.Column3 = -fSinX *  fCosY;
266 
267         aMatrix.Line3.Column1 = -fCosX *  fSinY *  fCosZ +  fSinX *  fSinZ;
268         aMatrix.Line3.Column2 =  fCosX *  fSinY *  fSinZ +  fSinX *  fCosZ;
269         aMatrix.Line3.Column3 =  fCosX *  fCosY;
270 
271         aDiaProp.setPropertyValue( "D3DTransformMatrix", aMatrix );
272 
273         // add a red light source
274 
275         // in a chart by default only the second (non-specular) light source is switched on
276         // light source 1 is a specular light source
277         aDiaProp.setPropertyValue( "D3DSceneLightColor1", new Integer( 0xff3333 ));
278 
279         // set direction
280         com.sun.star.drawing.Direction3D aDirection = new com.sun.star.drawing.Direction3D();
281 
282         aDirection.DirectionX = -0.75;
283         aDirection.DirectionY =  0.5;
284         aDirection.DirectionZ =  0.5;
285 
286         aDiaProp.setPropertyValue( "D3DSceneLightDirection1", aDirection );
287         aDiaProp.setPropertyValue( "D3DSceneLightOn1", new Boolean( true ));
288     }
289 
290     // ______________________________
291     //
292     // private members
293     // ______________________________
294 
295     private XChartDocument maChartDocument;
296     private XDiagram       maDiagram;
297 }
298