Skip to content

Charts

Every chart is built the same way regardless of type. A drawing is added to the workbook, a chart is added to the drawing, the type is chosen, series are added, and the drawing is attached to a sheet.

dwng := ss.AddDrawing()
chart, anc := dwng.AddChart(spreadsheet.AnchorTypeTwoCell)

lc := chart.AddLineChart()
series := lc.AddSeries()
series.SetText("Price")
series.CategoryAxis().SetLabelReference(`'Sheet 1'!A2:A6`)
series.Values().SetReference(`'Sheet 1'!B2:B6`)

sheet.SetDrawing(dwng)

The type-specific call is the one line in the middle. Swap AddLineChart for AddPieChart and the rest of the code is unchanged, apart from the series type you get back and which axes the chart needs.

Chart types

chart.Chart exposes sixteen constructors. The series type decides what data you can attach to it, and the axis column is what the chart needs before it will open cleanly in Excel.

ChartConstructorSeries typeAxes
LineAddLineChart()LineChartSeriesCategory, value
Line 3DAddLine3DChart()LineChartSeriesCategory, value, series
Bar or columnAddBarChart()BarChartSeriesCategory, value
Bar 3DAddBar3DChart()BarChartSeriesCategory, value, series
AreaAddAreaChart()AreaChartSeriesCategory, value
Area 3DAddArea3DChart()AreaChartSeriesCategory, value, series
RadarAddRadarChart()RadarChartSeriesCategory, value
PieAddPieChart()PieChartSeriesNone
Pie 3DAddPie3DChart()PieChartSeriesNone
Pie of pieAddPieOfPieChart()PieChartSeriesNone
DoughnutAddDoughnutChart()PieChartSeriesNone
SurfaceAddSurfaceChart()SurfaceChartSeriesCategory, value, series
Surface 3DAddSurface3DChart()SurfaceChartSeriesCategory, value, series
ScatterAddScatterChart()ScatterChartSeriesCategory, value
BubbleAddBubbleChart()BubbleChartSeriesCategory, value
StockAddStockChart()LineChartSeriesCategory, value

The pie family has no AddAxis method at all, so there is nothing to attach an axis to. Everything else does, and axes are created on the chart rather than on the plot: chart.AddCategoryAxis(), chart.AddValueAxis() and chart.AddSeriesAxis(), then lc.AddAxis(ca) to associate each one with the plot. Two axes that cross each other also need ca.SetCrosses(va) and va.SetCrosses(ca); without that pairing Excel has no reference axis to draw against.

Series types differ in more than name. LineChartSeries and ScatterChartSeries carry Marker(), Labels() and SetSmooth(); BarChartSeries, RadarChartSeries and AreaChartSeries carry none of the three. PieChartSeries adds SetExplosion(), and BubbleChartSeries adds BubbleSizes() for the third value per point.

Where the numbers come from

A series does not hold Go values. It holds a reference to a range of cells, so the numbers have to be written into the sheet first even when the chart is the only reason they exist.

series.CategoryAxis().SetLabelReference(`'Sheet 1'!A2:A6`)
series.Values().SetReference(`'Sheet 1'!B2:B6`)

SetLabelReference expects strings and SetNumberReference expects numbers; Values().SetReference is the numeric range that gets plotted. The reference is an unparsed string written straight into the chart XML, which means a typo or a stale range fails at open time in Excel rather than at save time in Go. The sheet name matters too: AddSheet names sheets Sheet 1, Sheet 2 and so on, and the space is why the references are quoted.

The alternative is SetValues, which embeds literal values in the chart and needs no cells. Line Chart with No Data covers what you give up by doing that.

Anchors

AddChart returns an anchor along with the chart, and the anchor type you pass decides both how the chart is positioned and which of the anchor’s setters do anything.

Anchor typePositionBehavior on resizeWorking setters
AnchorTypeTwoCellBetween a top-left and a bottom-right cellStretches as the rows and columns it spans resizeMoveTo, SetWidthCells, SetHeightCells
AnchorTypeOneCellTop-left cell plus a fixed sizeMoves with its cell, keeps its sizeMoveTo, SetWidth, SetHeight
AnchorTypeAbsoluteFixed offset from the sheet originDoes not move at allSetColOffset, SetRowOffset, SetWidth, SetHeight

The mismatched setters are silent no-ops, not errors. SetWidth and SetHeight on a two-cell anchor do nothing, SetWidthCells and SetHeightCells do nothing on the other two, and MoveTo does nothing on an absolute anchor. That is the single most common reason a chart comes out the wrong size or in the wrong place.

Where SetWidth does apply it takes a measurement.Distance, whose base unit is the point. SetWidth(10) is ten points wide, not ten cells; the cell-counted version is SetWidthCells(10).

A two-cell anchor with no positioning spans columns F to K and rows 1 to 21, which is a deliberate default: an anchor whose corners are both at 0,0 produces a chart Excel will not show.

One more constraint sits above all of this. A worksheet can reference only one drawing, so several charts on a sheet must be added to the same Drawing. See Multiple Charts.

Where to look

GuideCovers
Bar ChartVertical and horizontal bars, and where the grouping is fixed.
Line ChartTrends over an ordered category axis, markers and smoothing.
3D Line ChartSeries laid out in depth, and the third axis it needs.
Line Chart from CSVLoading a file, then computing the cell range instead of hardcoding it.
Line Chart with No DataLiteral series values, with no cells behind them.
Pie ChartProportions of one total, exploded slices, the axis-free chart family.
Radar ChartSeveral series compared across the same categories.
Surface ChartA grid of series read as a surface, and its color bands.
Bubble ChartA third value per point, sized as the bubble.
Multiple ChartsSeveral charts on one sheet through a single drawing.
Multiple Charts from CSVA report built from one dataset with three chart types.
Last updated on