my-archives / nchart

nChart for node.js inspired by Chart.js.
MIT License
48 stars 10 forks source link

Cannot set property 'canvas' of undefined #7

Closed metbosch closed 9 years ago

metbosch commented 9 years ago

When I try to run the example code on my installation I get the following error:

TypeError: Cannot set property 'canvas' of undefined at Chart (/usr/local/lib/node_modules/nchart/Chart.js:22:15) at repl:1:7 at REPLServer.self.eval (repl.js:110:21) at repl.js:249:20 at REPLServer.self.eval (repl.js:122:7) at Interface. (repl.js:239:12) at Interface.emit (events.js:95:17) at Interface._onLine (readline.js:202:10) at Interface._line (readline.js:531:8) at Interface._ttyWrite (readline.js:760:14)

The code was executed in the interactive console and the error appears on the call 'Chart(ctx)'.

fundon commented 9 years ago

Which is the example? Reinstall dependences and try again.

metbosch commented 9 years ago

The example on the README file:

var Canvas = require('canvas')
  , canvas = new Canvas(800, 800)
  , ctx = canvas.getContext('2d')
  , Chart = require('nchart')
  , fs = require('fs');

Chart(ctx).Pie(
    [
        {
            "value": 50
          , "color": "#E2EAE9"
        }
      , {
            "value": 100
          , "color": "#D4CCC5"
        }
      , {
            "value": 40
          , "color": "#949FB1"
        }
    ]
  , {
        scaleShowValues: true
      , scaleFontSize: 24
    }
);

canvas.toBuffer(function (err, buf) {
  if (err) throw err;
  fs.writeFile(__dirname + '/pie.png', buf);
});

Which dependencies? 'node-canvas'?

fundon commented 9 years ago

Yes, the canvas module is required.

$ npm install canvas
jurgen7777 commented 9 years ago

Looks like to get it working instead of

var Chart = require('nchart');

should be

var Chart = new (require('nchart'))(ctx);

and the following part

Chart(ctx).Pie(

should be

Chart.Pie(
fundon commented 9 years ago

Good catch! That's it. I should update these examples.

fundon commented 9 years ago

Fxed in v1.0.1.