vmpowerio/chartjs-node

Cannot read property 'length' of null

Opened this issue ยท 3 comments

Hello,
I just simply installed this (also installed canvas as npm install canvas and chart.js as npm install chart.js) and used the following code. and I got the below error.

var myChartOptions = {
    plugins: {
        afterDraw: function (chart, easing) {
            var self = chart.config;    /* Configuration object containing type, data, options */
            var ctx = chart.chart.ctx;  /* Canvas context used to draw with */
        }
    }
};
// xy is an array object as [{x:0,y:1},.....]
var chartJsOptions = {
    type: 'scatter',
    data: {datasets:[{data:xy}]},
    options: myChartOptions
};
const ChartjsNode = require('chartjs-node');
var chartNode = new ChartjsNode(600, 600);
chartNode.drawChart(chartJsOptions)
.then(() => {
    // chart is created

    // get image as png buffer
    return chartNode.getImageBuffer('image/png');
})
.then(buffer => {
    if(Array.isArray(buffer)) return chartNode.getImageStream('image/png');
})
.then(streamResult => {
    return chartNode.writeImageToFile('image/png', './testimage.png');
})
.then(() => {
    // chart is now written to the file path
    // ./testimage.png
});`
```
Unhandled rejection TypeError: Cannot read property 'length' of null
    at Object.acquireContext (x:\\node_modules\chart.js\dist\Chart.js:6896:19)
    at Chart.construct (x:\\node_modules\chart.js\dist\Chart.js:8415:26)
    at new Chart (x:\\node_modules\chart.js\dist\Chart.js:8402:7)
    at jsdom.envAsync.then.window (x:\\node_modules\chartjs-node\index.js:106:31)
    at tryCatcher (x:\\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (x:\\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (x:\\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (x:\\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (x:\\node_modules\bluebird\js\release\promise.js:694:18)
    at Promise._fulfill (x:\\node_modules\bluebird\js\release\promise.js:638:18)
    at Object.done (x:\\node_modules\bluebird\js\release\nodeback.js:42:21)
    at process.nextTick (x:\\node_modules\jsdom\lib\jsdom.js:320:18)
    at process._tickCallback (internal/process/next_tick.js:61:11)

having the same issue

JSDom is a secondary dependency of this project and the version included has a bug. You can fix this by installing JSDom directly.

npm install jsdom

This issue seems to be related to #42 ... It seems that canvas version 2+ doesn't work on Win. If you downgrade canvas to 1.6.13, it works for us.