Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.js graphs output into high resolution print quality files?

Is there a way to output graphs, charts, maps etc created in html/js based on d3.js into other data format with publication print quality high resolution?

The graphics of these charts are fantastic but would be useless when printed on paper and got highly pixelated. I am trying to avoid replotting them in Illustrator for vectors or photoshop.

The output formats that I'm looking for should be readable to Illustrator or Photoshop. And most preferably without much more visual manipulation needed once exported. It would really be defeats the purpose if I would have to replot or refill color or rephotoshop it to get the effect.

Thanks!

like image 502
FongYu Avatar asked Oct 04 '12 02:10

FongYu


People also ask

How do you make a graph in D3 JS?

Step 1 − Adding styles − Let us add a style to the line class using the code given below. Step 2 − Define variables − The SVG attributes are defined below. var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = 960 - margin. left - margin.

What is D3 SVG?

js (also known as D3, short for Data-Driven Documents) is a JavaScript library for producing dynamic, interactive data visualizations in web browsers. It makes use of Scalable Vector Graphics (SVG), HTML5, and Cascading Style Sheets (CSS) standards. It is the successor to the earlier Protovis framework.

Is d3js open-source?

js, or D3, stands for data-driven documents. It is an open-source JavaScript library that creates interactive data visualizations in a web browser using SVG, HTML, and CSS.

How do I download d3js?

Download D3. D3. js is an open-source library and the source code of the library is freely available on the web at https://d3js.org/ website. Visit the D3. js website and download the latest version of D3.


2 Answers

There are more complicated methods but a quick, easy way is to just copy the svg element from the DOM (you may need to include your css files as well), paste it into a file and save it with the extension .svg. After that you can open it in a vector editor.

There are also ways to convert the d3.js output to a png file as well. Somebody put together a jsfiddle of doing this with canvg at http://jsfiddle.net/plaliberte/HAXyd/.

like image 106
Bill Avatar answered Sep 19 '22 02:09

Bill


Modern browsers support the download attribute on links. If you create a link to an image with the download attribute, the browser will download it instead of opening it within the browser.

Since there's no actual file to download, what you have to do is to encode your svg string as a data-uri, All you have to do is...

var download = d3.select("body").append("a").attr("href", "#")  download.on("click", function(){       d3.select(this)         .attr("href", 'data:application/octet-stream;base64,' + btoa(d3.select("#viz").html()))         .attr("download", "viz.svg")      }) 

You can see a demo here http://bl.ocks.org/3831266 you can open the downloaded file in illustrator without any problems.

There are a couple of gotchas however: you must declare your styles inline (you can't style with an external css stylesheet).

A quick and dirty solution is to output the svg code to an alert box:

download.on("click", function(){   alert(d3.select("#viz").html()) }); 

And copy the alert into a text file and save as svg.

like image 34
methodofaction Avatar answered Sep 21 '22 02:09

methodofaction