JSFiddle - React, Tailwind, and code Playground

by HemalathaThanigaivel

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

CSS

.bar { fill: gray; }
.actualbar{ fill: steelblue; }

JavaScript

var data = [{"salesperson":"Bob","sales":33, "actual": 10},{"salesperson":"Robin","sales":12,"actual":8},{"salesperson":"Anne","sales":41,"actual":30},{"salesperson":"Mark","sales":16,"actual":15},{"salesperson":"Joe","sales":59,"actual":26},{"salesperson":"Eve","sales":38,"actual":26},{"salesperson":"Karen","sales":21,"actual":17},{"salesperson":"Kirsty","sales":25,"actual":14},{"salesperson":"Chris","sales":30,"actual":19},{"salesperson":"Lisa","sales":47,"actual":36},{"salesperson":"Tom","sales":5,"actual":2},{"salesperson":"Stacy","sales":20, "actual": 10},{"salesperson":"Charles","sales":13, "actual":10},{"salesperson":"Mary","sales":29,"actual": 19}];

// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// set the ranges
var y = d3.scaleBand()
          .range([height, 0])
          .padding(0.1);

var x = d3.scaleLinear()
          .range([0, width]);
          
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").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 + ")");

  // format the data
  data.forEach(function(d) {
    d.sales = +d.sales;
  });

  // Scale the range of the data in the domains
  x.domain([0, d3.max(data, function(d){ return d.sales; })])
  y.domain(data.map(function(d) { return d.salesperson; }));
  //y.domain([0, d3.max(data, function(d) { return d.sales; })]);

  // append the rectangles for the bar chart
  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      //.attr("x", function(d) { return x(d.sales); })
      .attr("width", function(d) {return x(d.sales); }...