Reputation: 4800
I have a simple line chart being fed from a web socket connection and i'm applying a "monotone" filter to smooth out the lines, so to avoid seeing the lines adjust as new data comes in I am clipping the chart to hide the most recent data points as advised in this article...
http://bost.ocks.org/mike/path/
but this makes my axis look incorrect, the right edge has a gap showing the difference between the clip rect and the real output domain as you can see...
I have been able to fix this by adding a different x scale that reduces the size of the domain to the clip rect but that seems hacky to me, and not a particularly clean solution.
Is there a correct way to fix this?
Here is an abbreviated code listing showing the relevant portions...
// Create an x-scale
var x = d3.scale.linear()
.domain([0, saved_points])
.range([0, width - margin]);
// Create the axis
var xAxis = d3.svg.axis()
.scale(x)
.tickSize(-height)
.tickValues([10,20,30,40,50,60,70,80,90]);
// Clip path truncates the last two points from the line, because adding new
// control points alters the shape of the line, and it "wiggles"
chart.append("defs")
.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width - margin - x(2))
.attr("height", height);
// Create the stack of lines
y_bands = d3.scale.ordinal().rangeBands([0,height]);
line = d3.svg.line()
.x(function(d,i){ return x(i); })
.y(function(d,i){
var a = -1.0 * (y(d.value) / y_bands.domain().length);
var b = y_bands(d.name);
var result = a + height - b;
return result;
})
.interpolate("monotone");
// Put the Axis at the bottom of the graph
d3.select("svg")
.append("svg:g")
.attr("class", "xaxis")
.attr("transform", "translate(0," + (height) + ")")
.call(xAxis);
// Finally create all the paths
chart.selectAll("path")
.data(my_line_chart.values)
.enter()
.append("g")
.attr("clip-path", "url(#clip)")
.append("svg:path")
.attr("class", "line_chart")
.attr("stroke", function(d, i) { return color(i); })
.attr('d', function(d,i){ return line(my_line_chart.values[i]);} );
Upvotes: 1
Views: 2153
Reputation: 4800
I haven't found an ideal solution for this but the technique I mentioned in my question does work.
By creating a second scale that matches the clip path size instead of the actual data range I can draw the axis with that and have them match up.
// define actual scales
var x = d3.scale.linear()
.domain([0, saved_points])
.range([0, width - margin]);
// Define a scale that's reduced in size of the output range by the
// length of two points of the actual scale.
var fake_x = d3.scale.linear()
.domain([0, saved_points - 2])
.range([0, width - margin - x(2)]);
// Use this fake scale for the axis instead of the real scale.
var xAxis = d3.svg.axis()
.scale(fake_x)
.tickSize(-height)
.tickValues(ticks);
// The clip path uses the same width reduced by two points
chart.append("defs")
.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width - margin - x(2))
.attr("height", height);
The only disadvantage i've seen to this method is that animating the width has issues because that x(2) value in the fake_x scale doesn't transition.
Upvotes: 1