Continuation of data join lesson for Civil War
https://gist.github.com/codetricity/3f4958488fdf87b912988bfa77384661
https://codetricity.github.io/d3-maps/civilwarjoin.html
- standard graphic file that people normally use
- jpg, png, tiff are all bitmap images
- loss of resolution when scaling
- edit in GIMP or Photoshop as well as most graphics programs
- most common format
SVG needs to be edited in Illustrator or InkScape.
Download image.
https://raw.githubusercontent.com/codetricity/d3-maps/master/assets/civilwarmap.jpg
svg.append('image')
.attr('xlink:href', 'assets/civilwarmap.jpg')
.attr('x', '0')
.attr('y', '0')
.attr('width', '600')
.attr('height', '500')
.attr('opacity', '0.2');
- https://raw.githubusercontent.com/codetricity/d3-maps/master/assets/border.png
- https://raw.githubusercontent.com/codetricity/d3-maps/master/assets/confederate.png
- https://raw.githubusercontent.com/codetricity/d3-maps/master/assets/union.png
assign a variable background
to the background image you created above.
- go to buttons event handler
on.('change',...
- go to the if statement for
selection == union
- add
background.attr...
- change opacity to 1.0
Answer and example code is below the next image.
const buttons = d3.selectAll('input');
buttons.on('change', function(d) {
console.log('button changed to ' + this.value);
const selection = this.value;
if (selection == 'union') {
background
.attr('xlink:href', 'assets/union.png')
.attr('opacity', '1.0');
listAlliance(union);
Assume that there is a new function called, listAllStates
. Pass all there alliance arrays to the new function.
} else if (selection == 'all') {
background
.attr('xlink:href', 'assets/civilwarmap.jpg')
.attr('opacity', '0.2');
listAllStates(union, confederate, border);
}
function listAllStates(union, confederate, border) {
svg.selectAll('text').remove();
svg.append('text')
.text('Union')
.attr('x', '50')
.attr('y', '50')
.style('font-weight', 'bold');
union.forEach((element, i) => {
svg.append('text')
.text(element)
.attr('x', '50')
.attr('y', i * 20 + 80);
});