OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Exporting a chart from a spreadsheet in JPEG format...

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
srawillliams
Newbie
Newbie


Joined: 09 Jun 2004
Posts: 1

PostPosted: Wed Jun 09, 2004 5:27 am    Post subject: Exporting a chart from a spreadsheet in JPEG format... Reply with quote

I am using OpenOffice server-side to create a spreadsheet and associated chart. I need to render the chart as a jpeg or gif but cannot figure out how to do this.

I thought that as each sheet exposes an associated XDrawPage, I would be able to use this with a GraphicExportFilter. This, however, does not appear to work.

Any ideas

[/list]
Back to top
View user's profile Send private message
David
Super User
Super User


Joined: 24 Oct 2003
Posts: 5668
Location: Canada

PostPosted: Wed Jun 09, 2004 10:07 am    Post subject: Re: Exporting a chart from a spreadsheet in JPEG format... Reply with quote

srawillliams wrote:
I am using OpenOffice server-side to create a spreadsheet and associated chart. I need to render the chart as a jpeg or gif but cannot figure out how to do this.

I thought that as each sheet exposes an associated XDrawPage, I would be able to use this with a GraphicExportFilter. This, however, does not appear to work.

Any ideas

[/list]


Use a screen capture program; i do it constantly and put the result into a wordprocessor, then print to a pdf, depending on need. Some programs like PaintShop Pro have one, but a great stadalone is called simply "capture".

Look here for a selection; my choice for reasonable quality is "capture" a bit down the page.

David.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Jun 09, 2004 1:40 pm    Post subject: Reply with quote

The following program....
1. Creates a new spreadsheet
2. Puts some glowing sales figures into it (see the chart!)
3. Creates a chart of those sales figures
4. Creates a new drawing document.
5. Select/Copy/Paste the chart into the Drawing
6. Export the drawing to a JPEG.

This is a standalone example, ready to run. Be sure the change the first line.

Code:

Sub Main
   ' CHANGE ME !!!
   cJpegChartFile = "C:\Documents and Settings\dbrewer\Desktop\Chart.jpg"
   
   
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, Array() )
   oSheet = oDoc.getSheets().getByIndex( 0 ) ' get the zero'th sheet

   ' Put some sales figures onto the sheet.
   oSheet.getCellByPosition( 0, 0 ).setString( "Month" )
   oSheet.getCellByPosition( 1, 0 ).setString( "Sales" )
   
   oSheet.getCellByPosition( 0, 1 ).setString( "Jan" )
   oSheet.getCellByPosition( 0, 2 ).setString( "Feb" )
   oSheet.getCellByPosition( 0, 3 ).setString( "Mar" )
   oSheet.getCellByPosition( 0, 4 ).setString( "Apr" )
   oSheet.getCellByPosition( 0, 5 ).setString( "May" )
   oSheet.getCellByPosition( 0, 6 ).setString( "Jun" )
   oSheet.getCellByPosition( 0, 7 ).setString( "Jul" )
   oSheet.getCellByPosition( 0, 8 ).setString( "Aug" )
   oSheet.getCellByPosition( 0, 9 ).setString( "Sep" )
   oSheet.getCellByPosition( 0, 10 ).setString( "Oct" )
   oSheet.getCellByPosition( 0, 11 ).setString( "Nov" )
   oSheet.getCellByPosition( 0, 12 ).setString( "Dec" )
   
   oSheet.getCellByPosition( 1, 1 ).setValue( 3826.37 )
   oSheet.getCellByPosition( 1, 2 ).setValue( 3504.21 )
   oSheet.getCellByPosition( 1, 3 ).setValue( 2961.45 )
   oSheet.getCellByPosition( 1, 4 ).setValue( 2504.12 )
   oSheet.getCellByPosition( 1, 5 ).setValue( 2713.98 )
   oSheet.getCellByPosition( 1, 6 ).setValue( 2248.17 )
   oSheet.getCellByPosition( 1, 7 ).setValue( 1802.13 )
   oSheet.getCellByPosition( 1, 8 ).setValue( 2003.22 )
   oSheet.getCellByPosition( 1, 9 ).setValue( 1502.54 )
   oSheet.getCellByPosition( 1, 10 ).setValue( 1207.68 )
   oSheet.getCellByPosition( 1, 11 ).setValue( 1319.71 )
   oSheet.getCellByPosition( 1, 12 ).setValue( 786.03 )

   
   '-----
   ' Now add a chart to the spreadsheet.
   
   oCellRangeAddress = oSheet.getCellRangeByName( "A1:B13" ).getRangeAddress()
