Using Charts in Spreadsheets

From Apache OpenOffice Wiki
< Documentation‎ | BASIC Guide
Revision as of 07:38, 3 September 2008 by Ccornell (Talk | contribs)

Jump to: navigation, search


Charts within spreadsheets can display the data from an assigned cell range within the spreadsheet. Any modifications made to the data within the spreadsheet will also be reflected in the assigned chart. The following example shows how to create a chart assigned to some cell ranges within a spreadsheet document:

Dim Doc As Object
Dim Charts As Object
Dim Chart as Object
Dim Rect As New com.sun.star.awt.Rectangle
Dim RangeAddress(0) As New com.sun.star.table.CellRangeAddress
 
Doc = StarDesktop.CurrentComponent
Charts = Doc.Sheets(0).Charts
 
Rect.X = 8000
Rect.Y = 1000
Rect.Width = 10000
Rect.Height = 7000
RangeAddress(0).Sheet = 0
RangeAddress(0).StartColumn = 0 
RangeAddress(0).StartRow = 0
RangeAddress(0).EndColumn = 2
RangeAddress(0).EndRow = 12
 
Charts.addNewByName("MyChart", Rect, RangeAddress(), True, True)

Although the code used in the example may appear to be complex, the central processes are limited to three lines. The first central line creates the Doc document variable, which references the current spreadsheet document (Doc line = StarDesktop.CurrentComponent). The code used in the example then creates a list containing all charts of the first spreadsheet (Charts line = Doc.Sheets(0).Charts). Finally, in the last line, a new chart is added to this list using the addNewByName method. This new chart is then visible to the user. The variable RangeAddress determines the assigned cell range whose data will be displayed within the chart. The variable Rect determines the position and size of the chart within the first sheet in the spreadsheet document.

The previous example creates a bar chart. If a different chart type is needed, then the bar chart must be explicitly replaced:

Chart = Charts.getByName("MyChart").embeddedObject
Chart.Diagram = Chart.createInstance("com.sun.star.chart.LineDiagram")

The first line defines the corresponding chart object. The second line replaces the current chart with a new one — in this example, a line chart.

Template:Documentation/Note

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools