d3.v7.js で棒グラフを作成

こちらのプログラムを参考にしました。
[d3noob](https://gist.github.com/d3noob/08cc6159b9315e20e74a72e85a50dd3e)
[D3.js tutorial: Build your first bar chart](https://www.educative.io/blog/d3-js-tutorial-bar-chart)

描画した結果
![d3_bar_dec15_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/b45c0833-a796-e944-c80d-d27c97ffee5b.png)

```html:index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Bar Graph</title>
<style>
.bar { fill: steelblue; }
</style>
</head>
<body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="bar_chart_sales.js"></script>
<br />
Dec/15/2021<br />
</body>
</html>
```

```js:bar_chart_sales.js
// ------------------------------------------------------------------------
//	bar_chart_sales.js
//
//					Dec/15/2021
// ------------------------------------------------------------------------
// 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 xx = d3.scaleBand()
          .range([0, width])
          .padding(0.1);
var yy = d3.scaleLinear()
          .range([height, 0]);
          
// 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 + ")");

// ------------------------------------------------------------------------
// get the data
d3.json("sales.json").then(function(data) {

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

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

  // append the rectangles for the bar chart
  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(dd) { return xx(dd.salesperson); })
      .attr("width", xx.bandwidth())
      .attr("y", function(dd) { return yy(dd.sales); })
      .attr("height", function(dd) { return height - yy(dd.sales); });

  // add the x Axis
  svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(xx));

  // add the y Axis
  svg.append("g")
      .call(d3.axisLeft(yy));

});

// ------------------------------------------------------------------------
```

```json:sales.json
[
{"salesperson": "Bob","sales": 33 },
{"salesperson": "Robin","sales": 12 },
{"salesperson": "Anne","sales": 41 },
{"salesperson": "Mark","sales": 16 },
{"salesperson": "Joe","sales": 59 },
{"salesperson": "Eve","sales": 38 },
{"salesperson": "Karen","sales": 21 },
{"salesperson": "Kirsty","sales": 25 },
{"salesperson": "Chris","sales": 30 },
{"salesperson": "Lisa","sales": 47 },
{"salesperson": "Tom","sales": 5 },
{"salesperson": "Stacy","sales": 20 },
{"salesperson": "Charles","sales": 13 },
{"salesperson": "Mary","sales": 29 }
]
```