ExcelFile.GetChart Method
Returns a chart from an object position and path. If the object does not contain a chart, it returns null. Note that charts can be first-level objects (in chart sheets), or they can be embedded inside other objects, that can be themselves embedded inside other objects. So you need to recursively look inside all objects to see if there are charts anywhere.
Look at the example in this topic to see how to get all charts in a sheet.
Syntax
Namespace: FlexCel.Core
public abstract ExcelChart GetChart(Int32 objectIndex, String objectPath)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
objectIndex | Int32 | Index of the object (1-based) | |
objectPath | String | Index to the child object where the chart is. If it is a simple object, you can use String.Empty here, if not you need to get the ObjectPath from GetObjectProperties(Int32, Boolean) If it is "absolute"(it starts with "\\"), then the path includes the objectIndex, and the objectIndex is not used. An object path of "\\1\\2\\3" is exactly the same as using objectIndex = 1 and objectPath = "2\\3" |
Examples
The following example will retrieve all charts that are inserted as objects in a sheet, along with the chart sheets.
public void ProcessAllCharts()
{
XlsFile xls = new XlsFile(true);
xls.Open("filewithcharts.xls");
for (int iSheet = 1; iSheet <= xls.SheetCount; iSheet++)
{
xls.ActiveSheet = iSheet;
//Process charts embedded as objects in worksheets.
for (int iChart = 1; iChart <= xls.ObjectCount; iChart++)
{
TShapeProperties props = xls.GetObjectProperties(iChart, true);
ProcessChart(xls, iChart, props); //We need to process it even if it is not a chart, since it might be a group with a chart inside.
}
//Process chart sheets.
if (xls.SheetType == TSheetType.Chart)
{
ExcelChart chart = xls.GetChart(1, null);
DoSomething(xls, chart);
}
}
xls.Save("changedfilewithcharts.xls");
}
private void ProcessChart(ExcelFile xls, int iChart, TShapeProperties props)
{
if (props.ObjectType == TObjectType.Chart)
{
ExcelChart chart = xls.GetChart(iChart, props.ObjectPath);
DoSomething(xls, chart);
}
for (int i = 1; i <= props.ChildrenCount; i++)
{
TShapeProperties childProp = props.Children(i);
ProcessChart(xls, i, childProp);
}
}