This document introduces d4, a charting DSL for D3 that aims to provide reusable chart components ("jigs") to simplify the creation of visualizations. It discusses key insights around treating charts as templates to guide data through D3, favoring context over configuration, and separating concerns between design and code. Features include base chart definitions, parsers to prepare data, and methods to add, remove or style features directly on the chart object. The goal is to reduce complexity and boost productivity for visualization development with D3.
3. –Garry Klass
“A graphical chart provides a visual
display of data that otherwise would be
presented in a table; a table, one that
would otherwise be presented in text.
Ideally, a chart should convey ideas
about the data that would not be readily
apparent if they were displayed in a table
or as text.”
4. Charts are not about displaying
data, they are about conveying
ideas visually through data.
5. Successful charts are succinct, self-
explanatory visual systems which answer
questions about the comparison,
composition, distribution or interrelationship
in the data they present.
7. Being fit for purpose is hard to do using
reusable charts because visual
expressiveness is often the first quality
scraped away when trying to wedge an
idea into the mold of a pre-made chart.
10. –Hunt & Thomas “The Pragmatic Programmer: From Journeyman to Master”
“When woodworkers are faced with the task of
producing the same thing over and over, they
cheat. They build themselves a jig or a
template. If they get the jig right once, they can
reproduce a piece of work time after time. The
jig takes away complexity and reduces the
chances of making mistakes, leaving the
craftsman free to concentrate on quality.”
11. Data is your raw material
D3 is a tool for visually shaping !
your data
d4 guides your data !
through D3
Image from: www.finewoodworking.com
12. A chart is a jig
• Used to control the process our output of another
tool
• Provides repeatability, accuracy and
interchangeability in the production process
• Simplifies a complexities of a task
• Acts as a template or a guide
13. Elements of a d4 jig
• Chart - A collection of features, and a builder which renders
data using d3 into a graphical representation.
• Builder - A routine to apply sensible defaults to the features.
• Feature - A visual component of a chart, which helps
convey meaning in the data.
• Dimension - A segment of the data described by the chart.
• Parser - A routine to prepare incoming data into a format
appropriate for a given chart.
17. There is a software design concept called
“convention over configuration,” which states that
software should specify a collection of
opinionated defaults for the developer.
!
The goal of this approach is to lessen the number
of obvious choices a developer must make
before they are able to use the software.
18. d4 extends this concept a bit and suggests that
configuration should also be highly contextual to
the object the developer needs changed.
!
Instead of making choices in some abstract
config file, developers instead use a highly
declarative API to make changes directly to the
object they want augment.
19. A Basic Chart
'use strict';
!
$(document).ready(function(){
var data = [
{ x: '2010', y:-10 },
{ x: '2011', y:20 },
{ x: '2012', y:30 },
{ x: '2013', y:40 },
{ x: '2014', y:50 },
];
!
var chart = d4.charts.column()
!
// lets get rid of the yaxis
.mixout(‘yAxis');
!
d3.select('#example')
.datum(data)
.call(chart);
});
#mixout()
d4’s base charts make assumptions on what features are wanted. It
is easy to remove an unwanted feature using mixout().
20. A Basic Chart
'use strict';
!
$(document).ready(function(){
var data = [
{ x: '2010', y:-10 },
{ x: '2011', y:20 },
{ x: '2012', y:30 },
{ x: '2013', y:40 },
{ x: '2014', y:50 },
];
!
var chart = d4.charts.column()
.mixin({
'name' : 'zeroLine',
'feature' : d4.features.referenceLine
});
!
d3.select('#example')
.datum(data)
.call(chart);
});
#mixin()
zeroLine
You can add features by mixing them in. However, some features do not look
that great using their default settings, as is the case with our reference line.
21. A Basic Chart
'use strict';
!
$(document).ready(function(){
var data = [
{ x: '2010', y:-10 },
{ x: '2011', y:20 },
{ x: '2012', y:30 },
{ x: '2013', y:40 },
{ x: '2014', y:50 },
];
!
var chart = d4.charts.column()
.mixin({
'name' : 'zeroLine',
'feature' : d4.features.referenceLine
})
.using('zeroLine', function(zeroLine){
zeroLine
.x1(0)
.y1(function(){
return this.y(0);
})
.x2(chart.outerWidth() -
chart.marginLeft() - chart.margin().right)
.y2(function(){
return this.y(0);
});
});
d3.select('#example')
.datum(data)
.call(chart);
});
#using()
You can control a feature’s attributes with the using() function.
23. Separation of
Responsibilities
• Designers should not need to know JavaScript to
style the chart and vice versa
• The chart does not own the data, and any
transformations should be made on a copy of the
data
• Don’t add needless abstractions, where possible
leverage or proxy existing functionality in d3 or svg.
24. d4 and CSS
d4 adds several generated
CSS classes to various
elements of the chart as it
renders.
!
The ability to control the
series colors changes the
story your charts are telling,
allowing you to choose the
correct visual representation
of your series data to
support the point you are
trying to make.
These is the same chart, using the same data. The only thing
that has changed is which CSS classes are being accessed.