Charts
Native OOXML charts and chart data tables.
Native OOXML Charts#
Chart types are stored as pure XML inside the DOCX package with inline data caches, so charts are completely self-contained with no linked spreadsheet.
Chart Methods#
Category-based charts (use string category labels)#
| Method | Chart Type | Best Used For |
|---|---|---|
addColumnChart(title, cats, series, opts) | Vertical bars | Category comparisons across groups |
addBarChart(title, cats, series, opts) | Horizontal bars | Long category labels |
addLineChart(title, cats, series, opts) | Lines / trends | Time-series, continuous data |
addAreaChart(title, cats, series, opts) | Filled areas | Cumulative or stacked trends |
addPieChart(title, cats, series, opts) | Pie slices | Proportions — uses first series only |
addDoughnutChart(title, cats, series, opts) | Hollow-centre pie | Proportions with inner space |
addChart(type, title, cats, series, opts) | Any type above | Generic entry point |
Numeric-axis charts (no category labels — pass NULL for cats)#
| Method | Chart Type | Best Used For |
|---|---|---|
addScatterChart(title, series, opts) | XY scatter | Correlation, scientific data, irregular X spacing |
addBubbleChart(title, series, opts) | Bubble | Three-variable data — X, Y, and a size dimension |
Series Format#
Category-based series (column, bar, line, area, pie, doughnut)#
| Key | Type | Required | Description |
|---|---|---|---|
:name | String | Yes | Series label shown in the legend |
:values | List | Yes | Numeric values, one per category |
:color | String | No | Hex colour override, e.g. "4472C4" |
Scatter series#
| Key | Type | Required | Description |
|---|---|---|---|
:name | String | Yes | Series label shown in the legend |
:xValues | List | Yes | X-axis numeric values |
:yValues | List | Yes | Y-axis numeric values (same length as :xValues) |
:color | String | No | Hex colour override |
Bubble series#
| Key | Type | Required | Description |
|---|---|---|---|
:name | String | Yes | Series label shown in the legend |
:xValues | List | Yes | X-axis numeric values |
:yValues | List | Yes | Y-axis numeric values |
:sizes | List | Yes | Bubble size values — positive numbers, relative scale |
:color | String | No | Hex colour override |
If :color is omitted for any series type the library cycles through an 8-colour Office palette automatically.
Options Reference#
Layout and legend options (all chart types)#
| Option | Type | Default | Description |
|---|---|---|---|
:widthCm | Number | 14 | Display width in centimetres |
:heightCm | Number | 9 | Display height in centimetres |
:centered | Bool | true | Centre chart horizontally on the page |
:showLegend | Bool | true | Show or hide the legend |
:legendPos | String | "r" | Legend position: "r" "b" "t" "l" |
:showDataLabels | Bool | false | Show value labels on each data point |
Axis formatting options (all non-pie/doughnut chart types)#
| Option | Type | Default | Description |
|---|---|---|---|
:yAxisMin | Number | auto | Force value axis lower bound (e.g. 0 to start at zero) |
:yAxisMax | Number | auto | Force value axis upper bound |
:yAxisFormat | String | "General" | Number format for value axis labels — see table below |
:yAxisTitle | String | "" | Rotated label alongside the value axis |
:xAxisTitle | String | "" | Label below the category axis (or X axis for scatter/bubble) |
:showGridlines | Bool | true | Show major gridlines on the value axis |
:showDataTable | Bool | false | Show source data in a table below the chart (column/bar/line/area only) |
:dataTableShowKeys | Bool | true | Include colour legend keys in the data table |
Axis format string reference#
| Format string | Example output | Use case |
|---|---|---|
"General" | 1234567 | Default — no formatting |
"#,##0" | 1,234,567 | Large integers with thousands separator |
"$#,##0" | $1,234,567 | Currency (whole dollars) |
"$#,##0.00" | $1,234,567.89 | Currency with cents |
"0%" | 32% | Percentage (value 0.32 → 32%) |
"0.0%" | 32.5% | Percentage with one decimal |
"0.00" | 3.14 | Two decimal places |
"0.0" | 3.1 | One decimal place |
"0.00E+00" | 3.14E+06 | Scientific notation |
Note: These are standard OOXML/Excel number format strings. Any Excel format string is accepted — the library passes it through verbatim to the XML.
Category-chart options#
| Option | Type | Default | Description |
|---|---|---|---|
:grouping | String | "clustered" | Bar/column/area: "clustered" "stacked" "percentstacked" |
:smooth | Bool | false | Line chart only — bezier curves |
:colors | List | (palette) | Override full palette with a list of hex strings |
Scatter options#
| Option | Type | Default | Description |
|---|---|---|---|
:markerStyle | String | "circle" | "circle" "square" "diamond" "triangle" "x" "star" "dot" "dash" "none" |
:markerSize | Number | 7 | Marker size in half-points |
:lines | Bool | false | Connect points with lines |
:smooth | Bool | false | Bezier curves on lines (requires :lines = true) |
:xAxisMin | Number | auto | Force X axis lower bound |
:xAxisMax | Number | auto | Force X axis upper bound |
:xAxisFormat | String | "General" | Number format for X axis labels |
:xAxisTitle | String | "" | X axis label |
:yAxisTitle | String | "" | Y axis label |
Bubble options#
| Option | Type | Default | Description |
|---|---|---|---|
:bubble3D | Bool | false | Render bubbles with 3-D specular shading |
:xAxisMin | Number | auto | Force X axis lower bound |
:xAxisMax | Number | auto | Force X axis upper bound |
:xAxisFormat | String | "General" | Number format for X axis labels |
:xAxisTitle | String | "" | X axis label |
:yAxisTitle | String | "" | Y axis label |
Code Examples#
Column chart — three series, data labels:
doc.addColumnChart(
"Quarterly Revenue vs Costs ($K)",
["Q1 2024", "Q2 2024", "Q3 2024", "Q4 2024"],
[
[:name = "Revenue", :values = [320, 410, 380, 490]],
[:name = "Costs", :values = [210, 255, 230, 290]],
[:name = "Profit", :values = [110, 155, 150, 200]]
],
[:widthCm = 14, :heightCm = 9, :showDataLabels = true, :legendPos = "b"])Line chart — smooth curves:
doc.addLineChart(
"Monthly Website Traffic",
["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
[
[:name = "2023", :values = [12000, 14500, 13200, 16800, 18000, 17400]],
[:name = "2024", :values = [15000, 18200, 17500, 21000, 23500, 22800]]
],
[:smooth = true, :legendPos = "r"])Pie chart — percent labels:
doc.addPieChart(
"Product Revenue Share",
["Ring", "PWCT", "Supernova", "PWCT2"],
[[:name = "Revenue ($K)", :values = [450, 280, 160, 110]]],
[:showDataLabels = true])Stacked column — :grouping option:
doc.addColumnChart(
"Revenue by Product",
["Q1 2024", "Q2 2024", "Q3 2024", "Q4 2024"],
[
[:name = "Ring", :values = [120, 155, 140, 180]],
[:name = "PWCT", :values = [80, 95, 90, 110]],
[:name = "Supernova", :values = [60, 75, 70, 90]]
],
[:grouping = "stacked"])Scatter chart — markers only (note: NULL for categories, :xValues/:yValues in series):
doc.addScatterChart(
"Study Hours vs Exam Score",
[
[:name = "Cohort A",
:xValues = [2, 3, 4, 5, 6, 7, 8, 9, 10],
:yValues = [45, 52, 60, 67, 73, 79, 84, 88, 93]],
[:name = "Cohort B",
:xValues = [1, 2, 4, 5, 7, 8, 9, 10, 11],
:yValues = [38, 48, 55, 63, 70, 75, 81, 86, 90]]
],
[:markerStyle = "circle", :xAxisTitle = "Study Hours", :yAxisTitle = "Score (%)"])Scatter chart — smooth lines + markers:
doc.addScatterChart(
"Algorithm Complexity",
[
[:name = "O(n log n)", :xValues = [100, 500, 1000, 5000, 10000],
:yValues = [0.8, 5.2, 11.5, 68.3, 145.2], :color = "4472C4"],
[:name = "O(n^2)", :xValues = [100, 500, 1000, 5000, 10000],
:yValues = [1.2, 28.5, 112.4, 2802.5, 11210.0], :color = "C00000"]
],
[:lines = true, :smooth = true, :markerStyle = "diamond",
:xAxisTitle = "Input Size (n)", :yAxisTitle = "Time (ms)"])Scatter chart — lines only, no markers:
doc.addScatterChart(
"Sine vs Cosine Wave",
[
[:name = "sin(x)", :xValues = aX, :yValues = aSin, :color = "2E75B6"],
[:name = "cos(x)", :xValues = aX, :yValues = aCos, :color = "70AD47"]
],
[:lines = true, :smooth = true, :markerStyle = "none"])Bubble chart — flat shading (each product is its own series for distinct colours):
doc.addBubbleChart(
"Product Portfolio — Share / Growth / Revenue",
[
[:name = "Ring", :xValues = [35], :yValues = [22], :sizes = [450], :color = "4472C4"],
[:name = "PWCT", :xValues = [28], :yValues = [15], :sizes = [280], :color = "ED7D31"],
[:name = "Supernova", :xValues = [18], :yValues = [31], :sizes = [160], :color = "70AD47"]
],
[:xAxisTitle = "Market Share (%)", :yAxisTitle = "Growth (%)"])Bubble chart — 3-D shading, bubble-size labels, multiple points per series:
doc.addBubbleChart(
"Research Projects — Team / Duration / Impact",
[
[:name = "Project Alpha",
:xValues = [3, 5, 8, 12], :yValues = [6, 9, 14, 18],
:sizes = [45, 120, 280, 500], :color = "1F3864"],
[:name = "Project Beta",
:xValues = [2, 4, 7, 10], :yValues = [4, 7, 11, 15],
:sizes = [30, 90, 200, 380], :color = "C00000"]
],
[:bubble3D = true, :showDataLabels = true,
:xAxisTitle = "Team Size", :yAxisTitle = "Months"])Column chart — fixed axis bounds and currency format:
doc.addColumnChart(
"Regional Revenue vs Target",
["North", "South", "East", "West", "Central"],
[
[:name = "Actual", :values = [1250000, 980000, 1420000, 1100000, 870000]],
[:name = "Target", :values = [1200000, 1050000, 1350000, 1150000, 950000]]
],
[:yAxisMin = 0, :yAxisMax = 1800000,
:yAxisFormat = "$#,##0",
:yAxisTitle = "Revenue (USD)", :xAxisTitle = "Region",
:showGridlines = false, :legendPos = "b"])Line chart — percentage format on value axis:
doc.addLineChart(
"Gross Margin Trend",
["Q1", "Q2", "Q3", "Q4"],
[
[:name = "2023", :values = [0.38, 0.41, 0.39, 0.43]],
[:name = "2024", :values = [0.41, 0.44, 0.42, 0.46]]
],
[:yAxisMin = 0, :yAxisMax = 0.6,
:yAxisFormat = "0.0%",
:yAxisTitle = "Gross Margin", :xAxisTitle = "Quarter"])Scatter chart — fixed bounds with formatted numeric axes:
doc.addScatterChart(
"Ad Spend vs Revenue",
[
[:name = "2024",
:xValues = [50000, 100000, 200000, 350000],
:yValues = [210000, 430000, 870000, 1500000]]
],
[:xAxisMin = 0, :xAxisMax = 400000,
:yAxisMin = 0, :yAxisMax = 2000000,
:xAxisFormat = "$#,##0", :yAxisFormat = "$#,##0",
:xAxisTitle = "Ad Spend (USD)", :yAxisTitle = "Revenue (USD)",
:markerStyle = "circle", :lines = true, :smooth = true])Pie and Doughnut: Single Series Only#
Pie and doughnut charts render only the first series. If more than one series is passed, DOCXLib prints a warning to the console and silently discards the extra series. Use a column or bar chart when you need to compare multiple series.
# WRONG — triggers warning; only "2023" series will appear in the chart
doc.addPieChart("Sales", cats, [
[:name = "2023", :values = [380, 240, 140, 90]],
[:name = "2024", :values = [450, 280, 160, 110]] # ignored
], [])
# CORRECT — grouped column for side-by-side comparison
doc.addColumnChart("Sales 2023 vs 2024", cats, [
[:name = "2023", :values = [380, 240, 140, 90]],
[:name = "2024", :values = [450, 280, 160, 110]]
], [:legendPos = "b"])OOXML Internals#
Each chart adds three components to the DOCX package automatically:
| File | Purpose |
|---|---|
word/charts/chartN.xml | Full chart definition — data stored inline as XML caches; no spreadsheet needed |
word/charts/_rels/chartN.xml.rels | Chart relationship manifest (no external dependencies) |
Drawing reference in document.xml | <c:chart r:id="rIdN"/> anchors the chart in the document body |
[Content_Types].xml receives one Override entry per chart automatically.
Category-based charts use <c:cat> + <c:val> for series data. Scatter and bubble charts use <c:xVal> + <c:yVal> (and <c:bubbleSize> for bubble) instead — both axes are <c:valAx> rather than <c:catAx> + <c:valAx>.
Compatibility: Charts render in Word 2016+, Microsoft 365, and LibreOffice 7+. The DrawingML chart schema (http://schemas.openxmlformats.org/drawingml/2006/chart) is part of the ECMA-376 standard.
Chart Data Tables#
The :showDataTable = true option renders the chart's source data in a compact bordered table directly beneath the plot area — useful for printed reports where readers need exact values.
Options#
| Option | Type | Default | Description |
|---|---|---|---|
:showDataTable | Bool | false | Render source data table beneath the chart |
:dataTableShowKeys | Bool | true | Include colour legend keys in the leftmost column |
Supported chart types: column, bar, line, area Not applicable to: pie, doughnut, scatter, bubble
Code Examples#
Column chart with data table and legend keys:
doc.addColumnChart(
"Quarterly Revenue",
["Q1", "Q2", "Q3", "Q4"],
[
[:name = "Ring", :values = [320000, 410000, 380000, 490000]],
[:name = "PWCT", :values = [210000, 265000, 245000, 305000]]
],
[:yAxisFormat = "$#,##0", :legendPos = "b",
:showDataTable = true, :dataTableShowKeys = true])Line chart — data table without colour keys:
doc.addLineChart(
"Monthly Active Users",
["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
[[:name = "Web", :values = [142, 158, 171, 185, 201, 218]],
[:name = "Mobile", :values = [89, 104, 118, 135, 152, 171]]],
[:smooth = true, :showDataTable = true, :dataTableShowKeys = false])Combined with chart style presets:
doc.setChartDefaults([
:widthCm = 14,
:legendPos = "b",
:showDataTable = true,
:dataTableShowKeys = true,
:yAxisFormat = "#,##0"
])
# Every subsequent chart automatically gets a data table
doc.addColumnChart("Revenue", quarters, series, [])
doc.addLineChart("Trend", months, trendSeries, [:smooth = true])
# Remove all chart defaults
doc.clearChartDefaults()