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