AnyChart/AnyChart-NodeJS

Error on example

Berkmann18 opened this issue · 6 comments

Output

/home/maxie/Projects/ac-learn/node_modules/anychart-nodejs/lib/anychart-node.js:755
    setAsRootDocument(anychart.global().document);
                               ^

TypeError: anychart.global is not a function
    at AnychartExportWrapper (/home/maxie/Projects/ac-learn/node_modules/anychart-nodejs/lib/anychart-node.js:755:32)
    at Object.<anonymous> (/home/maxie/Projects/ac-learn/src/chart.js:16:18)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
    at internal/main/run_main_module.js:17:11

Code:

/* chart.js */
// require file system and jsdom
const fs = require('fs');
const anyc = require('anychart-nodejs');

// For jsdom version 10 or higher.
// Require JSDOM Class.
const JSDOM = require('jsdom').JSDOM;
// Create instance of JSDOM.
const jsdom = new JSDOM('', {runScripts: 'dangerously'});
// Get window
const window = jsdom.window;

// require anychart and anychart export modules
const anychart = anyc(window);
const anychartExport = anyc(anychart);

// create and a chart to the jsdom window.
// chart creating should be called only right after anychart-nodejs module requiring
const chart = anychart.pie([10, 20, 7, 18, 30]);
chart.bounds(0, 0, 800, 600);
chart.container('container');
chart.draw();

// generate JPG image and save it to a file
/* eslint-disable no-console */
anychartExport.exportTo(chart, 'jpg').then((image) => {
  fs.writeFile('anychart.jpg', image, (fsWriteError) => {
    console.log(fsWriteError || 'Complete');
  });
}, (generationError) => {
  console.log(generationError);
});
/* eslint-enable no-console */

How to reproduce:

  1. npm i jsdom anychart-nodejs -D
  2. node chart.js

@Berkmann18
Please, make sure that all sub dependencies are installed correctly, especially anychart package.
The anychart.global() returns Window object. anychart is a global variable if the package is installed correctly and included to the running project.

@Shestac92 They are. I've clean re-installed it like twice and still get this error.

@Berkmann18
anychart and anychartExport are required in wrong way. Please, try to install them manually along with anychart-nodejs:

npm i jsdom anychart-nodejs anychart -D

After that replace

const anychart = anyc(window);
const anychartExport = anyc(anychart);

with the following code:

const anychart = require('anychart')(window);
const anychartExport = require('anychart-nodejs')(anychart);

This should solve the issue.

I tried that and got a different issue 😕.

Error: 1
Description: Container is not set or can not be properly recognized. Use container() method to set it. 
TypeError: Cannot read property 'split' of null
    at HTMLUnknownElement.getBBox (/home/maxie/Projects/ac-learn/node_modules/anychart-nodejs/lib/anychart-node.js:155:29)
    at Aca (/home/maxie/Projects/ac-learn/node_modules/anychart/dist/js/anychart-bundle.min.js:492:141)
    at $.qv.$.g.X (/home/maxie/Projects/ac-learn/node_modules/anychart/dist/js/anychart-bundle.min.js:1301:79)
    at Xv (/home/maxie/Projects/ac-learn/node_modules/anychart/dist/js/anychart-bundle.min.js:508:132)
    at YK.$.g.CE (/home/maxie/Projects/ac-learn/node_modules/anychart/dist/js/anychart-bundle.min.js:1383:398)
    at YK.$.g.Jj (/home/maxie/Projects/ac-learn/node_modules/anychart/dist/js/anychart-bundle.min.js:1343:217)
    at YK.$.g.X (/home/maxie/Projects/ac-learn/node_modules/anychart/dist/js/anychart-bundle.min.js:1344:485)
    at /home/maxie/Projects/ac-learn/node_modules/anychart-nodejs/lib/anychart-node.js:460:20
    at loadExternalResources (/home/maxie/Projects/ac-learn/node_modules/anychart-nodejs/lib/anychart-node.js:345:12)
    at getSvg (/home/maxie/Projects/ac-learn/node_modules/anychart-nodejs/lib/anychart-node.js:383:12)

But the chart.container('container') is still there.

@Berkmann18
Ok! It means we are almost there!
There are two things we should modify.

  1. You create JSDOM like this:
const jsdom = new JSDOM('', {runScripts: 'dangerously'});

And then applying chart container with ID container:

chart.container('container');

But there's no such DIV with id container.
You should create JSDOM like this:

var jsdom = new JSDOM('<body><div id="container"></div></body>', {runScripts: 'dangerously'});
  1. With using the latest AnyChart version you will receive TypeError: Cannot read property 'split' of null. To avoid it you should install exactly 8.3.0 version of AnyChrt package. This issue was discussed here.

This should solve the last issue.

Sweet, it did.
Thank you.