Dr. Luciano Nocera
Find soft spot
by achieving balance
First use the space to help users understand the data, then decorate with a purpose!
Beauty is not the goal of visualization and it is usually not required to achieve the goal… remember that the goal is to enlighten.
Do not underestimate users and cater to the least common denominator: not all readers are equal!
create graphics that do not simplify but clarify
add appropriate Boom effect with artistry to attract the reader.
I want my readers to flip the page and, boom! The infographic shows up as an explosion! -- Luiz Iria
Elegance in visuals is attained when the complexity of the data matches the simplicity of the design
Chartjunk
The interior decoration of graphics generates a lot of ink that does not tell the viewer anything new.
The purpose of decoration varies — to make the graphic appear more scientific and precise, to enliven the display, to give the designer an opportunity to exercise artistic skills.
Regardless of its cause, it is all non-data-ink or redundant data-ink, and it is often chartjunk.
Use humor to instill affection in readers for numbers and charts
Javascript | Initial DOM | Final DOM |
---|---|---|
|
[Blank Page]
|
|
Javascript | Initial DOM | Final DOM |
---|---|---|
|
[Blank Page]
|
[Blank Page]
|
Javascript | Initial DOM | Final DOM |
---|---|---|
|
A B
|
A B 15
|
Javascript | Initial DOM | Final DOM |
---|---|---|
|
A B
|
A B
|
Javascript | Initial DOM | Final DOM |
---|---|---|
|
A B
|
A B 5 10 15
|
.attr()
to set attributes, e.g., class
.style()
to set style parameters.text()
to set inner text.attr()
to place and size, e.g., x
, width
.style()
to configure and update appearance
var el = d3.select('body') //select body
var el = d3.select('#div0') //select div with id div0
var el = d3.select('#svg0') //select svg with id svg0
<svg id='svg0' width='300' height='100'></svg>
Dynamically
var svg = d3.select('body')
.append('svg')
.attr('width', '300')
.attr('height', '100');
<svg width="60px" height="60px" style="background-color: lightgrey">
<!-- First data point -->
1
<!-- Second data point -->
2
<!-- Third data point -->
3
</svg>
<body>
<svg width="60px" height="60px" style="background-color: lightgrey" id="chart1"></svg>
<script>
var dataset = [{name: '1', color: 'red', width: 20, height: 20},
{name: '2', color: 'green', width: 20, height: 40},
{name: '3', color: 'blue', width: 20, height: 60}];
d3.select('#chart1')
.selectAll('rect')
.data(dataset)
.enter()
.append('rect')
.attr('x', function (d, i) { return i * d.width; })
.attr('y', function (d) { return 60 - d.height; })
.attr('width', function (d) { return d.width; })
.attr('height', function (d) { return d.height; })
.attr('fill', function (d) { return d.color;})
.append('text') // NOT SEEN!
.attr('x', function (d, i) { return i * d.width; })
.attr('y', function (d) { return 60; })
.attr('font-size', '18px')
.attr('fill', 'white')
.text(function (d) { return d.name; })
</script>
</body>
<body>
<svg width="60px" height="60px" style="background-color: lightgrey" id="chart2"></svg>
<script>
var dataset = [{name: '1', color: 'red', width: 20, height: 20},
{name: '2', color: 'green', width: 20, height: 40},
{name: '3', color: 'blue', width: 20, height: 60}];
var svg = d3.select('#chart2');
svg.selectAll('rect')
.data(dataset)
.enter()
.append('rect')
.attr('x', function (d, i) { return i * d.width; })
.attr('y', function (d) { return 60 - d.height; })
.attr('width', function (d) { return d.width; })
.attr('height', function (d) { return d.height; })
.attr('fill', function (d) { return d.color;});
svg.selectAll('text')
.data(dataset)
.enter()
.append('text')
.attr('x', function (d, i) { return i * d.width; })
.attr('y', function (d) { return 60; })
.attr('font-size', '18px')
.attr('fill', 'white')
.text(function (d) { return d.name; })
</script>
</body>
<body>
<svg width="60px" height="60px" style="background-color: lightgrey" id="chart2"></svg>
<script>
var dataset = [{name: '1', color: 'red', width: 20, height: 20},
{name: '2', color: 'green', width: 20, height: 40},
{name: '3', color: 'blue', width: 20, height: 60}];
var enter_selection = d3.select('#chart2')
.selectAll('rect')
.data(dataset)
.enter();
enter_selection.append('rect')
.attr('x', function (d, i) { return i * d.width; })
.attr('y', function (d) { return 60 - d.height; })
.attr('width', function (d) { return d.width; })
.attr('height', function (d) { return d.height; })
.attr('fill', function (d) { return d.color;});
enter_selection.append('text')
.attr('x', function (d, i) { return i * d.width; })
.attr('y', function (d) { return 60; })
.attr('font-size', '18px')
.attr('fill', 'white')
.text(function (d) { return d.name; })
</script>
</body>
A promise allows to associate handlers with asynchronous actions
let myFirstPromise = new Promise((resolve, reject) => {
// We call resolve(...) when what we were doing asynchronously was successful, and reject(...) when it failed.
// In this example, we use setTimeout(...) to simulate async code.
setTimeout( function() {
resolve("Success!") // Yay! Everything went well!
}, 250)
})
myFirstPromise.then((successMessage) => {
// successMessage is whatever we passed in the resolve(...) function above.
// It doesn't have to be a string, but if it is only a succeed message, it probably will be.
console.log("Yay! " + successMessage)
});
$ cat > cars.csv
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
d3.csv("cars.csv").then(function (data) {
console.log(data);
});
[{Year: "1997", Make: "Ford", Model: "E350", Length: "2.34"},
{Year: "2000", Make: "Mercury", Model: "Cougar", Length: "2.38"}]
parseInt()
and parseFloat()
parseInt('10'); //int 10
parseFloat('10.1'); //float 10.1
unary + operator(faster)
+'' //int 0
+'1' //int 1
+'1.1' //float 1.1
[
{"year": 1997, "make": "Ford", "model": "E350", "length": 2.34},
{"year": 2000, "make": "Mercury", "model": "Cougar", "length": 2.38}
]
$ cat > cars.json
[{"year": 1997, "make": "Ford", "model": "E350", "length": 2.34},
{"year": 2000, "make": "Mercury", "model": "Cougar", "length": 2.38}]
d3.json("cars.json").then(function (data) {
console.log(data);
//prints to the console
//[{year: 1997, make: "Ford", model: "E350", length: 2.34},
// {year: 2000, make: "Mercury", model: "Cougar", length: 2.38}]
});
Color
Red
Green
Blue
<p>Orange</p>
<script>
d3.csv("data.csv").then(function(data) {
d3.select("body")
.selectAll("p")
.data(data)
.enter()
.append("p")
.text(function(d) {return d.Color; })
});
</script>