JSFiddle - React, Tailwind, and code Playground
HTML
<div id='chart'></div>
<div id='buttons'></div>
JavaScript
var width = 550,
height = 400,
z = d3.scale.category10();
// 3 datasets. d3 join will use name as the key.
// not all datasets have same name
// position matches name, each name will always be in the x position
// Each name appears in only some datasets. This will allow for enter/update/exiting in d3
var dataset1 = [
{name: 'Joe', value: 40, position: 0},
{name: 'Jim', value: 130, position: 2},
{name: 'Janet', value: 40, position: 3},
{name: 'Julie', value: 80, position: 5}
];
var dataset2 = [
{name: 'Joe', value: 100, position: 0},
{name: 'Janet', value: 80, position: 3},
{name: 'John', value: 110, position: 4},
{name: 'Julie', value: 100, position: 5}
];
var dataset3 = [
{name: 'Jane', value: 40, position: 1},
{name: 'Jim', value: 50, position: 2},
{name: 'John', value: 30, position: 4}
];
// array for the buttons, to be created with d3
var buttonData = ["One", "Two", "Three"];
// put all the data into an array for the button on click function
var dataArray =[dataset1, dataset2, dataset3];
// start with dataset1
var activeData = dataArray[0];
// svg element for the chart
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
// svg element for the buttons
var buttonDiv = d3.select('#buttons').append('svg')
.attr('width', 550)
.attr('height', 30);
// buttons, will update datasets .on('click')
var buttons = buttonDiv.selectAll('.updateButton')
.data(buttonData)
.enter()
.append('g')
.attr('class', 'updateButton')
.on('click', function(d, i) {
activeData = dataArray[i];
updateVis(activeData);
});
buttons
.append("rect")
.attr("x", function(d, i) { return (i * 100) + 20; })
.attr("width", 98)
.attr("height", 25)
.attr("ry", 5)
.style("stroke", "#787878")
.style("fill", "#80daeb");
buttons
.append("text")
.attr("x", function(d, i) { return (i * 100) + (100 / 2) + 20; })
.attr("y", 12)
.attr("dy",...