图表渲染

渲染图

Aspose.Cells API 支持将 Excel 图表转换为图像和 PDF 格式,无需任何其他工具或应用程序。为了提供渲染支持,图表班级暴露了印象 & 到PDF具有大量重载的方法,以最好地满足应用程序的要求。

将图表渲染为图像

图表转图像 & 到PDF方法具有大量重载以支持简单和高级渲染。如果应用程序要求以默认尺寸呈现图表,我们建议您使用图表转图像方法如下。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its index to WorksheetCollection
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding sample values to cells
worksheet.Cells["A1"].PutValue(50);
worksheet.Cells["A2"].PutValue(100);
worksheet.Cells["A3"].PutValue(150);
worksheet.Cells["B1"].PutValue(4);
worksheet.Cells["B2"].PutValue(20);
worksheet.Cells["B3"].PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5);
// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// Adding Series Collection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3", true);
// Converting chart to image
chart.ToImage(dataDir + "chartEMF_out.emf", ImageType.Emf);
// Converting chart to Bitmap
System.Drawing.Bitmap bitmap = chart.ToImage();
bitmap.Save(dataDir + "chartBMP_out.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

也可以使用高级设置将图表渲染为图像。 Aspose.Cells API 公开了重载版本图表转图像可以接受实例的方法图像或打印选项同时允许指定分辨率、平滑模式、图像格式等参数。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its index to WorksheetCollection
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding sample values to cells
worksheet.Cells["A1"].PutValue(50);
worksheet.Cells["A2"].PutValue(100);
worksheet.Cells["A3"].PutValue(150);
worksheet.Cells["B1"].PutValue(4);
worksheet.Cells["B2"].PutValue(20);
worksheet.Cells["B3"].PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5);
// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// Adding Series Collection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3", true);
// Create an instance of ImageOrPrintOptions and set a few properties
ImageOrPrintOptions options = new ImageOrPrintOptions()
{
VerticalResolution = 300,
HorizontalResolution = 300,
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
};
// Convert chart to image with additional settings
chart.ToImage(dataDir + "chartPNG_out.png", options);

渲染图到 PDF

为了将图表呈现为 PDF 格式,Aspose.Cells API 公开了图表.ToPdf方法能够将结果 PDF 存储在光盘路径或流中。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its index to WorksheetCollection
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding sample values to cells
worksheet.Cells["A1"].PutValue(50);
worksheet.Cells["A2"].PutValue(100);
worksheet.Cells["A3"].PutValue(150);
worksheet.Cells["B1"].PutValue(4);
worksheet.Cells["B2"].PutValue(20);
worksheet.Cells["B3"].PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5);
// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// Adding Series Collection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3", true);
// Converting chart to PDF
chart.ToPdf(dataDir + "chartPDF_out.pdf");

支持的渲染图表类型

有一些图表类型当前不支持渲染。此类图表类型包含N支持下表的列。

图表类型 图表子类型 支持的
柱子 柱子 Y
列堆叠 Y
列 100% 堆叠 Y
Column3D簇状 Y
列 3D 堆叠 Y
Column3D100PercentStacked Y
立柱三维 Y
酒吧 酒吧 Y
酒吧堆叠 Y
Bar100Percent 堆叠 Y
Bar3DClustered Y
Bar3D堆叠 Y
Bar3D100PercentStacked Y
线 线 Y
线堆叠 Y
Line100Percent 堆叠 Y
带数据标记的线 Y
LineStackedWithDataMarkers Y
Line100PercentStackedWithDataMarkers Y
直线三维 Y
馅饼 馅饼 Y
Pie3D Y
派派 Y
馅饼爆炸 Y
饼图3D分解 Y
馅饼吧 Y
分散 分散 Y
ScatterConnectedByCurvesWithDataMarker Y
ScatterConnectedByCurvesWithoutDataMarker Y
ScatterConnectedByLinesWithDataMarker Y
ScatterConnectedByLinesWithoutDataMarker Y
区域 区域 Y
面积堆叠 Y
面积 100% 堆叠 Y
三维区域 Y
Area3D堆叠 Y
Area3D100PercentStacked Y
油炸圈饼 油炸圈饼 Y
甜甜圈爆炸 Y
雷达 雷达 Y
带数据标记的雷达 Y
雷达填充 Y
表面 Surface3D
表面线框3D
表面轮廓
表面轮廓线框
气泡 气泡 Y
泡泡3D
库存 股价高低收盘 Y
股票开高低收 Y
StockVolumeHighLow关闭 Y
StockVolumeOpenHighLowClose Y
圆柱 圆柱 Y
圆柱叠层 Y
圆柱100%堆叠 Y
圆柱棒 Y
圆柱形条堆叠 Y
CylindricalBar100PercentStacked Y
圆柱3D Y
锥体 锥体 Y
圆锥堆叠 Y
Cone100Percent堆叠 Y
锥形棒 Y
锥形条堆叠 Y
ConicalBar100PercentStacked Y
锥形柱3D Y
金字塔 金字塔 Y
金字塔堆叠 Y
金字塔100%堆积 Y
金字塔酒吧 Y
金字塔酒吧堆叠 Y
PyramidBar100PercentStacked Y
金字塔柱3D Y
盒须 盒须
漏斗 漏斗 Y
帕累托线 帕累托线 Y
森伯斯特 森伯斯特 Y
树形图 树形图 Y
瀑布 瀑布 Y
直方图 直方图
地图 地图 N

推进主题