@nextgensoftwares/folio-charts
Charts shared by slides and documents. The main entry is DOM-free; /react adds the view and the data grid.
ts
import { layoutChart, chartToSvg, sampleChart, writeChartXml, chartWorkbook } from '@nextgensoftwares/folio-charts';
const chart = sampleChart('column');
const svg = chartToSvg(layoutChart(chart, 480, 300));Model
Plain JSON, shaped like DrawingML's c:chartSpace.
| Export | What |
|---|---|
Chart | { type, grouping?, categories, series, title?, legend?, labels?, axes?, style?, smooth?, markers?, hole? } |
ChartType, CHART_TYPES | column, bar, line, area, pie, doughnut, scatter |
ChartGrouping | clustered, stacked, percent (100% stacked) |
ChartSeries | { name, values, x? (scatter), color?, type? (combo: column, line or area on the same axes), points? (colours by index) } |
ChartAxis | { visible?, title?, gridlines?, min?, max?, format? } (axes.cat, axes.val) |
ChartLabels | data labels: { value?, percent?, category?, series? } |
ChartStyle | { palette?, vary?, background? (null: none), border?, fontSize? (pt), font? } |
ChartColor | #rrggbb, or a theme slot (accent1…accent6, text, background) resolved when drawn |
ChartTheme, OFFICE_THEME | accents, text, muted, grid, background, font; Office's default theme |
resolveColor(c, theme, fallback?), seriesColor(chart, i, theme, point?), shade(hex, k), varies(chart) | colours: theme slots to hex (accents past six darkened as Office does), a series' or point's colour, one colour per point (pies; style.vary) |
Data
| Export | What |
|---|---|
ChartData, chartData(chart) | the data as a grid: { categories, series: { name, values, x? }[] } |
withChartData(chart, data) | the chart with new data; series keep colour, type and point colours by position |
sampleChart(type, grouping?) | a ready-made chart of a type (what galleries insert) |
sanitizeChart(raw), CHART_LIMITS | a chart from untrusted JSON (unknown fields dropped, sizes capped, colours checked), or null |
Layout and drawing
| Export | What |
|---|---|
layoutChart(chart, width, height, opts?: ChartLayoutOptions) → ChartScene (ChartLayoutOptions: { theme?, measure? }) | lays a chart out in a box: background, title, legend, plot (axes with nice ticks, gridlines, axis titles, bars, lines, areas, points, slices, data labels). Pure; measure(text, size, bold) gives exact text widths (default: an estimate). Bar charts list the first category at the bottom, as Office does |
ChartScene, SceneItem, ScenePath, SceneText, PathCmd | the display list: paths (M/L/C/Z, fill, stroke, width, dash) and texts (anchor, baseline, size, colour, align, bold, rotate) in box px |
chartToSvg(scene, { title? }), svgItem, svgPathData | a standalone SVG document, one element, path data |
paintChartCanvas(ctx, scene, x?, y?), Canvas2D | draws on any 2D canvas context |
paintChartPdf(scene, target, at, measure), PdfTarget | vectors into a PDF page through folio-export-pdf's painter context (ops, text): path operators, real text, rotated axis titles |
arcPath, smoothPath, rectPath, estimateWidth, formatNumber(v, format), niceScale(min, max, count?, fixed?) | geometry and number helpers (General, 0, 0.00, #,##0, 0%, $#,##0…) |
Chart XML and the embedded workbook
| Export | What |
|---|---|
readChartXml(xml) → { chart, workbookRel? } | null | a chart part (c:chartSpace) from its cached values: bar/col, line, area, pie, doughnut, scatter (3-D read flat), grouping, combos (each plot's series typed), titles (Office's automatic one too), legend, data labels, axes (titles, min/max, number format, gridlines, hidden), colours (sRGB and theme accents), smoothing, markers, hole size; the embedded workbook's relationship id |
writeChartXml(chart, { workbookRel? }) | a chart part Word and PowerPoint draw natively: series with Sheet1! formulas and caches, combos as one plot per type on shared axes, schema element order |
chartWorkbook(chart), readChartWorkbook(bytes) | the .xlsx "Edit Data" opens (one sheet in Office's layout: categories down column A, series across from B), and its data back |
sheetLayout(chart), colName(i), cellRef(col, row) | where the data sits on the sheet: the formulas' ranges |
colorIn(el), C_NS, A_NS | a DrawingML colour as a chart colour; the chart and drawing namespaces |
React (/react)
Needs react, react-dom and @nextgensoftwares/folio-react installed (optional peers since 0.4.1, so server-side users of the main entry, such as folio-docx and folio-pptx, install no React).
| Export | What |
|---|---|
ChartView (ChartViewProps: chart, width, height, label?, theme?, measure?, className?) | the chart as inline SVG |
ChartDataGrid (ChartDataGridProps: data, onChange, single?, scatter?, disabled?, className?), CHART_GRID_CSS | the data sheet: categories down, series across; values commit on Enter or blur; add and remove rows and series. Slides and documents both use it |