Difference between revisions of "Documentation/BASIC Guide/Charts in Spreadsheets"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 13: Line 13:
 
While charts in text and drawing documents remain isolated from the content of the document, when used in spreadsheet documents, a mechanism is provided which allows a link to be established between the document data and embedded charts. The following example explains the interaction between spreadsheet document and chart:
 
While charts in text and drawing documents remain isolated from the content of the document, when used in spreadsheet documents, a mechanism is provided which allows a link to be established between the document data and embedded charts. The following example explains the interaction between spreadsheet document and chart:
  
Dim Doc As Object
+
<source lang="oobas">
Dim Charts As Object
+
Dim Doc As Object
Dim Chart as Object
+
Dim Charts As Object
Dim Rect As New com.sun.star.awt.Rectangle
+
Dim Chart as Object
Dim RangeAddress(0) As New com.sun.star.table.CellRangeAddress
+
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
+
Doc = StarDesktop.CurrentComponent
+
Charts = Doc.Sheets(0).Charts
Rect.X = 8000
+
 
Rect.Y = 1000
+
Rect.X = 8000
Rect.Width = 10000
+
Rect.Y = 1000
Rect.Height = 7000
+
Rect.Width = 10000
RangeAddress(0).Sheet = 0
+
Rect.Height = 7000
RangeAddress(0).StartColumn = 0  
+
RangeAddress(0).Sheet = 0
RangeAddress(0).StartRow = 0
+
RangeAddress(0).StartColumn = 0  
RangeAddress(0).EndColumn = 2
+
RangeAddress(0).StartRow = 0
RangeAddress(0).EndRow = 12
+
RangeAddress(0).EndColumn = 2
+
RangeAddress(0).EndRow = 12
Charts.addNewByName("MyChart", Rect, RangeAddress(), True, True)
+
 
 +
Charts.addNewByName("MyChart", Rect, RangeAddress(), True, True)
 +
</source>
  
 
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 <tt>Doc</tt> document variable, which references the current spreadsheet document (<tt>Doc</tt> line = <tt>StarDesktop.CurrentComponent</tt>). The code used in the example then creates a list containing all charts of the first spreadsheet (<tt>Charts</tt> line = <tt>Doc.Sheets(0).Charts</tt>). Finally, a new chart is added to the last line of this list using the <tt>addNewByName</tt> method. This new chart is then visible to the user.
 
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 <tt>Doc</tt> document variable, which references the current spreadsheet document (<tt>Doc</tt> line = <tt>StarDesktop.CurrentComponent</tt>). The code used in the example then creates a list containing all charts of the first spreadsheet (<tt>Charts</tt> line = <tt>Doc.Sheets(0).Charts</tt>). Finally, a new chart is added to the last line of this list using the <tt>addNewByName</tt> method. This new chart is then visible to the user.
Line 40: Line 42:
 
The previous example creates a bar chart. If a different type of graphic is needed, then the bar chart must be explicitly replaced:
 
The previous example creates a bar chart. If a different type of graphic is needed, then the bar chart must be explicitly replaced:
  
Chart = Charts.getByName("MyChart").embeddedObject
+
<source lang="oobas">
Chart.Diagram = Chart.createInstance("com.sun.star.chart.LineDiagram")
+
Chart = Charts.getByName("MyChart").embeddedObject
 +
Chart.Diagram = Chart.createInstance("com.sun.star.chart.LineDiagram")
 +
</source>
  
 
The first lines defines the corresponding chart object. The second line replaces the current chart with a new one — in this example, a line chart.
 
The first lines defines the corresponding chart object. The second line replaces the current chart with a new one — in this example, a line chart.

Revision as of 12:03, 3 April 2008

Template:Documentation/NeedsRework


Charts are not treated as independent documents in Apache OpenOffice, but as objects that are embedded in an existing document.

While charts in text and drawing documents remain isolated from the content of the document, when used in spreadsheet documents, a mechanism is provided which allows a link to be established between the document data and embedded charts. The following example explains the interaction between spreadsheet document and chart:

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, a new chart is added to the last line of this list using the addNewByName method. This new chart is then visible to the user.

The last line initializes the Rect and RangeAddress auxiliary structures, which the addNewByName method also provides as a parameter. Rect determines the position of the chart within the spreadsheet. RangeAddress determines the range whose data is to be linked to the chart.

The previous example creates a bar chart. If a different type of graphic 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 lines 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