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

From Apache OpenOffice Wiki
Jump to: navigation, search
m (ThisComponent)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Documentation/NeedsRework}}
 
 
{{Documentation/BASICGuideTOC/v2
 
{{Documentation/BASICGuideTOC/v2
 
|ShowPrevNext=block
 
|ShowPrevNext=block
Line 8: Line 7:
 
}}
 
}}
 
{{DISPLAYTITLE:Using Charts in Spreadsheets}}
 
{{DISPLAYTITLE:Using Charts in Spreadsheets}}
 +
 +
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:
  
= Using Charts in Spreadsheets =
+
<source lang="oobas">
 +
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
  
Charts are not treated as independent documents in {{OOo}}, but as objects that are embedded in an existing document.
+
Doc = ThisComponent
 +
Charts = Doc.Sheets(0).Charts
  
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:
+
Rect.X = 8000
 
+
Rect.Y = 1000
Dim Doc As Object
+
Rect.Width = 10000
Dim Charts As Object
+
Rect.Height = 7000
Dim Chart as Object
+
RangeAddress(0).Sheet = 0
Dim Rect As New com.sun.star.awt.Rectangle
+
RangeAddress(0).StartColumn = 0  
Dim RangeAddress(0) As New com.sun.star.table.CellRangeAddress
+
RangeAddress(0).StartRow = 0
+
RangeAddress(0).EndColumn = 2
Doc = StarDesktop.CurrentComponent
+
RangeAddress(0).EndRow = 12
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 <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.
+
Charts.addNewByName("MyChart", Rect, RangeAddress(), True, True)
 +
</source>
  
The last line initializes the <tt>Rect</tt> and <tt>RangeAddress</tt> auxiliary structures, which the <tt>addNewByName</tt> method also provides as a parameter. <tt>Rect</tt> determines the position of the chart within the spreadsheet. <tt>RangeAddress</tt> determines the range whose data is to be linked to the chart.
+
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, in the last line, a new chart is added to this list using the <tt>addNewByName</tt> method. This new chart is then visible to the user. The variable <tt>RangeAddress</tt> determines the assigned cell range whose data will be displayed within the chart. The variable <tt>Rect</tt> 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 type of graphic is needed, then the bar chart must be explicitly replaced:
+
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
+
<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 line defines the corresponding chart object. The second line replaces the current chart with a new one — in this example, a line chart.
  
{{Documentation/Note|In Excel, a distinction is made between charts which have been inserted as a separate page in an Excel document and charts which are embedded in a table page. Correspondingly, two different access methods are defined there for charts. This distinction is not made in {{OOo}} Basic, because charts in {{OOo}} Calc are always created as embedded objects of a table page. The charts are always accessed using the <tt>Charts</tt> list of the associated <tt>Sheet</tt> object.}}
+
{{Documentation/VBAnote|In Microsoft Excel, a distinction is made between charts which have been inserted as a separate page in a Microsoft Excel document and charts which are embedded in a table page. Correspondingly, two different access methods are defined there for charts. This distinction is not made in {{OOo}} Basic, because charts in {{OOo}} Calc are always created as embedded objects of a table page. The charts are always accessed using the <tt>Charts</tt> list of the associated <tt>Sheet</tt> object.}}
  
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Charts in Spreadsheets}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 09:14, 1 November 2009


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 = ThisComponent
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.

Documentation note.png VBA : In Microsoft Excel, a distinction is made between charts which have been inserted as a separate page in a Microsoft Excel document and charts which are embedded in a table page. Correspondingly, two different access methods are defined there for charts. This distinction is not made in Apache OpenOffice Basic, because charts in Apache OpenOffice Calc are always created as embedded objects of a table page. The charts are always accessed using the Charts list of the associated Sheet object.


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