vmpowerio/chartjs-node

Allow custom chart types

Closed this issue · 2 comments

From what I understand it is not possible to add custom chart types to chartjs-node? At least not without running JSDom separately to import the ChartJS library.

Might I suggest something like this:

drawChart(configuration) {
  ...
  const Chartjs = require('chart.js');
  ...
  if (configuration.options.charts) {
    configuration.options.charts.forEach(chart => {
      Chartjs.defaults[chart.type] = chart.defaults || {};
      if (chart.baseType) {
        Chartjs.controllers[chart.type] = Chartjs.controllers[chart.baseType].extend(chart.controller);
      } else {
        Chartjs.controllers[chart.type] = Chartjs.DatasetController.extend(chart.controller);
      }
    });
  }
  ...
}

which can then be used like so:

var myChartOptions = {
  charts: [{
    type: 'custom',
    baseType: 'bar',
    controller: {
     draw: function(ease) {},
      ...
    },
    defaults: {
      ...
    },
  }]
}

Would a pull request for this be accepted possibly?

@SeanSobey yes absolutely 🎉. However because we don't use this functionality - we would need a test which verifies we can make custom chart types.

Great, I have created a pull request.