Contained WithinFind More DocumentationFeatured Support Resources | Download this book in PDF (1223 KB)
Chapter 9 Diagrams (Charts)StarOffice can display data as a diagram, which creates graphical links between data in the form of bars, pie charts, lines or other elements. Data can either be displayed as 2D or 3D graphics, and the appearance of the diagram elements can be individually adapted in a similar way to the process used for drawing elements. If the data is available in the form of a spreadsheet, then this can be dynamically linked to the diagram. Any modifications made to the basic data can in this instance be seen immediately in the assigned diagram. This chapter provides an overview of the programming interface for diagram modules of StarOffice and focuses on the use of diagrams within spreadsheet documents. Using Diagrams in SpreadsheetsDiagrams are not treated as independent documents in StarOffice, but as objects that are embedded in an existing document. While diagrams 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 diagrams. The following example explains the interaction between spreadsheet document and diagram: 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 diagram. If a different type of graphic is needed, then the bar diagram 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 diagram with a new one — in this example, a line diagram. 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 StarOffice Basic, because charts in StarOffice 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. The Structure of DiagramsThe structure of a diagram — and therefore the list of services and interfaces supported by it — depends on its type. The methods and properties of the Z-axis, are, for example, only available in 3D diagrams, but not in 2D diagrams. In pie charts, there are no interfaces for working with axes. The Individual Elements of a DiagramTitle, Sub-title and KeyA title, sub-title and key form part of the basic elements of every diagram. Diagrams provide their own objects for each of these elements. The Chart object provides the following properties for administrating these elements:
In many respects, the elements specified correspond to a drawing element. This is due to the fact that both the com.sun.star.chart.ChartTitle service and the com.sun.star.chart.ChartLegendPosition support the com.sun.star.drawing.Shape service, which forms the technical program basis for drawing elements. Users therefore have the opportunity to determine the position and size of the element using the Size and Position properties. The other fill and line properties (com.sun.star.drawing.FillProperties and com.sun.star.drawing.LineStyle services) as well as the character properties (com.sun.star.style.CharacterProperties service) are provided for formatting the elements. com.sun.star.chart.ChartTitle contains not only the named format properties, but also two other properties:
The diagram key (com.sun.star.chart.ChartLegend service) contains the following additional property:
The following example creates a diagram and assigns it the title "Test", the sub-title "Test 2" and a key. The key has a gray background color, is placed at the bottom of the diagram, and has a character size of 7 points. 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
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
Doc = StarDesktop.CurrentComponent
Charts = Doc.Sheets(0).Charts
Charts.addNewByName("MyChart", Rect, RangeAddress(), True, True)
Chart = Charts.getByName("MyChart").EmbeddedObject
Chart.HasMainTitle = True
Chart.Title.String = "Test"
Chart.HasSubTitle = True
Chart.Subtitle.String = "Test 2"
Chart.HasLegend = True
Chart.Legend.Alignment = com.sun.star.chart.ChartLegendPosition.BOTTOM
Chart.Legend.FillStyle = com.sun.star.drawing.FillStyle.SOLID
Chart.Legend.FillColor = RGB(210, 210, 210)
Chart.Legend.CharHeight = 7
BackgroundEvery diagram has a background area. Every area has an object, which can be accessed using the following properties of the diagram object: The background of a diagram covers its complete area, including the area under the title, sub-title and diagram key. The associated com.sun.star.chart.ChartArea service supports line and fill properties and provides no more extensive properties. Diagram Walls and FloorsAlthough the diagram background covers the entire area of the diagram, the diagram back wall is limited to the area directly behind the data area. Two diagram walls usually exist for 3D diagrams: one behind the data area and one as the left-hand demarcation to the Y-axis. 3D diagrams usually also have a floor.
The specified objects support the com.sun.star.chart.ChartArea service, which in turn provides the usual fill and line properties (com.sun.star.drawing.FillProperties and com.sun.star.drawing.LineStyle services, refer to Chapter 8, Drawings and Presentations). The diagram walls and floor are accessed through the Chart object, which in turn is part of the Chart object: Chart.Area.FillBitmapName = "Sky" The following example shows how graphics (named Sky) already contained in StarOffice can be used as a background to a diagram. 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
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
Doc = StarDesktop.CurrentComponent
Charts = Doc.Sheets(0).Charts
Charts.addNewByName("MyChart", Rect, RangeAddress(), True, True)
Chart = Charts.getByName("MyChart").EmbeddedObject
Chart.Area.FillStyle = com.sun.star.drawing.FillStyle.BITMAP
Chart.Area.FillBitmapName = "Sky"
Chart.Area.FillBitmapMode = com.sun.star.drawing.BitmapMode.REPEAT
AxesStarOffice recognizes five different axes that can be used in a diagram. In the simplest scenario, these are the X and Y-axes. When working with 3D diagrams, a Z-axis is also sometimes provided. For diagrams in which the values of the various rows of data deviate significantly from one another, StarOffice provides a second X and Y-axis for second scaling operations. First X, Y and Z-AxisIn addition to the actual axis, for each of the first X, Y, and Z-axes there can also be a title, a description, a grid, and an auxiliary grid. There is an option for displaying and concealing all of these elements. The diagram object provides the following properties for administration of these features (taking the example of a X-axis; properties for Y and Z-axis are structured in the same way):
Second X and Y-AxisThe following properties are available for the second X and Y-axes (properties taking example of the second X-axis):
Properties of the AxesThe axis objects of a StarOffice diagram support the com.sun.star.chart.ChartAxis service. In addition to the properties for characters (com.sun.star.style.CharacterProperties service, refer to Chapter 6, Text Documents) and lines (com.sun.star.drawing.LineStyle service, refer to Chapter 8, Drawings and Presentations), it provides the following properties:
Properties of the axis gridThe object for the axis grid is based on the com.sun.star.chart.ChartGrid service, which in turn supports the line properties of the com.sun.star.drawing.LineStyle support service (refer to Chapter 8, Drawings and Presentations). Properties of the axis titleThe objects for formatting the axis title are based on the com.sun.star.chart.ChartTitle service, which is also used for diagram titles. ExampleThe following example creates a line diagram. The color for the rear wall of the diagram is set to white. Both the X and Y-axes have a gray auxiliary grid for visual orientation. The minimum value of the Y-axis is fixed to 0 and the maximum value is fixed to 100 so that the resolution of the diagram is retained even if the values are changed. 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)
Chart = Charts.getByName("MyChart").embeddedObject
Chart.Diagram = Chart.createInstance("com.sun.star.chart.LineDiagram")
Chart.Diagram.Wall.FillColor = RGB(255, 255, 255)
Chart.Diagram.HasXAxisGrid = True
Chart.Diagram.XMainGrid.LineColor = RGB(192, 192, 192)
Chart.Diagram.HasYAxisGrid = True
Chart.Diagram.YMainGrid.LineColor = RGB(192, 192, 192)
Chart.Diagram.YAxis.Min = 0
Chart.Diagram.YAxis.Max = 100
3D DiagramsMost diagrams in StarOffice can also be displayed with 3D graphics. All diagram types that provide this option support the com.sun.star.chart.Dim3DDiagram. service. The service provides just one property: Stacked DiagramsStacked diagrams are diagrams that are arranged with several individual values on top of one another to produce a total value. This view shows not only the individual values, but also an overview of all the values. In StarOffice, various types of diagrams can be displayed in a stacked form. All of these diagrams support the com.sun.star.chart.StackableDiagram service, which in turn provides the following properties: Diagram TypesLine DiagramsLine diagrams (Service com.sun.star.chart.LineDiagram) support one X-axis, two Y-axes and one Z-axis. They can be displayed as 2D or 3D graphics (com.sun.star.chart.Dim3Ddiagram service). The lines can be stacked (com.sun.star.chart.StackableDiagram). Line diagrams provide the following properties:
Area DiagramsArea diagrams (com.sun.star.chart.AreaDiagram service) support one X-axis, two Y-axes and one Z-axis. They can be displayed as 2D or 3D graphics (com.sun.star.chart.Dim3Ddiagram service). The areas can be stacked (com.sun.star.chart.StackableDiagram). Bar DiagramsBar diagrams (Service com.sun.star.chart.BarDiagram) support one X-axis, two Y-axes and one Z-axis. They can be displayed as 2D or 3D graphics (com.sun.star.chart.Dim3Ddiagram service). The bars can be stacked (com.sun.star.chart.StackableDiagram). They provide the following properties:
Pie DiagramsPie diagrams (com.sun.star.chart.PieDiagram service) do not contain any axes and cannot be stacked. They can be displayed as 2D or 3D graphics (com.sun.star.chart.Dim3Ddiagram service). |