# Chart & graph builder — library for developers

Chart engine: 29 chart types drawn into a <canvas> in a container you give it. No UI of its own; you build the controls and call the API. Export to PNG, SVG, PDF and other image formats.

## Installation

```sh
npm install @openlibsdev/chart-maker
```

```js
import ChartMaker from "https://cdn.jsdelivr.net/npm/@openlibsdev/chart-maker@1.0.0/+esm";
```

## Usage

```js
import ChartMaker from "@openlibsdev/chart-maker";

const chart = ChartMaker.create("#chart");
chart.setState({
  ...chart.getState(),
  chartType: "bar",
  title: "Sales",
  lv: [{ label: "Q1", value: 12 }, { label: "Q2", value: 19 }, { label: "Q3", value: 15 }]
});
chart.on("statechange", () => console.log(chart.getType()));
chart.exportImage({ format: "png", filename: "sales", scale: 2 });
```

## API

### `ChartMaker.create(container, options)`

Creates a chart in a container (an element or a CSS selector) and returns the chart instance.

### `ChartMaker.registerLocale(code, strings)`

Adds the texts of an interface language (an object of strings) under its code.

### `ChartMaker.loadLocaleJSON(code, url)`

Loads the texts of a language from a JSON file and registers them.

### `ChartMaker.registerCountries(code, data)`

Adds country names in a language, for the map charts.

### `ChartMaker.listExportFormats()`

Lists the formats a chart can be exported to.

### `ChartMaker.setUrls(o)`

Sets the addresses of files loaded on demand (world map data and others).

### `ChartMaker.decodeImage(input)`

Decodes an image file (also HEIC and other formats the browser cannot open) to a data URL, for background pictures.

### `ChartMaker.version`

Version of the library.

### `ChartMaker.FontForge`

The font-forge library used by the chart (with its font picker), for fonts on the chart and in exported images.

### `ChartMaker.ImageForge`

The image-forge library used by the chart (with its background picker), for image export and backgrounds.

### `ChartMaker.findNamedColor(text, lang)`

Finds a named color by its name in the given language ("dark blue" → #00008b).

### `ChartMaker.listNamedColors(lang)`

Lists the named colors with their names in the given language.

### `ChartMaker.loadCountriesJSON(code, url)`

Loads country names in a language from a JSON file and registers them.

### `ChartMaker.locales`

Registered interface languages: {code: texts}.

### `ChartMaker.namedColorTexts(lang)`

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

### `ChartMaker.textFontData`

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

### `ChartMaker.APP`

Name and version of the library: {name, version}.

### `chart.getState()`

Returns a copy of the whole chart state: type, title, data, colors, background.

### `chart.setState(s)`

Replaces the whole state. To change one field, pass {...chart.getState(), field: value}.

### `chart.setType(v)`

Sets the chart type by its key (bar, line, pie, radar, …). Returns the chart, so calls can be chained.

### `chart.getTypes()`

Lists the chart types with their keys, names in the current language and input kind.

### `chart.setTitle(s)`

Sets the chart title.

### `chart.setDatasetLabel(s)`

Sets the name of the data series.

### `chart.getDataLines()`

Returns the data as text lines, one row per line.

### `chart.setTheme(th)`

Sets text, background and grid colors, the font and the palette: {fg, surface, grid, font, palette}.

### `chart.setBackground(bg)`

Sets the background: transparent, a color or a picture.

### `chart.setLocale(c)`

Switches the language of the chart texts.

### `chart.setAnimation(b)`

Turns the drawing animation on or off.

### `chart.exportImage(o)`

Saves the chart to a file: {format, filename, width, height, scale, quality, transparent, includeData}.

### `chart.printChart(o)`

Opens the print dialog with the chart.

### `chart.undo()`

Undoes the last change.

### `chart.redo()`

Redoes the undone change.

### `chart.saveProject(o)`

Downloads the chart as a project file.

### `chart.loadProject(obj)`

Opens a project object made by projectData() or saveProject().

### `chart.on(ev, cb)`

Subscribes to an event (statechange, history, locale, …); returns a function that unsubscribes.

### `chart.destroy()`

Removes the chart and frees its resources.

### `chart.canRedo()`

Whether there is an undone change to redo.

### `chart.canUndo()`

Whether there is a change to undo.

### `chart.clearJournal()`

Clears the journal of actions and messages.

### `chart.evalNumber(v)`

A number from a value or a formula ("2*pi" once mathjs is loaded); 0 for an empty value.

### `chart.getAnimation()`

Whether the drawing animation is on.

### `chart.getCountries()`

Countries of the map charts with their names in the current language.

### `chart.getJournal()`

The journal of actions and messages: [{time, key, arg, type}].

### `chart.getLibraries()`

Which optional chart libraries are on: {name: true/false}.

### `chart.getLocale()`

Current language of the chart texts.

### `chart.getTheme()`

A copy of the current theme: text, background and grid colors, font, palette.

### `chart.getType()`

Current chart type.

### `chart.journalText(e)`

A journal entry as text in the current language.

### `chart.loadMath()`

Loads mathjs for formulas in the data.

### `chart.log(k, ty, arg)`

Adds an entry to the journal: text key, type (info, warning, error) and an argument.

### `chart.mathReady()`

Whether mathjs is loaded.

### `chart.off(ev, cb)`

Unsubscribes from an event.

### `chart.projectData()`

The project data that saveProject writes to a file, without downloading it (for autosave).

### `chart.render()`

Draws the chart again.

### `chart.setLibraries(m)`

Turns optional chart libraries on or off; a type that needs a turned-off library falls back to bar.

### `chart.t(key)`

A text of the chart in the current language by its key.

## Dependencies

- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @sgratzl/chartjs-chart-boxplot 4?external=chart.js (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- canvas2svg 1.0.16 (MIT)
- chart.js 4.4.1 (MIT)
- chart.js 4.4.1 (MIT)
- chart.js 4.4.1 (MIT)
- chartjs-adapter-date-fns 3?external=chart.js (MIT)
- chartjs-chart-error-bars 4?external=chart.js (MIT)
- chartjs-chart-financial 0.2.1?external=chart.js (MIT)
- chartjs-chart-funnel?external=chart.js  (?)
- chartjs-chart-geo 4?external=chart.js (MIT)
- chartjs-chart-graph 4?external=chart.js (MIT)
- chartjs-chart-matrix 2?external=chart.js (MIT)
- chartjs-chart-sankey 0.14.0?external=chart.js (MIT)
- chartjs-chart-treemap 3?external=chart.js (MIT)
- chartjs-chart-venn 4?external=chart.js (MIT)
- chartjs-chart-wordcloud 4?external=chart.js (MIT)
- chartjs-plugin-annotation 3?external=chart.js (MIT)
- chartjs-plugin-crosshair?external=chart.js  (?)
- chartjs-plugin-datalabels 2?external=chart.js (MIT)
- chartjs-plugin-trendline?external=chart.js  (?)
- chartjs-plugin-zoom 2?external=chart.js (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- imagetracerjs 1.2.6 (Unlicense)
- jspdf 2.5.2 (MIT)
- mathjs 12 (Apache-2.0)
- topojson-client 3 (ISC)

- GitHub: https://github.com/OpenLibsDev/chart-maker
- npm: https://www.npmjs.com/package/@openlibsdev/chart-maker
- Open the app: https://openlibs.dev/en/app/chart-maker/
