TimeChart

Authoring Plugins

TimeChart comes with a simple but flexible plugin system to help you customize the chart to whatever you want.

A plugin is a JavaScript object with a function named apply. This function takes a single argument, the current TimeChart instance.

And the return value of this function will be added to chart.plugins object. You can use this interact with code outside the plugin.

import TimeChart from 'timechart';
const demoPlugin = {
    apply(chart) {
        // Do whatever you want
        return 'demo';
    }
};
const chart = new TimeChart(el, {
    plugins: { demoPlugin },
});
chart.plugins.demoPlugin === 'demo'; // true

The same plugin instance may be applied to multiple chart instance.

Inside the apply function. The plugin can use many advanced APIs documented below.

Event

TimeChart implemented a simple event mechanism to communicate with plugins. For example, to subscribe to an event:

chart.model.updated.on(() => console.log('chart updated'));
chart.model.resized.on((w, h) => console.log(`chart resized to ${w}x${h}`));

The following document use TypeScript notation to denote the event signature. For example: Event<(width: number, height: number) => void> means this is an event, the listener of it will get two arguments, each of type number; and the listener should not return anything.

APIs

Pass Options to Plugin

Sometimes you may need to pass some extra options or data to plugins. The recommended way to do this is declare your plugin as a class and pass them in from constructor:

import TimeChart from 'timechart';
class DemoPlugin {
    constructor(options) {
        this.options = options;
    }
    apply(chart) {
        // Do whatever you want with this.options
    }
};
const chart = new TimeChart(el, {
    plugins: { demoPlugin: new DemoPlugin({...}) },
});