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.
| Chart | Constructor | Series type | Axes |
|---|---|---|---|
| Line | AddLineChart() | LineChartSeries | Category, value |
| Line 3D | AddLine3DChart() | LineChartSeries | Category, value, series |
| Bar or column | AddBarChart() | BarChartSeries | Category, value |
| Bar 3D | AddBar3DChart() | BarChartSeries | Category, value, series |
| Area | AddAreaChart() | AreaChartSeries | Category, value |
| Area 3D | AddArea3DChart() | AreaChartSeries | Category, value, series |
| Radar | AddRadarChart() | RadarChartSeries | Category, value |
| Pie | AddPieChart() | PieChartSeries | None |
| Pie 3D | AddPie3DChart() | PieChartSeries | None |
| Pie of pie | AddPieOfPieChart() | PieChartSeries | None |
| Doughnut | AddDoughnutChart() | PieChartSeries | None |
| Surface | AddSurfaceChart() | SurfaceChartSeries | Category, value, series |
| Surface 3D | AddSurface3DChart() | SurfaceChartSeries | Category, value, series |
| Scatter | AddScatterChart() | ScatterChartSeries | Category, value |
| Bubble | AddBubbleChart() | BubbleChartSeries | Category, value |
| Stock | AddStockChart() | LineChartSeries | Category, 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 type | Position | Behavior on resize | Working setters |
|---|---|---|---|
AnchorTypeTwoCell | Between a top-left and a bottom-right cell | Stretches as the rows and columns it spans resize | MoveTo, SetWidthCells, SetHeightCells |
AnchorTypeOneCell | Top-left cell plus a fixed size | Moves with its cell, keeps its size | MoveTo, SetWidth, SetHeight |
AnchorTypeAbsolute | Fixed offset from the sheet origin | Does not move at all | SetColOffset, 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
| Guide | Covers |
|---|---|
| Bar Chart | Vertical and horizontal bars, and where the grouping is fixed. |
| Line Chart | Trends over an ordered category axis, markers and smoothing. |
| 3D Line Chart | Series laid out in depth, and the third axis it needs. |
| Line Chart from CSV | Loading a file, then computing the cell range instead of hardcoding it. |
| Line Chart with No Data | Literal series values, with no cells behind them. |
| Pie Chart | Proportions of one total, exploded slices, the axis-free chart family. |
| Radar Chart | Several series compared across the same categories. |
| Surface Chart | A grid of series read as a surface, and its color bands. |
| Bubble Chart | A third value per point, sized as the bubble. |
| Multiple Charts | Several charts on one sheet through a single drawing. |
| Multiple Charts from CSV | A report built from one dataset with three chart types. |