Charts
UniOffice cannot create a native PowerPoint chart on a slide. The presentation
package has no chart constructor - AddChart exists only on a spreadsheet
drawing - so a chart on a slide has to be rendered to an image first and added
as a picture. The example does that with UniChart and UniPDF’s renderer.
| Approach | Result |
|---|---|
| Render with UniChart, add as a picture | A chart on any slide. Fixed image, no underlying data. |
| Open a template that already contains a chart | The chart survives a round trip and ppt.Charts() reads it. |
Neither approach gives you a chart PowerPoint will let the reader edit and refresh. If that matters, the chart has to be authored in the template and the program limited to filling the placeholders around it.
Rendering a chart onto a slide
chart := &unichart.PieChart{
Values: []dataset.Value{
{Value: 5, Label: "Blue"},
{Value: 4, Label: "Gray"},
},
}
chart.SetHeight(500)
c := creator.New()
if err := c.Draw(creator.NewChart(chart)); err != nil {
log.Fatalf("failed to draw chart: %v", err)
}
if err := c.WriteToFile("output.pdf"); err != nil {
log.Fatalf("failed to write output file: %v", err)
}That produces a one-page PDF. The example then renders page 1 to a PNG with
render.NewImageDevice(), trims the surrounding whitespace with its own
cropImage and findBoundingBox helpers, and hands the result to the ordinary
picture path:
img, err := common.ImageFromFile("preview.png")
iref, err := ppt.AddImage(img)
pic := slide.AddImage(iref)
pic.Properties().SetWidth(2 * measurement.Inch)
pic.Properties().SetHeight(iref.RelativeHeight(2 * measurement.Inch))From AddImage onwards this is the same code as the images guide.
The chart is a PNG like any other.
What this needs
Three UniDoc modules: unioffice/v2 for the presentation, unipdf/v5 for the
creator and the renderer, and unichart for the chart itself. Both UniOffice
and UniPDF check their own metered license, so init sets the key twice, once
through each package.
The rendered image resolution comes from device.OutputWidth, which the example
sets to 2000. Too low and the chart is soft on a projector; too high and the
PNG dominates the file size of the deck.
Limitations
The chart is a picture. It does not scale as vector art, its text is not
selectable or searchable, and text extraction will not see the labels. The
.pptx grows by the size of the PNG for every chart on every slide.
Rendering goes through a PDF on disk. c.WriteToFile and the image device both
touch the filesystem, so the working directory has to be writable and the
intermediate output.pdf and preview.png are left behind.
ppt.Charts() and slide.GetChartSpaceByRelId return the ChartSpace of
charts that were already in an opened file. They are read paths into the
DrawingML chart schema, not a way to build one.
Run the example
The example builds a pie chart with seven slices, renders it, crops it, adds it
to a new slide two inches wide, and writes output.pptx alongside the
intermediate output.pdf and preview.png.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/presentation/chart
go run main.goIf this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.