'   oCellRangeAddress = MakeCellRangeAddress( 0, 0, 1, 1, 12 )
   ' Get the collection of charts from the sheet.
   oCharts = oSheet.getCharts()
   ' Add a new chart with a specific name,
   '  in a specific rectangle on the drawing page,
   '  and connected to specific cells of the spreadsheet.
   oCharts.addNewByName( "Sales",_
               MakeRectangle( 8000, 1000, 16000, 10000 ),_
               Array( oCellRangeAddress ),_
               True, True )
   ' From the collection of charts, get the new chart we just created.
   oChart = oCharts.getByName( "Sales" )
   ' Get the chart document model.
   oChartDoc = oChart.getEmbeddedObject()
   
   ' Get the drawing text shape of the title of the chart.
   oTitleTextShape = oChartDoc.getTitle()
   ' Change the title.
   oTitleTextShape.String = "Sales Chart"
   
   ' Create a diagram.
   oDiagram = oChartDoc.createInstance( "com.sun.star.chart.BarDiagram" )
   ' Set its parameters.
   oDiagram.Vertical = True
   ' Make the chart use this diagram.
   oChartDoc.setDiagram( oDiagram )
   
   ' Ask the chart what diagram it is using.
   ' (Unnecessary, since variable oDiagram already contains this value.)
   oDiagram = oChartDoc.getDiagram()
   ' Make more changes to the diagram.
   oDiagram.DataCaption = com.sun.star.chart.ChartDataCaption.VALUE
   oDiagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.COLUMNS
   '
   '-----
   
   
   ' Get one of the document's controllers.
   oDocCtrl = oDoc.getCurrentController()
   
   lFound = False
   
   ' Search the draw page for the chart.
   oSheet = oDoc.getSheets().getByIndex( 0 )
   oDrawPage = oSheet.getDrawPage()
   nNumShapes = oDrawPage.getCount()
   For i = 0 to nNumShapes-1
      oShape = oDrawPage.getByIndex( i )
      ' Can't call supportsService unless the com.sun.star.lang.XServiceInfo is present.
      If HasUnoInterfaces( oShape, "com.sun.star.lang.XServiceInfo" ) Then
         If oShape.supportsService( "com.sun.star.drawing.OLE2Shape" ) Then
'            oText.insertString( oCursor, oShape.CLSID, False )
            ' Is it a Chart?
            If oShape.CLSID = "12DCAE26-281F-416F-a234-c3086127382e" Then
               ' Select the chart shape.
               oDocCtrl.select( oShape )
               lFound = True
            EndIf
         EndIf
      EndIf
   Next
   
   If lFound Then
      oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )

      oDocFrame = oDocCtrl.getFrame()
      oDispatchHelper.executeDispatch( oDocFrame, ".uno:Copy", "", 0, Array() )
      
      oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
      oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
      oDrawDocCtrl = oDrawDoc.getCurrentController()
      oDrawDocFrame = oDrawDocCtrl.getFrame()
      oDispatchHelper.executeDispatch( oDrawDocFrame, ".uno:Paste", "", 0, Array() )
      
      oDrawDoc.storeToURL( ConvertToURL( cJpegChartFile ), _
            Array( MakePropertyValue( "FilterName", "draw_jpg_Export" ) ) )
   EndIf
End Sub


'----------
'   Create and return a new com.sun.star.beans.PropertyValue.
'
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function


'----------
'   Create and return a new com.sun.star.awt.Rectangle.
'
Function MakeRectangle( nX, nY, nWidth, nHeight ) As com.sun.star.awt.Rectangle
   oRectangle = createUnoStruct( "com.sun.star.awt.Rectangle" )
   With oRectangle
      .X = nX
      .Y = nY
      .Width = nWidth
      .Height = nHeight
   End With
   MakeRectangle() = oRectangle
