For reference, I have taken sample code from Mike Bostock's Line Chart Example. At all places I have commented original code and replaced it with new code to convert given example with Floating Scale.
xxxxxxxxxx
<meta charset="utf-8">
<style>
body
{
font: 12px sans-serif;
}
.axis path, .axis line
{
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path
{
display: none;
}
.line
{
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.floating
{
cursor: pointer;
stroke: #FF0000;
stroke-width: 1.5px;
}
.floatingLabel
{
cursor: pointer;
fill: #FF0000;
}
</style>
<body>
<div style="padding: 10px;">
<label >
Add new <B><I>Floating</I></B> axis line</label>
<input id="myValue" type="text" value="" />
<input type="button" value="Add" onclick="updateValues()" />
</div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://darshit-shah.github.io/d3.floatingscale.js"></script>
<script>
var margin = { top: 20, right: 50, bottom: 30, left: 50 },
width = 960 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.time.scale().range([0, width]);
//Replaced
//var y = d3.scale.linear().range([height, 0]);
var y = d3.svg.floatingScale().range([height, 0]).ticks(5);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
//Replaced
//var yAxis = d3.svg.axis().scale(y).orient("left");
var yAxis = y.axis().orient("left");
var line = d3.svg.line()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.close); });
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 + ")");
var data = [];
d3.tsv("data.tsv", function (error, localData) {
data = localData;
localData = null;
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
x.domain(d3.extent(data, function (d) { return d.date; }));
y.domain(d3.extent(data, function (d) { return d.close; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
//added extra
y.updateChart(updateChart).chart(svg);
});
function updateChart(delay, duration) {
svg.selectAll('.line').transition().delay(delay).duration(duration).attr("d", line);
svg.selectAll(".y.axis").transition().delay(delay).duration(duration).call(yAxis);
}
function updateValues() {
var value = parseFloat(document.querySelector('#myValue').value.trim());
y.addFloatingScaleLine(value);
}
</script>
</body>
Modified http://d3js.org/d3.v3.min.js to a secure url
Updated missing url https://darshit-shah.github.io/d3.floatingScale.js to https://darshit-shah.github.io/d3.floatingscale.js
https://d3js.org/d3.v3.min.js
https://darshit-shah.github.io/d3.floatingScale.js