1

Given a JS script running a D3js dynamic dataviz

enter image description here

I currently print out a single view into a output.svg file using :

svgcreator.node.js :

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS MAP DRAWING CODE * * * * * * * * * * * * * * * * * * * 
  function (err, window) {

    var svg = window.d3.select("body")
        .append("svg")                 // append svg on body
       ...                             // more D3js code
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

Terminal NodeJS command :

node svgcreator.node.js > output.svg

The way it works is that each console.log() message from svgcreator.node.js script is printed into the file output.svg file.

Since this D3js dataviz turn around the globe, It should be possible to print out one console.log() message for each view into a proper svg file named correctly. I thing I should get a variable such var country=...; from the JS script up to the terminal. Then, I could do (?) something such :

node svgcreator.node.js > $(JsVar.country).svg    # nb: this var syntax if fictional 

So whenever a console.log() message come, the message in printed into an SVG file with the correct name.

Also, how to pass JS variable's value from script to terminal ?

See also: Node.js : how to pass parameter's value from terminal to JS script

Community
  • 1
  • 1
Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • I just tried in JS code to do `process.env.country = currentCountryName;`, followed by variants of `node svgcreator.node.js > $country.svg` in the terminal without success. – Hugolpz Sep 02 '14 at 21:20
  • Seems it's quite tricky : http://stackoverflow.com/questions/22681043/ – Hugolpz Sep 02 '14 at 22:28
  • Are you just looking for a way to save the output to an appropriately named file? Does the file need to be created via stdout redirect in the shell, or would it be ok for node itself to create the file? – Mike S Sep 02 '14 at 22:51
  • I'am new to nodejs and use the ways I'am aware about, but It's ok for node to create the file as soos as it's a valid SVG whose code was made from my D3js code. – Hugolpz Sep 02 '14 at 23:05

1 Answers1

1

The easiest thing to do is to just have node write to the file directly instead of trying to use > somefile.svg.

The code to do that is pretty simple. Let's say you have a variable in your script called country that contains the name of the country that you want to create an .svg for. Instead of using console.log to output your .svg file, write it out to country + '.svg' instead using fs.writeFileSync.

So, this:

console.log(window.d3.select("body").html());

Would change to this:

fs.writeFileSync(country + '.svg', window.d3.select("body").html());

Note: Don't forget to var fs = require('fs'); to get access to the fs module.

Mike S
  • 41,895
  • 11
  • 89
  • 84