xref: /AOO41X/main/odk/examples/DevelopersGuide/Charts/ChartInDraw.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // __________ Imports __________
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir // base classes
38*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40*cdf0e10cSrcweir import com.sun.star.lang.*;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // property access
43*cdf0e10cSrcweir import com.sun.star.beans.*;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // application specific classes
46*cdf0e10cSrcweir import com.sun.star.chart.*;
47*cdf0e10cSrcweir import com.sun.star.drawing.*;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
50*cdf0e10cSrcweir import com.sun.star.frame.XModel;
51*cdf0e10cSrcweir import com.sun.star.frame.XController;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir import com.sun.star.util.XNumberFormatsSupplier;
54*cdf0e10cSrcweir import com.sun.star.util.XNumberFormats;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir // base graphics things
57*cdf0e10cSrcweir import com.sun.star.awt.Point;
58*cdf0e10cSrcweir import com.sun.star.awt.Size;
59*cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
60*cdf0e10cSrcweir import com.sun.star.awt.FontWeight;
61*cdf0e10cSrcweir import com.sun.star.awt.FontRelief;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir // Exceptions
64*cdf0e10cSrcweir import com.sun.star.uno.Exception;
65*cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
66*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
67*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
68*cdf0e10cSrcweir import com.sun.star.util.MalformedNumberFormatException;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir // __________ Implementation __________
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir /** Create a spreadsheet add some data and add a chart
74*cdf0e10cSrcweir     @author Björn Milcke
75*cdf0e10cSrcweir  */
76*cdf0e10cSrcweir public class ChartInDraw
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     // ____________________
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     public static void main( String args[] )
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         Helper aHelper = new Helper( args );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         ChartHelper aChartHelper = new ChartHelper( aHelper.createDrawingDocument());
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         // the unit for measures is 1/100th of a millimeter
87*cdf0e10cSrcweir         // position at (1cm, 1cm)
88*cdf0e10cSrcweir         Point aPos    = new Point( 1000, 1000 );
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         // size of the chart is 15cm x 12cm
91*cdf0e10cSrcweir         Size  aExtent = new Size( 15000, 13000 );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         // insert a new chart into the "Chart" sheet of the
94*cdf0e10cSrcweir         // spreadsheet document
95*cdf0e10cSrcweir         XChartDocument aChartDoc = aChartHelper.insertOLEChartInDraw(
96*cdf0e10cSrcweir             "BarChart",
97*cdf0e10cSrcweir             aPos,
98*cdf0e10cSrcweir             aExtent,
99*cdf0e10cSrcweir             "com.sun.star.chart.BarDiagram" );
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         // instantiate test class with newly created chart
102*cdf0e10cSrcweir         ChartInDraw aTest   = new ChartInDraw( aChartDoc );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         try
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             aTest.lockControllers();
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir             aTest.testArea();
109*cdf0e10cSrcweir             aTest.testWall();
110*cdf0e10cSrcweir             aTest.testTitle();
111*cdf0e10cSrcweir             aTest.testLegend();
112*cdf0e10cSrcweir             aTest.testThreeD();
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir             aTest.unlockControllers();
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir         catch( Exception ex )
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             System.out.println( "UNO Exception caught: " + ex );
119*cdf0e10cSrcweir             System.out.println( "Message: " + ex.getMessage() );
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         System.exit( 0 );
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     // ________________________________________
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     public ChartInDraw( XChartDocument aChartDoc )
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         maChartDocument = aChartDoc;
131*cdf0e10cSrcweir         maDiagram       = maChartDocument.getDiagram();
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     // ____________________
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     public void lockControllers()
137*cdf0e10cSrcweir         throws RuntimeException
138*cdf0e10cSrcweir     {
139*cdf0e10cSrcweir         ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).lockControllers();
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     // ____________________
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     public void unlockControllers()
145*cdf0e10cSrcweir         throws RuntimeException
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir         ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).unlockControllers();
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     // ____________________
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     public void testArea()
153*cdf0e10cSrcweir         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
154*cdf0e10cSrcweir                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         XPropertySet   aArea = maChartDocument.getArea();
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         if( aArea != null )
159*cdf0e10cSrcweir         {
160*cdf0e10cSrcweir             // change background color of entire chart
161*cdf0e10cSrcweir             aArea.setPropertyValue( "FillStyle", FillStyle.SOLID );
162*cdf0e10cSrcweir             aArea.setPropertyValue( "FillColor", new Integer( 0xeeeeee ));
163*cdf0e10cSrcweir         }
164*cdf0e10cSrcweir     }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     // ____________________
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     public void testWall()
169*cdf0e10cSrcweir         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
170*cdf0e10cSrcweir                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         XPropertySet aWall = ((X3DDisplay) UnoRuntime.queryInterface(
173*cdf0e10cSrcweir                                   X3DDisplay.class, maDiagram )).getWall();
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir         // change background color of area
176*cdf0e10cSrcweir         aWall.setPropertyValue( "FillColor", new Integer( 0xcccccc ));
177*cdf0e10cSrcweir         aWall.setPropertyValue( "FillStyle",  FillStyle.SOLID );
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     // ____________________
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     public void testTitle()
183*cdf0e10cSrcweir         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
184*cdf0e10cSrcweir                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
185*cdf0e10cSrcweir     {
186*cdf0e10cSrcweir         // change main title
187*cdf0e10cSrcweir         XPropertySet aDocProp = (XPropertySet) UnoRuntime.queryInterface(
188*cdf0e10cSrcweir             XPropertySet.class, maChartDocument );
189*cdf0e10cSrcweir         aDocProp.setPropertyValue( "HasMainTitle", new Boolean( true ));
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         XShape aTitle = maChartDocument.getTitle();
192*cdf0e10cSrcweir         XPropertySet aTitleProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aTitle );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         // set new text
195*cdf0e10cSrcweir         if( aTitleProp != null )
196*cdf0e10cSrcweir         {
197*cdf0e10cSrcweir             aTitleProp.setPropertyValue( "String", "Bar Chart in a Draw Document" );
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     // ____________________
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     public void testLegend()
204*cdf0e10cSrcweir         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
205*cdf0e10cSrcweir                com.sun.star.lang.IllegalArgumentException, WrappedTargetException
206*cdf0e10cSrcweir     {
207*cdf0e10cSrcweir         XShape aLegend = maChartDocument.getLegend();
208*cdf0e10cSrcweir         XPropertySet aLegendProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aLegend );
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir         aLegendProp.setPropertyValue( "Alignment", ChartLegendPosition.LEFT );
211*cdf0e10cSrcweir         aLegendProp.setPropertyValue( "FillStyle", FillStyle.SOLID );
212*cdf0e10cSrcweir         aLegendProp.setPropertyValue( "FillColor", new Integer( 0xeeddee ));
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     // ____________________
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     public void testThreeD()
218*cdf0e10cSrcweir         throws RuntimeException, UnknownPropertyException, PropertyVetoException,
219*cdf0e10cSrcweir         com.sun.star.lang.IllegalArgumentException, WrappedTargetException,
220*cdf0e10cSrcweir         com.sun.star.lang.IndexOutOfBoundsException
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, maDiagram );
223*cdf0e10cSrcweir         Boolean aTrue = new Boolean( true );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "Dim3D", aTrue );
226*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "Deep", aTrue );
227*cdf0e10cSrcweir         // from Chart3DBarProperties:
228*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "SolidType", new Integer( ChartSolidType.CYLINDER ));
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         // change floor color to Magenta6
231*cdf0e10cSrcweir         XPropertySet aFloor = ((X3DDisplay) UnoRuntime.queryInterface(
232*cdf0e10cSrcweir                                    X3DDisplay.class, maDiagram )).getFloor();
233*cdf0e10cSrcweir         aFloor.setPropertyValue( "FillColor", new Integer( 0x6b2394 ));
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         // apply changes to get a 3d scene
236*cdf0e10cSrcweir         unlockControllers();
237*cdf0e10cSrcweir         lockControllers();
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir         // rotate scene to a different angle
241*cdf0e10cSrcweir         HomogenMatrix aMatrix = new HomogenMatrix();
242*cdf0e10cSrcweir         HomogenMatrixLine aLines[] = new HomogenMatrixLine[]
243*cdf0e10cSrcweir             {
244*cdf0e10cSrcweir                 new HomogenMatrixLine( 1.0, 0.0, 0.0, 0.0 ),
245*cdf0e10cSrcweir                 new HomogenMatrixLine( 0.0, 1.0, 0.0, 0.0 ),
246*cdf0e10cSrcweir                 new HomogenMatrixLine( 0.0, 0.0, 1.0, 0.0 ),
247*cdf0e10cSrcweir                 new HomogenMatrixLine( 0.0, 0.0, 0.0, 1.0 )
248*cdf0e10cSrcweir             };
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir         aMatrix.Line1 = aLines[ 0 ];
251*cdf0e10cSrcweir         aMatrix.Line2 = aLines[ 1 ];
252*cdf0e10cSrcweir         aMatrix.Line3 = aLines[ 2 ];
253*cdf0e10cSrcweir         aMatrix.Line4 = aLines[ 3 ];
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         // rotate 10 degrees along the x axis
256*cdf0e10cSrcweir         double fAngle = 10.0;
257*cdf0e10cSrcweir         double fCosX = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
258*cdf0e10cSrcweir         double fSinX = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         // rotate -20 degrees along the y axis
261*cdf0e10cSrcweir         fAngle = -20.0;
262*cdf0e10cSrcweir         double fCosY = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
263*cdf0e10cSrcweir         double fSinY = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         // rotate -5 degrees along the z axis
266*cdf0e10cSrcweir         fAngle = -5.0;
267*cdf0e10cSrcweir         double fCosZ = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
268*cdf0e10cSrcweir         double fSinZ = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         aMatrix.Line1.Column1 =  fCosY *  fCosZ;
271*cdf0e10cSrcweir         aMatrix.Line1.Column2 =  fCosY * -fSinZ;
272*cdf0e10cSrcweir         aMatrix.Line1.Column3 =  fSinY;
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         aMatrix.Line2.Column1 =  fSinX *  fSinY *  fCosZ +  fCosX *  fSinZ;
275*cdf0e10cSrcweir         aMatrix.Line2.Column2 = -fSinX *  fSinY *  fSinZ +  fCosX *  fCosZ;
276*cdf0e10cSrcweir         aMatrix.Line2.Column3 = -fSinX *  fCosY;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         aMatrix.Line3.Column1 = -fCosX *  fSinY *  fCosZ +  fSinX *  fSinZ;
279*cdf0e10cSrcweir         aMatrix.Line3.Column2 =  fCosX *  fSinY *  fSinZ +  fSinX *  fCosZ;
280*cdf0e10cSrcweir         aMatrix.Line3.Column3 =  fCosX *  fCosY;
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "D3DTransformMatrix", aMatrix );
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         // add a red light source
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir         // in a chart by default only the second (non-specular) light source is switched on
287*cdf0e10cSrcweir         // light source 1 is a specular light source
288*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "D3DSceneLightColor1", new Integer( 0xff3333 ));
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir         // set direction
291*cdf0e10cSrcweir         com.sun.star.drawing.Direction3D aDirection = new com.sun.star.drawing.Direction3D();
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir         aDirection.DirectionX = -0.75;
294*cdf0e10cSrcweir         aDirection.DirectionY =  0.5;
295*cdf0e10cSrcweir         aDirection.DirectionZ =  0.5;
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "D3DSceneLightDirection1", aDirection );
298*cdf0e10cSrcweir         aDiaProp.setPropertyValue( "D3DSceneLightOn1", new Boolean( true ));
299*cdf0e10cSrcweir     }
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir     // ______________________________
302*cdf0e10cSrcweir     //
303*cdf0e10cSrcweir     // private members
304*cdf0e10cSrcweir     // ______________________________
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     private XChartDocument maChartDocument;
307*cdf0e10cSrcweir     private XDiagram       maDiagram;
308*cdf0e10cSrcweir }
309