SFDocuments.Chart service/text/sbasic/shared/03/sf_chart.xhpChart service
SFDocuments.Chart service
The Chart service provides a set of properties and methods to handle charts in Calc documents. With this service it is possible to:Access chart objects in Calc documents and manipulate their properties.Create and insert new charts into a Calc document.Export charts as image files.
Chart names
Charts may have two different names:An internal name given by %PRODUCTNAME as soon as the chart object is created (usually "Object 1", "Object 2" and so on).A user-defined name, which can be defined by right-clicking the chart and choosing in the context menu.The Chart service primarily uses the user-defined name to access a chart object. If it does not exist, than the internal name is used.
Service invocation
Before using the Chart service the ScriptForge library needs to be loaded or imported:The Chart service is instantiated from a Calc service instance either using the Charts or CreateChart methods.The example below creates a Chart service instance from an existing chart in the current Calc document:GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")Dim oDoc as Object, oChart as ObjectSet oDoc = CreateScriptService("Calc")Set oChart = oDoc.Charts("Sheet1", "Object 1")The following example instantiate the Chart service by creating a new chart object based on the data contained in the range "Sheet1.A1:C10".Dim oDoc as Object, oChart as ObjectSet oDoc = CreateScriptService("Calc")Set oChart = oDoc.CreateChart("My Chart", "Sheet1", "Sheet1.A1:C10")Read the CreateChart method description to learn more about its arguments.The examples above can be written in Python as follows:from scriptforge import CreateScriptServicedoc = CreateScriptService("Calc")chart = doc.Charts("Sheet1", "Object 1")doc = CreateScriptService("Calc")chart = doc.CreateChart("My Chart", "Sheet1", "Sheet1.A1:C10")Chart service;ChartTypeChart service;DeepChart service;Dim3DChart service;ExplodedChart service;FilledChart service;LegendChart service;PercentChart service;StackedChart service;TitleChart service;XTitleChart service;YTitleChart service;XChartObjChart service;XDiagramChart service;XShapeChart service;XTableChart
Properties
NameReadonlyTypeDescriptionChartTypeNoStringSpecifies the chart type as a string that can assume one of the following values: "Pie", "Bar", "Donut", "Column", "Area", "Line", "XY", "Bubble", "Net".DeepNoBooleanWhen True indicates that the chart is three-dimensional and each series is arranged in the z-direction.When False series are arranged considering only two dimensions.Dim3DNoBoolean or StringSpecifies if the chart is displayed with 3D elements. If the value is a string, it must be either "Bar", "Cylinder", "Cone" or "Pyramid".If the boolean True value is specified, then the chart is displayed using 3D bars.ExplodedNoNumericSpecifies how much pie segments are offset from the chart center as a percentage of the radius. Applicable to pie and donut charts only.FilledNoBooleanWhen True, specifies a filled net chart. Applicable to net charts only.LegendNoBooleanSpecifies whether or not the chart has a legend.PercentNoBooleanWhen True, chart series are stacked and each category sums up to 100%. Applicable to Area, Bar, Bubble, Column and Net charts.StackedNoBooleanWhen True, chart series are stacked. Applicable to Area, Bar, Bubble, Column and Net charts.TitleNoStringSpecifies the main title of the chart.XTitleNoStringSpecifies the title of the X axis.YTitleNoStringSpecifies the title of the Y axis.XChartObjYesUNO ObjectReturns the object representing the chart, which is an instance of the ScChartObj class.XDiagramYesUNO ObjectReturns the com.sun.star.chart.XDiagram object representing the diagram of the chart.XShapeYesUNO ObjectReturns the com.sun.star.drawing.XShape object representing the shape of the chart.XTableChartYesUNO ObjectReturns the com.sun.star.table.XTableChart object representing the data being displayed in the chart.
Creating a chart
Consider the following data in the range "A1:B6" of a sheet named "Report".
AB1Sample ASample B23640339434454055248
The examples below in Basic and Python show how to create a line chart from this data with legends.oDoc = CreateScriptService("Calc")oChart = oDoc.CreateChart("Samples", "Report", "Report.A1:B6")oChart.ChartType = "Line"oChart.Legend = TrueoChart.Resize(1000, 1000, 25000, 15000)doc = CreateScriptService("Calc")chart = doc.CreateChart("Samples", "Report", "Report.A1:B6")chart.ChartType = "Line"chart.Legend = Truechart.Resize(1000, 1000, 25000, 15000)The chart does not need to be created in the same sheet where the data is located. It can be created in any existing sheet in the current file by specifying the sheet name in the second argument of the CreateChart method.
Methods
List of Methods in the Chart Service
ExportToFile
Resize
Saves the chart as an image file in a specified location. Returns True if the image file could be successfully created.
chart.ExportToFile(filename: str, imagetype: str = "png", overwrite: bool = False): bool
filename: Identifies the path and file name where the image will be saved. It must follow the notation defined in SF_FileSystem.FileNaming.imagetype: The name of the image type to be created. The following values are accepted: "gif", "jpeg", "png" (default), "svg" and "tiff".overwrite: Specifies if the destination file can be overwritten (Default = False).oChart.ExportToFile("C:\Temp\myChart.svg", ImageType := "svg", Overwrite := True)chart.ExportToFile(r"C:\Temp\myChart.svg", imagetype="svg", overwrite=True) Resize ----------------------------------------------------------------------------------------- Chart service;Resize
Resize
Changes the position of the chart in the current sheet and modifies its width and height. Returns True if resizing was successful.
chart.Resize([xpos: int], [ypos: int], [width: int], [height: int]): bool
xpos, ypos: Specify the new X and Y positions of the chart. If any of these values are omitted or if negative values are provided, the corresponding positions are left unchanged.width: Specify the new width of the chart. If this argument is omitted or if a negative value is provided, the chart width is left unchanged.height: Specify the new height of the chart. If this argument is omitted or if a negative value is provided, the chart height is left unchanged.All arguments are provided as integer values that correspond to 1/100 of a millimeter.' Changes only X and Y positionoChart.Rezise(1000, 3000)' Changes only the chart width and heightoChart.Resize(, , 25000, 12500)' Keyword arguments are supportedoChart.Resize(Width := 25000, Height := 12500)chart.Rezise(1000, 3000)chart.Resize(-1, -1, 20000, 20000)chart.Resize(width=25000, height=12500)