使用 viewBox 属性将图表导出到 SVG

默认情况下,当图表导出为 SVG 格式时,视图框属性不包含在其 XML 中。但是,Aspose.Cells 提供ImageOrPrintOptions.setSVGFitToViewPort()设置为的属性真的使用 viewBox 属性将图表导出到 SVG。

如果你用记事本打开图表的SVG,你会发现视图框属性类似于此。

 <svg xmlns="http://www.w3.org/2000/svg"

     xmlns:xlink="http://www.w3.org/1999/xlink"

     width="100%" height="100%"

     viewBox="0 0 480 288">

代码片段

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ExportCharttoSVG.class);
// Create workbook object from source file
Workbook workbook = new Workbook(dataDir + "source.xlsx");
// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access first chart inside the worksheet
Chart chart = worksheet.getCharts().get(0);
// Set image or print options
// with SVGFitToViewPort true
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.setSaveFormat(SaveFormat.SVG);
opts.setSVGFitToViewPort(true);
// Save the chart to svg format
chart.toImage(dataDir + "out.svg", opts);

相关文章