Export Chart to SVG with viewBox attribute using Node.js via C++
Contents
[
Hide
]
By default, when the chart is exported to SVG format, the viewBox attribute is not included in its XML. However, Aspose.Cells provides ImageOrPrintOptions.getSVGFitToViewPort() property which when set to true exports the chart to SVG with viewBox attribute.
Export Chart to SVG with viewBox attribute
The following sample code exports the chart to SVG format with the viewBox attribute.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create workbook object from source file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "SampleChartBook.xlsx"));
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Access first chart inside the worksheet
const chart = worksheet.getCharts().get(0);
// Set image or print options with SVGFitToViewPort true
const opts = new AsposeCells.ImageOrPrintOptions();
opts.setImageType(AsposeCells.ImageType.Svg);
opts.setSVGFitToViewPort(true);
// Save the chart to svg format
chart.toImage(path.join(dataDir, "Image_out.svg"), opts);
If you open the chart’s SVG in notepad, you will find the viewBox attribute similar to this.
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
<span class="n">width</span><span class="o">=</span><span class="s">"100%"</span> <span class="n">height</span><span class="o">=</span><span class="s">"100%"</span>
<span class="n">viewBox</span><span class="o">=</span><span class="s">"0 0 480 288"</span><span class="o">></span></code></pre></div>