Excel.Chart class
表示工作簿中的 chart 对象。 若要了解有关图表对象模型的详细信息,请参阅使用 Excel JavaScript API 处理图表。
属性
| axes | 表示图表坐标轴。 |
| context | 与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。 |
| data |
表示图表上的数据标签。 |
| format | 封装图表区域的格式属性。 |
| height | 指定图表对象的高度(以磅为单位)。 |
| left | 从图表左侧到工作表原点的距离,以磅为单位。 |
| legend | 表示图表的图例。 |
| name | 指定图表对象的名称。 |
| series | 表示单个系列或图表中的系列集合。 |
| title | 表示指定图表的标题,包括标题的文本、可见性、位置和格式。 |
| top | 指定从对象的顶部边缘到工作表) 上 (第 1 行顶部或图表) 上 (图表区域顶部的距离(以磅为单位)。 |
| width | 指定图表对象的宽度(以磅为单位)。 |
| worksheet | 包含当前 chart 的 worksheet 对象。 |
方法
| delete() | 删除 chart 对象。 |
| get |
通过缩放图表以适应指定的维度,将图表呈现为 Base64 编码的图像。 纵横比作为调整大小的一部分保留。 |
| get |
通过缩放图表以适应指定的维度,将图表呈现为 Base64 编码的图像。 纵横比作为调整大小的一部分保留。 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| set(properties, options) | 同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。 |
| set(properties) | 基于现有加载的对象,同时在对象上设置多个属性。 |
| set |
重置图表的源数据。 |
| set |
重置图表的源数据。 |
| set |
相对于工作表上的单元格放置图表。 |
| toJSON() | 覆盖 JavaScript |
属性详细信息
axes
context
dataLabels
表示图表上的数据标签。
readonly dataLabels: Excel.ChartDataLabels;
属性值
注解
format
封装图表区域的格式属性。
readonly format: Excel.ChartAreaFormat;
属性值
注解
height
left
legend
表示图表的图例。
readonly legend: Excel.ChartLegend;
属性值
注解
示例
// Set to show legend of Chart1 and make it on top of the chart.
await Excel.run(async (context) => {
const chart = context.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1");
chart.legend.visible = true;
chart.legend.position = "Top";
chart.legend.overlay = false;
await context.sync()
console.log("Legend Shown ");
});
name
指定图表对象的名称。
name: string;
属性值
string
注解
示例
// Rename the chart to new name, resize the chart to 200 points in both height and weight.
// Move Chart1 to 100 points to the top and left.
await Excel.run(async (context) => {
const chart = context.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1");
chart.name = "New Name";
chart.top = 100;
chart.left = 100;
chart.height = 200;
chart.width = 200;
await context.sync();
});
series
表示单个系列或图表中的系列集合。
readonly series: Excel.ChartSeriesCollection;
属性值
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/10-chart/chart-series.yaml
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const chart: Excel.Chart = sheet.charts.getItemAt(0);
let rangeSelection = sheet.getRange("C2:C7");
let xRangeSelection = sheet.getRange("A1:A7");
// Add a series.
const newSeries: Excel.ChartSeries = chart.series.add("Qtr2");
newSeries.setValues(rangeSelection);
newSeries.setXAxisValues(xRangeSelection);
await context.sync();
});
title
表示指定图表的标题,包括标题的文本、可见性、位置和格式。
readonly title: Excel.ChartTitle;
属性值
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/10-chart/chart-title-format.yaml
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const title = sheet.charts.getItemAt(0).title;
title.textOrientation = -45;
await context.sync();
});
top
width
worksheet
包含当前 chart 的 worksheet 对象。
readonly worksheet: Excel.Worksheet;
属性值
注解
方法详细信息
delete()
删除 chart 对象。
delete(): void;
返回
void
注解
示例
await Excel.run(async (context) => {
const chart = context.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1");
chart.delete();
await context.sync();
});
getImage(width, height, fittingMode)
通过缩放图表以适应指定的维度,将图表呈现为 Base64 编码的图像。 纵横比作为调整大小的一部分保留。
getImage(width?: number, height?: number, fittingMode?: Excel.ImageFittingMode): OfficeExtension.ClientResult<string>;
参数
- width
-
number
可选。 生成的图像的所需宽度。
- height
-
number
可选。 生成的图像的所需高度。
- fittingMode
- Excel.ImageFittingMode
可选。 用于将图表缩放到指定维度的方法 (高度和宽度都设置为) 。
返回
OfficeExtension.ClientResult<string>
注解
示例
await Excel.run(async (context) => {
const chart = context.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1");
const image = chart.getImage();
await context.sync();
});
getImage(width, height, fittingMode)
通过缩放图表以适应指定的维度,将图表呈现为 Base64 编码的图像。 纵横比作为调整大小的一部分保留。
getImage(width?: number, height?: number, fittingMode?: "Fit" | "FitAndCenter" | "Fill"): OfficeExtension.ClientResult<string>;
参数
- width
-
number
可选。 生成的图像的所需宽度。
- height
-
number
可选。 生成的图像的所需高度。
- fittingMode
-
"Fit" | "FitAndCenter" | "Fill"
可选。 用于将图表缩放到指定维度的方法 (高度和宽度都设置为) 。
返回
OfficeExtension.ClientResult<string>
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Excel.Interfaces.ChartLoadOptions): Excel.Chart;
参数
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Excel.Chart;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
示例
// Get a chart named "Chart1".
await Excel.run(async (context) => {
const chart = context.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1");
chart.load('name');
await context.sync();
console.log(chart.name);
});
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Excel.Chart;
参数
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
set(properties, options)
同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。
set(properties: Interfaces.ChartUpdateData, options?: OfficeExtension.UpdateOptions): void;
参数
- properties
- Excel.Interfaces.ChartUpdateData
一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。
- options
- OfficeExtension.UpdateOptions
提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。
返回
void
set(properties)
setData(sourceData, seriesBy)
重置图表的源数据。
setData(sourceData: Range, seriesBy?: Excel.ChartSeriesBy): void;
参数
- sourceData
- Excel.Range
与源数据对应的范围对象。
- seriesBy
- Excel.ChartSeriesBy
指定列或行在图表上用作数据系列的方式。 可以是下列项之一:自动 (默认) 、行和列。 有关详细信息,请参阅 Excel.ChartSeriesBy。
返回
void
注解
示例
// Set the sourceData to be the range at "A1:B4" and seriesBy to be "Columns".
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sheet1");
const chart = sheet.charts.getItem("Chart1");
const sourceData = sheet.getRange("A1:B4");
chart.setData(sourceData, "Columns");
await context.sync();
});
setData(sourceData, seriesBy)
重置图表的源数据。
setData(sourceData: Range, seriesBy?: "Auto" | "Columns" | "Rows"): void;
参数
- sourceData
- Excel.Range
与源数据对应的范围对象。
- seriesBy
-
"Auto" | "Columns" | "Rows"
指定列或行在图表上用作数据系列的方式。 可以是下列项之一:自动 (默认) 、行和列。 有关详细信息,请参阅 Excel.ChartSeriesBy。
返回
void
注解
setPosition(startCell, endCell)
相对于工作表上的单元格放置图表。
setPosition(startCell: Range | string, endCell?: Range | string): void;
参数
- startCell
-
Excel.Range | string
起始单元格。 这是图表将移动到的位置。 起始单元格为左上角或右上角的单元格,具体取决于用户的从右到左显示设置。
- endCell
-
Excel.Range | string
可选。 结束单元格。 如果已指定,图表的宽度和高度将设置为完全覆盖此单元格/区域。
返回
void
注解
示例
await Excel.run(async (context) => {
const sheetName = "Charts";
const rangeSelection = "A1:B4";
const range = context.workbook.worksheets.getItem(sheetName).getRange(rangeSelection);
const sourceData = sheetName + "!" + "A1:B4";
const chart = context.workbook.worksheets.getItem(sheetName).charts.add("pie", range, "auto");
chart.width = 500;
chart.height = 300;
chart.setPosition("C2", null);
await context.sync();
});
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.Chart 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.ChartData) ,其中包含从原始对象加载的任何子属性的浅层副本。
toJSON(): Excel.Interfaces.ChartData;