End Function


'----------
'   Create and return a new com.sun.star.table.CellRangeAddress.
'
Function MakeCellRangeAddress( nSheetIndex, nStartColumn, nStartRow, nEndColumn, nEndRow ) As com.sun.star.table.CellRangeAddress
   oCellRangeAddress = createUnoStruct( "com.sun.star.table.CellRangeAddress" )
   With oCellRangeAddress
      .Sheet = nSheetIndex
      .StartColumn = nStartColumn
      .StartRow = nStartRow
      .EndColumn = nEndColumn
      .EndRow = nEndRow
   End With
   MakeCellRangeAddress() = oCellRangeAddress
End Function

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
srawilllliams
Guest





PostPosted: Mon Jun 14, 2004 12:08 am    Post subject: Reply with quote

Thanks DannyB, Just got a java version of your code working ... will post up once i've got it cleaned up a bit
Back to top
srawilliams
Guest





PostPosted: Mon Jun 21, 2004 2:53 am    Post subject: java version Reply with quote

public void exportChart(
XSpreadsheetDocument document,
String chartSheetName,
String fileFormat,
String fileUrl,
XMultiServiceFactory multiServiceFactory
) throws Exception {




//need a draw document
XComponent drawDocument = null;

try {

//get document model
XModel model = (XModel) UnoRuntime.queryInterface(XModel.class, document);

//get collection of sheets belonging to document
XSpreadsheets sheets = document.getSheets();

//get name access reference
XNameContainer sheetsWithNameAccess = (XNameContainer) UnoRuntime.
queryInterface(XNameContainer.class, sheets);

//get the chart sheet
XSpreadsheet chartSheet = (XSpreadsheet) UnoRuntime.queryInterface(
XSpreadsheet.class, sheets.getByName(chartSheetName));

if (chartSheet != null) {

//get draw page supplier for chart sheet
XDrawPageSupplier calcDrawPageSupplier =
(XDrawPageSupplier)
UnoRuntime.queryInterface(XDrawPageSupplier.class, chartSheet);

//get draw page
XDrawPage calcDrawPage = calcDrawPageSupplier.getDrawPage();
int numberOfShapes = calcDrawPage.getCount();

if (calcDrawPage != null && numberOfShapes > 0) {

com.sun.star.drawing.XShape shape = null;
XPropertySet shapeProperties = null;
String classId = null;

//need the doucment controller
XController calcController
= model.getCurrentController();

XSelectionSupplier calcSelectionSupplier = (XSelectionSupplier)
UnoRuntime.queryInterface(XSelectionSupplier.class,
calcController);

boolean found = false;
findShape:for (int i = 0; i < numberOfShapes; i++) {

shape = (XShape) UnoRuntime.queryInterface
(XShape.class, calcDrawPage.getByIndex(i));

shapeProperties = (XPropertySet) UnoRuntime.
queryInterface(XPropertySet.class, shape);

classId = (String) shapeProperties.getPropertyValue("CLSID");

if (classId != null &&
classId.toLowerCase().equals(CHART_CLASS_ID.toLowerCase())) {

calcSelectionSupplier.select(shape);
found = true;
break findShape;
}

}


if(found) {
//get a dispatch helper object/service
Object dispatchHelperObject =
multiServiceFactory.createInstance
("com.sun.star.frame.DispatchHelper");

com.sun.star.frame.XDispatchHelper dispatchHelper =
(com.sun.star.frame.XDispatchHelper) UnoRuntime.queryInterface(
com.sun.star.frame.XDispatchHelper.class, dispatchHelperObject);

//get the XFrame for the calc doc
com.sun.star.frame.XFrame calcDocFrame = calcController.getFrame();

XDispatchProvider calcDispatchProvider = (XDispatchProvider)
UnoRuntime.queryInterface(XDispatchProvider.class, calcDocFrame);

PropertyValue[] dispatchPropertyValues = new PropertyValue[0];

dispatchHelper.executeDispatch(calcDispatchProvider,
".uno:Copy", "", 0,
dispatchPropertyValues);

//create a draw document
XComponentLoader loader = (XComponentLoader)
UnoRuntime.queryInterface(
XComponentLoader.class,
multiServiceFactory.createInstance("com.sun.star.frame.Desktop"));

//hide the document by setting the Hidden property
PropertyValue[] drawDocProperties = new PropertyValue[1];
PropertyValue aValue = new PropertyValue();
aValue.Name = "Hidden";
aValue.Value= new Boolean(true);
drawDocProperties[0] = aValue;

drawDocument = (XComponent)
UnoRuntime.queryInterface(XComponent.class,
loader.loadComponentFromURL(
"private:Factory/sdraw",
"_blank", 0, drawDocProperties
));

XDrawPagesSupplier drawDrawPagesSupplier =
(XDrawPagesSupplier) UnoRuntime.queryInterface(
XDrawPagesSupplier.class, drawDocument);

//get the first drawpage
XDrawPages drawDrawPages = drawDrawPagesSupplier.getDrawPages();
Object drawPageObject = drawDrawPages.getByIndex(0);
XDrawPage drawDrawPage = (XDrawPage) UnoRuntime.queryInterface
(XDrawPage.class, drawPageObject);


//get the draw document model
XModel drawDocModel =
(XModel) UnoRuntime.queryInterface(XModel.class, drawDocument);

//get draw documet controller
XController drawDocController = drawDocModel.getCurrentController();

//get the frame for the draw doc
XFrame drawDocFrame = drawDocController.getFrame();

//paste into draw document
XDispatchProvider drawDispatchProvider = (XDispatchProvider)
UnoRuntime.queryInterface(XDispatchProvider.class, drawDocFrame);

dispatchHelper.executeDispatch(drawDispatchProvider,
".uno:Paste", "", 0,
new PropertyValue[0]);

//get an export filter object
Object GraphicExportFilter = multiServiceFactory.createInstance
("com.sun.star.drawing.GraphicExportFilter");
XExporter exporter = (XExporter)
UnoRuntime.queryInterface(XExporter.class, GraphicExportFilter);

PropertyValue[] exportProperties = new PropertyValue[2];
for (int i = 0; i < exportProperties.length; i++) {
exportProperties[i] = new PropertyValue();
}

//use an export filter
exportProperties[0] = new PropertyValue();
exportProperties[0].Name = "MediaType";
//exportProperties[0].Value = "image/gif";
exportProperties[0].Value = fileFormat;

exportProperties[1].Name = "URL";
exportProperties[1].Value = fileUrl;

//export the chart
XDrawPagesSupplier drawPagesSupplier =
(XDrawPagesSupplier) UnoRuntime.queryInterface(
XDrawPagesSupplier.class, drawDocument);

//get collection of draw pages
XDrawPages drawPages = drawPagesSupplier.getDrawPages();

//get first draw page
XDrawPage drawPage = (XDrawPage) UnoRuntime.queryInterface(
XDrawPage.class,
drawPages.getByIndex(0));

Object shapeAsObject = drawPage.getByIndex(0);
XShape chartShape = (XShape) UnoRuntime.queryInterface(
XShape.class, shapeAsObject);


XComponent pageToExport = (XComponent)
UnoRuntime.queryInterface(XComponent.class, chartShape);

exporter.setSourceDocument(pageToExport);

XFilter drawFilter = (XFilter)
UnoRuntime.queryInterface(XFilter.class, exporter);
drawFilter.filter(exportProperties);
} else {
throw new Exception("Chart shape could not be found. ");
}

}
} else {
throw new Exception("Chart sheet could not be found. ");
}
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("Error exporting chart :" + e.getMessage());
}
finally {
if(drawDocument != null) {
//kill off the local draw document
drawDocument.dispose();
}
}
}
Back to top
leofantasy
Newbie
Newbie


Joined: 05 Mar 2009
Posts: 1

PostPosted: Thu Mar 12, 2009 8:02 pm    Post subject: Reply with quote

Have you tested the java code?
I run the codes, it doesn't successful!
Back to top
View user's profile Send private message
mihel
Power User
Power User


Joined: 30 Sep 2005
Posts: 83

PostPosted: Fri Feb 19, 2010 4:25 pm    Post subject: Reply with quote

Java code works perfectly well.
Except for some minor issues.
Many thanks!!!
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group