fivethirtyeight/d3-pre

Name: d3-pre

Owner: FiveThirtyEight

Description: Pre-render d3 visualizations

Created: 2016-01-08 01:54:27.0

Updated: 2018-01-16 08:48:26.0

Pushed: 2016-10-08 19:07:12.0

Homepage:

Size: 2112

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits
Aaron Bycoffe2016-07-14 21:18:02.02
Andrei Scheinkman2016-10-05 20:49:49.01
Matthew Conlen2016-10-08 19:07:12.079
Zlatan Vasovi?2016-10-08 18:45:17.01

Other Committers

UserEmailMost Recent Commit# Commits

README

d3-pre

A JavaScript library that pre-renders d3 visualizations into inline SVG elements, to reduce perceived page-load time and cut down on unwanted paint flashes.

The pre-rendering tool uses a headless browser to turn d3 code into its resulting SVG, and inserts the markup into your HTML. Then, the d3-pre JavaScript library overrides d3.append to check if a pre-rendered DOM node already exists before creating a new one. This approach allows you to use pre-rendered SVG without changing your visualization code.

See an example of the speed benefits of using inline SVG over SVG generated in the client by refreshing this page without pre-rendering and with pre-rendering.

Examples
In the wild
Installation
install --save d3-pre
Usage

There are two things that you need to do to use this library:

1. Include d3-pre in your javascript
d3 = require('d3');

equire the library and give it a reference to d3
Prerender = require('d3-pre');
prerender = Prerender(d3);


hen, when you start drawing SVG, call `prerender.start()`.
his modifies some d3 functions to make it aware
f the pre-rendered SVGs.
ender.start();


xisting d3 code goes here
3.select('body')
 .append('svg')
    .data(data)
 .enter()
    .append('rect')
    .on('click', clickhandler)
    etc. etc.


f you ever want to go back to the unmodified d3,
ust call `prerender.stop()`.
his is optional and usually not necessary.
ender.stop();
2. Pass your HTML through the pre-rendering tool

This can be done via a build task (like gulp), or on the command line. To provide control over which DOM modifications are saved back to the HTML file, you can add the following data-attributes in the HTML:

Command line example

Install the command line tool:

m install -g d3-pre-cli

Run the d3-pre command on an HTML file:

-pre ./path/to/index.html

This command will open the index.html file in a headless browser, running any JavaScript included on the page. Any modifications that the JavaScript makes to the DOM are saved back to the HTML file.

Gulp example

Install the gulp plugin:

m install gulp-d3-pre

Create a gulp task:

gulp = require('gulp');
d3Pre = require('gulp-d3-pre');


.task('prerender-svgs', function() {
lp.src('./public/index.html')
.pipe(d3Pre(options))
.pipe(gulp.dest('./public/'));

This task will open the index.html file in a headless browser, running any JavaScript included on the page. Any modifications that the JavaScript makes to the DOM are saved back to the HTML file. The following options may be passed to the gulp plugin:

Advanced usage

Both of the above modules are thin wrappers around d3-pre-renderer. If you require more fine-grained control of when and where the pre-rendering step takes place, use d3-pre-renderer directly.

Release Notes
v1.3.0
Contributors
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.