A collection of all the chart objects on a worksheet.
Property | Type | Description |
---|---|---|
count | int | Returns the number of charts in the worksheet. Read-only. |
items | Chart[] | A collection of chart objects. Read-only. |
None
Method | Return Type | Description |
---|---|---|
add(type: string, sourceData: string, seriesBy: string) | Chart | Creates a new chart. |
getItem(name: string) | Chart | Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned. |
getItemAt(index: number) | Chart | Gets a chart based on its position in the collection. |
load(param: object) | void | Fills the proxy object created in JavaScript layer with property and object values specified in the parameter. |
Creates a new chart.
chartCollectionObject.add(type, sourceData, seriesBy);
Parameter | Type | Description |
---|---|---|
type | string | Represents the type of a chart. Possible values are: ColumnClustered, ColumnStacked, ColumnStacked100, BarClustered, BarStacked, BarStacked100, LineStacked, LineStacked100, LineMarkers, LineMarkersStacked, LineMarkersStacked100, PieOfPie, etc. |
sourceData | string | The address or name of the range that contains the source data. If an address or a worksheet-scoped name is used, it must include the worksheet name (e.g. "Sheet1!A5:B9"). |
seriesBy | string | Optional. Specifies the way columns or rows are used as data series on the chart. Possible values are: Auto, Columns, Rows |
Add a chart of chartType
"ColumnClustered" on worksheet "Charts" with sourceData
from Range "A1:B4" and seriresBy
is set to be "auto".
var sheetName = "Sheet1";
var sourceData = sheetName + "!" + "A1:B4";
var ctx = new Excel.RequestContext();
var chart = ctx.workbook.worksheets.getItem(sheetName).charts.add("ColumnClustered", sourceData, "auto");
ctx.executeAsync().then(function () {
Console.log("New Chart Added");
});
Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned.
chartCollectionObject.getItem(name);
Parameter | Type | Description |
---|---|---|
name | string | Name of the chart to be retrieved. |
var ctx = new Excel.RequestContext();
var chartname = 'Chart1';
var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem(chartname);
ctx.executeAsync().then(function () {
Console.log(chart.height);
});
Gets a chart based on its position in the collection.
chartCollectionObject.getItemAt(index);
Parameter | Type | Description |
---|---|---|
index | number | Index value of the object to be retrieved. Zero-indexed. |
var ctx = new Excel.RequestContext();
var lastPosition = ctx.workbook.worksheets.getItem("Sheet1").charts.count - 1;
var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItemAt(lastPosition);
ctx.executeAsync().then(function () {
Console.log(chart.name);
});
Fills the proxy object created in JavaScript layer with property and object values specified in the parameter.
object.load(param);
Parameter | Type | Description |
---|---|---|
param | object | Optional. Accepts parameter and relationship names as delimited string or an array. Or, provide loadOption object. |
void
var ctx = new Excel.RequestContext();
var charts = ctx.workbook.worksheets.getItem("Sheet1").charts;
ctx.load(charts);
ctx.executeAsync().then(function () {
for (var i = 0; i < charts.items.length; i++)
{
Console.log(charts.items[i].name);
Console.log(charts.items[i].index);
}
});
Get the number of charts
var ctx = new Excel.RequestContext();
var charts = ctx.workbook.worksheets.getItem("Sheet1").charts;
ctx.load(charts);
ctx.executeAsync().then(function () {
Console.log("charts: Count= " + charts.count);
});