Learn pattern to change data on the same chart. For example, change data from Union to Confederate.
- selectAll( thing to draw such as circle )
- data - binds data to the thing to draw such as circle
- enter - new stuff to add
- append (thing to draw such as circle )
- exit() is used to delete unused objects on screen
- update existing objects on screen with new data (such as circle color or size)
- select
- data
- exit - remove unused objects
- update existing objects
- enter
- append
https://bl.ocks.org/codetricity/raw/3f4958488fdf87b912988bfa77384661/
- Use the same data file on Civil War
- create new civilwar.js file or similar. Create new html file or edit existing
- read in data
d3.csv('civil-war.csv').then((data) => {
- create blank arrays for union, confederate, border
- console.log the data so you can see what it looks like
Note that the data is an array of objects.
Take out each object from the array.
The command below will loop through the array and return each element.
data.forEach((element) => {
Use console.log to print out each object.
Each object has three keys.
Question: What are the keys?
This command will loop through each object.
for (var alliance in element) {
Pattern: for
(variable in
object)
console.log to print out each key for every object. console.log(alliance)
The name of the object is element.
The name of the key is alliance.
The value is element[alliance]
use console.log to print out each value.
Assign value to variable stateName
data.forEach((element) => {
for (var alliance in element) {
let stateName = element[alliance];
if (stateName != "") {
if (alliance == 'union') {
union.push(stateName);
} else if (alliance == 'confederate') {
confederate.push(stateName);
... fill in rest
function listAlliance(allianceData) {
/**
* Pattern:
* 1. select
* 2. data join
* 3. exit and remove excess information on screen
* 4. update information on screen
* 5. enter
* 6. append new items
*/
const allianceText = svg.selectAll('text') // 1. select
.data(allianceData); // 2. data join
Use console.log on allianceText to inspect the output.
This removes items on screen that are not in the data array.
allianceText.exit()
Use console to see the results of alliance.exit()
Remove objects that are unused.
allianceText.exit().remove(); // 3. exit and remove excess information on screen
Update existing elements on screen with the new data.
allianceText.text(d => d); // 4. update information on screen
Find data that needs to be added to screen.
allianceText.enter() // 5. enter and return list of information that needs to be on screen
.append('text') // 6. append
.text((d) => d)
.attr('x', '50')
.attr('y', (d, i) => i * 20 + 50);
<form>
<input type="radio" value="union" name="alliance">
<label>
Union
</label>
<input type="radio" value="confederate" name="alliance" >
<label>
Confederate
</label>
<input type="radio" value="border" name="alliance" >
<label>
Border
</label>
</form>
const buttons = d3.selectAll('input');
buttons.on('change', function(d) {
console.log('button changed to ' + this.value);
const selection = this.value;
if (selection == 'union') {
listAlliance(union);
... fill in the rest
- what does
forEach
do? Is it for an array or an object? - what does
for ... in ...
do? - There are 6 steps of the pattern to update data on the chart. Step 1 is
select
. Step 6 isappend
What are steps 2, 3, 4? - What does
enter()
return? - What does
exit()
return?