Built with blockbuilder.org
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body {
background-color: #ffffff;
}
.line {
fill: none;
stroke: MidnightBlue;
stroke-width: 2px;
}
.grid line {
stroke: lightgrey;
stroke-opacity: 0.6;
shape-rendering: crispEdges
}
.grid path {
stroke-width: 0;
}
.area {
fill: none;
clip-path: url(#clip);
}
.dot:hover {
r: 4px;
stroke-width: 3px;
}
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 20, bottom: 100, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseTime = d3.timeParse("%d-%b-%y");
var formatTime = d3.timeFormat("%b %e %y");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var valueline = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.lbs); });
var area = d3.area()
.curve(d3.curveMonotoneX)
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.lbs); });
var xAxis = d3.axisBottom(x),
yAxis = d3.axisLeft(y);
var zoom = d3.zoom()
.scaleExtent([1,32])
.translateExtent([0,0],[width,height])
.extent([0,0],[width,height])
.on("zoom",zoomed)
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 + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
function make_x_gridlines() {
return d3.axisBottom(x)
.ticks(20)
}
function make_y_gridlines() {
return d3.axisLeft(y)
.ticks(12)
}
d3.tsv("myLbs.tsv",function(error,data){
if (error) throw error;
data.forEach(function(d){
d.date = parseTime(d.date)
d.lbs = +d.lbs;
});
x.domain(d3.extent(data, function(d){ return d.date}));
y.domain([200, d3.max(data, function(d) { return d.lbs; })]);
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(12))
.selectAll("text")
.style("text-anchor","end")
.attr("dx","-.8em")
.attr("dy",".15em")
.attr("transform","rotate(-45)");
svg.append("g")
.attr("class","axis")
.call(d3.axisLeft(y)
);
svg.append("g")
.attr("class","grid")
.attr("transform","translate(0," + height + ")")
.style("stroke-dasharray",("3,3"))
.call(make_x_gridlines()
.tickSize(-height)
.tickFormat("")
)
svg.append("g")
.attr("class","grid")
.style("stroke-dasharray",("3,3"))
.call(make_y_gridlines()
.tickSize(-width)
.tickFormat("")
)
svg.append('path')
.data([data])
.attr('class','line')
.attr('d',valueline)
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 3)
.attr("class","dot")
.attr("fill","IndianRed")
.attr("stroke","Orange")
.attr("stroke-width","1px")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.lbs); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .8);
div.html(formatTime(d.date) + "<br/>" + d.lbs + " lbs")
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
});
function zoomed() {
var t = d3.event.transform, xt = t.rescaleX(x);
g.select(".area").attr("d", area.x(function(d) { return xt(d.date); }));
g.select(".axis--x").call(xAxis.scale(xt));
}
</script>
</body>
https://d3js.org/d3.v4.min.js