Skip to content

Instantly share code, notes, and snippets.

@enthal
Last active November 14, 2020 02:55
Show Gist options
  • Select an option

  • Save enthal/060290a72e8c29d9f160 to your computer and use it in GitHub Desktop.

Select an option

Save enthal/060290a72e8c29d9f160 to your computer and use it in GitHub Desktop.
series normalization per mouse-x (D3.js): https://bl.ocks.org/enthal/060290a72e8c29d9f160
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg .chart {
fill: none;
stroke: #000;
stroke-width: 0.5px;
}
svg rect.mouse {
fill: none;
pointer-events: all;
}
</style>
<body></body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script>
var n=15, seriesList = [[],[]];
for (var i=0;i<n;i++) { seriesList[0].push(Math.pow(2,i)); }
for (var i=0;i<n;i++) { seriesList[1].push(Math.pow(2,n) / (i+1)); }
var width = 960,
height = 500;
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var series = seriesList[0];
x.domain([0, series.length]);
y.domain(d3.extent(series));
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var chart = svg.append('g').classed('chart', true);
chart.selectAll('path.series')
.data(seriesList)
.enter().append('path').classed('series', true)
.attr('d', d3.svg.line()
.interpolate('basis')
.x(function(d,i) { return x(i); })
.y(y) );
svg.append("rect").classed("mouse", true)
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemove);
function mousemove() {
var x0 = x.invert(d3.mouse(this)[0]);
var y0 = y.invert(d3.mouse(this)[1]);
console.log(x0,y0);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment