bump product version to 7.2.5.1
[LibreOffice.git] / odk / examples / DevelopersGuide / Charts / ChartInDraw.java
blobd13b6d07fc8dfc4fab720b1ae65e068567b82527
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.lang.*;
41 // property access
42 import com.sun.star.beans.*;
44 // application specific classes
45 import com.sun.star.chart.*;
46 import com.sun.star.drawing.*;
48 import com.sun.star.frame.XModel;
49 // base graphics things
50 import com.sun.star.awt.Point;
51 import com.sun.star.awt.Size;
52 // Exceptions
53 import com.sun.star.uno.Exception;
54 import com.sun.star.uno.RuntimeException;
55 import com.sun.star.beans.UnknownPropertyException;
58 // __________ Implementation __________
60 // Create a spreadsheet add some data and add a chart
62 public class ChartInDraw
66 public static void main( String args[] )
68 Helper aHelper = new Helper( args );
70 ChartHelper aChartHelper = new ChartHelper( aHelper.createDrawingDocument());
72 // the unit for measures is 1/100th of a millimeter
73 // position at (1cm, 1cm)
74 Point aPos = new Point( 1000, 1000 );
76 // size of the chart is 15cm x 12cm
77 Size aExtent = new Size( 15000, 13000 );
79 // insert a new chart into the "Chart" sheet of the
80 // spreadsheet document
81 XChartDocument aChartDoc = aChartHelper.insertOLEChartInDraw(
82 aPos,
83 aExtent,
84 "com.sun.star.chart.BarDiagram" );
86 // instantiate test class with newly created chart
87 ChartInDraw aTest = new ChartInDraw( aChartDoc );
89 try
91 aTest.lockControllers();
93 aTest.testArea();
94 aTest.testWall();
95 aTest.testTitle();
96 aTest.testLegend();
97 aTest.testThreeD();
99 aTest.unlockControllers();
101 catch( Exception ex )
103 System.out.println( "UNO Exception caught: " + ex );
104 System.out.println( "Message: " + ex.getMessage() );
107 System.exit( 0 );
113 public ChartInDraw( XChartDocument aChartDoc )
115 maChartDocument = aChartDoc;
116 maDiagram = maChartDocument.getDiagram();
121 public void lockControllers()
122 throws RuntimeException
124 UnoRuntime.queryInterface( XModel.class, maChartDocument ).lockControllers();
129 public void unlockControllers()
130 throws RuntimeException
132 UnoRuntime.queryInterface( XModel.class, maChartDocument ).unlockControllers();
137 public void testArea()
138 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
139 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
141 XPropertySet aArea = maChartDocument.getArea();
143 if( aArea != null )
145 // change background color of entire chart
146 aArea.setPropertyValue( "FillStyle", FillStyle.SOLID );
147 aArea.setPropertyValue( "FillColor", Integer.valueOf( 0xeeeeee ));
153 public void testWall()
154 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
155 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
157 XPropertySet aWall = UnoRuntime.queryInterface(
158 X3DDisplay.class, maDiagram ).getWall();
160 // change background color of area
161 aWall.setPropertyValue( "FillColor", Integer.valueOf( 0xcccccc ));
162 aWall.setPropertyValue( "FillStyle", FillStyle.SOLID );
167 public void testTitle()
168 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
169 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
171 // change main title
172 XPropertySet aDocProp = UnoRuntime.queryInterface(
173 XPropertySet.class, maChartDocument );
174 aDocProp.setPropertyValue( "HasMainTitle", Boolean.TRUE);
176 XShape aTitle = maChartDocument.getTitle();
177 XPropertySet aTitleProp = UnoRuntime.queryInterface( XPropertySet.class, aTitle );
179 // set new text
180 if( aTitleProp != null )
182 aTitleProp.setPropertyValue( "String", "Bar Chart in a Draw Document" );
188 public void testLegend()
189 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
190 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
192 XShape aLegend = maChartDocument.getLegend();
193 XPropertySet aLegendProp = UnoRuntime.queryInterface( XPropertySet.class, aLegend );
195 aLegendProp.setPropertyValue( "Alignment", ChartLegendPosition.LEFT );
196 aLegendProp.setPropertyValue( "FillStyle", FillStyle.SOLID );
197 aLegendProp.setPropertyValue( "FillColor", Integer.valueOf( 0xeeddee ));
202 public void testThreeD()
203 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
204 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
206 XPropertySet aDiaProp = UnoRuntime.queryInterface( XPropertySet.class, maDiagram );
207 Boolean aTrue = Boolean.TRUE;
209 aDiaProp.setPropertyValue( "Dim3D", aTrue );
210 aDiaProp.setPropertyValue( "Deep", aTrue );
211 // from Chart3DBarProperties:
212 aDiaProp.setPropertyValue( "SolidType", Integer.valueOf( ChartSolidType.CYLINDER ));
214 // change floor color to Magenta6
215 XPropertySet aFloor = UnoRuntime.queryInterface(
216 X3DDisplay.class, maDiagram ).getFloor();
217 aFloor.setPropertyValue( "FillColor", Integer.valueOf( 0x6b2394 ));
219 // apply changes to get a 3d scene
220 unlockControllers();
221 lockControllers();
224 // rotate scene to a different angle
225 HomogenMatrix aMatrix = new HomogenMatrix();
226 HomogenMatrixLine aLines[] = new HomogenMatrixLine[]
228 new HomogenMatrixLine( 1.0, 0.0, 0.0, 0.0 ),
229 new HomogenMatrixLine( 0.0, 1.0, 0.0, 0.0 ),
230 new HomogenMatrixLine( 0.0, 0.0, 1.0, 0.0 ),
231 new HomogenMatrixLine( 0.0, 0.0, 0.0, 1.0 )
234 aMatrix.Line1 = aLines[ 0 ];
235 aMatrix.Line2 = aLines[ 1 ];
236 aMatrix.Line3 = aLines[ 2 ];
237 aMatrix.Line4 = aLines[ 3 ];
239 // rotate 10 degrees along the x axis
240 double fAngle = 10.0;
241 double fCosX = Math.cos( Math.PI / 180.0 * fAngle );
242 double fSinX = Math.sin( Math.PI / 180.0 * fAngle );
244 // rotate -20 degrees along the y axis
245 fAngle = -20.0;
246 double fCosY = Math.cos( Math.PI / 180.0 * fAngle );
247 double fSinY = Math.sin( Math.PI / 180.0 * fAngle );
249 // rotate -5 degrees along the z axis
250 fAngle = -5.0;
251 double fCosZ = Math.cos( Math.PI / 180.0 * fAngle );
252 double fSinZ = Math.sin( Math.PI / 180.0 * fAngle );
254 aMatrix.Line1.Column1 = fCosY * fCosZ;
255 aMatrix.Line1.Column2 = fCosY * -fSinZ;
256 aMatrix.Line1.Column3 = fSinY;
258 aMatrix.Line2.Column1 = fSinX * fSinY * fCosZ + fCosX * fSinZ;
259 aMatrix.Line2.Column2 = -fSinX * fSinY * fSinZ + fCosX * fCosZ;
260 aMatrix.Line2.Column3 = -fSinX * fCosY;
262 aMatrix.Line3.Column1 = -fCosX * fSinY * fCosZ + fSinX * fSinZ;
263 aMatrix.Line3.Column2 = fCosX * fSinY * fSinZ + fSinX * fCosZ;
264 aMatrix.Line3.Column3 = fCosX * fCosY;
266 aDiaProp.setPropertyValue( "D3DTransformMatrix", aMatrix );
268 // add a red light source
270 // in a chart by default only the second (non-specular) light source is switched on
271 // light source 1 is a specular light source
272 aDiaProp.setPropertyValue( "D3DSceneLightColor1", Integer.valueOf( 0xff3333 ));
274 // set direction
275 com.sun.star.drawing.Direction3D aDirection = new com.sun.star.drawing.Direction3D();
277 aDirection.DirectionX = -0.75;
278 aDirection.DirectionY = 0.5;
279 aDirection.DirectionZ = 0.5;
281 aDiaProp.setPropertyValue( "D3DSceneLightDirection1", aDirection );
282 aDiaProp.setPropertyValue( "D3DSceneLightOn1", Boolean.TRUE);
287 // private members
290 private final XChartDocument maChartDocument;
291 private final XDiagram maDiagram;
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */