JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<svg class="chart"></svg>
CSS
.bar {
fill: steelblue;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
JavaScript
var data = [
{name: "Locke", value: 4},
{name: "Reyes", value: 8},
{name: "Ford", value: 15},
{name: "Jarrah", value: 16},
{name: "Shephard", value: 23},
{name: "Kwon", value: 42}
];
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 500 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.domain(data.map(function(d) { return d.name; }))
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.value; })])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var chart = d3.select(".chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(d3.behavior.zoom().scaleExtent([1, 10]).on("zoom", zoom));
//Add a clip path, so the content outside the domain should be hidden
var mSvg = d3.select(".chart");
//Add a "defs" element to the svg
var defs = mSvg.append("defs");
//Append a clipPath element to the defs element, and a Shape
// to define the cliping area
defs.append("clipPath").attr('id','my-clip-path').append('rect')
.attr('width',width) //Set the width of the clipping area
.attr('height',height); // set the height of the clipping area
//clip path for x axis
defs.append("clipPath").attr('id','x-clip-path').append('rect')
.attr('width',width) //Set the width of the clipping area
.attr('height',height + margin.bottom); // set the height of the clipping area
//add a group that will be clipped (this will contain the bars)
var barsGroup = chart.append('g');
//Set the clipping path to the group (g element) that you want to clip
barsGroup.attr('clip-path','url(#my-clip-path)');
var xAxisGroup = chart.append("g").attr('class','x-axis')
xAxisGroup.append('g')
.attr("class", "x...