JSFiddle - React, Tailwind, and code Playground

by Douglas Bahiense

HTML

<!-- Load Alpine.js -->
<script src="https://unpkg.com/alpinejs" defer></script>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v7.js"></script>

<div x-data="myComponent()">
  <!-- Add 2 buttons -->
  <button x-on:click="update(data1)">Data 1</button>
  <button x-on:click="update(data2)">Data 2</button>
</div>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

JavaScript

// create 2 data_set
const data1 = [
   {group: "A", value: 4},
   {group: "B", value: 16},
   {group: "C", value: 8}
];

const data2 = [
   {group: "A", value: 7},
   {group: "B", value: 1},
   {group: "C", value: 20},
   {group: "D", value: 10}
];

// set the dimensions and margins of the graph
const margin = {top: 30, right: 30, bottom: 70, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
const svg = d3.select("#my_dataviz")
  .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", `translate(${margin.left},${margin.top})`);

// Initialize the X axis
const x = d3.scaleBand()
  .range([ 0, width ])
  .padding(0.2);
const xAxis = svg.append("g")
  .attr("transform", `translate(0,${height})`)

// Initialize the Y axis
const y = d3.scaleLinear()
  .range([ height, 0]);
const yAxis = svg.append("g")
  .attr("class", "myYaxis")


function myComponent(){
	return {
    // A function that create / update the plot for a given variable:
    update(data){
      // Update the X axis
      x.domain(data.map(d => d.group))
      xAxis.call(d3.axisBottom(x))

      // Update the Y axis
      y.domain([0, d3.max(data, d => d.value) ]);
      yAxis.transition().duration(1000).call(d3.axisLeft(y));

      // Create the u variable
      var u = svg.selectAll("rect")
        .data(data)

      u
        .join("rect") // Add a new rect for each new elements
        .transition()
        .duration(1000)
          .attr("x", d => x(d.group))
          .attr("y", d => y(d.value))
          .attr("width", x.bandwidth())
          .attr("height", d => height - y(d.value))
          .attr("fill", "#69b3a2")
          
      // Initialize the plot with the first dataset
			// update(data1)
    }
  }
}