Quickstart
快速入门
此页面包含两个独立的示例,您可以将其复制到 .NET 控制台项目中(在 从源码构建库)并立即运行。
示例 1 — 使用折线图创建文档
创建一个新文档,使用 DocumentBuilder.InsertChart() 插入折线图,清除默认系列,并添加您自己的数据:
using Aspose.Words;
using Aspose.Words.Drawing.Charts;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 432, 252);
Chart chart = shape.Chart;
// Delete default generated series.
chart.Series.Clear();
string[] categories = new string[] { "AW Category 1", "AW Category 2", "AW Category 3" };
chart.Series.Add("AW Series 1", categories, new double[] { 4.3, 2.5, 3.5 });
doc.Save("chart.docx");示例 2 — 配置带有数据标签的饼图
插入饼图,添加一个系列,并配置 ChartDataLabelCollection 以显示百分比和数值:
using Aspose.Words;
using Aspose.Words.Drawing.Charts;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Pie, 432, 252);
Chart chart = shape.Chart;
chart.Series.Clear();
ChartSeries series = chart.Series.Add("Series 1",
new string[] { "Category1", "Category2", "Category3" },
new double[] { 2.7, 3.2, 0.8 });
ChartDataLabelCollection labels = series.DataLabels;
labels.ShowPercentage = true;
labels.ShowValue = true;
labels.ShowLeaderLines = false;
labels.Separator = " - ";
doc.Save("pie-chart.docx");ChartSeries.Add() 接受与数值一起的类别名称,ChartDataLabelCollection 控制每个数据标签显示的内容——百分比、数值、类别名称,或由 Separator 分隔的组合。