Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 line filter out (remove) duplicate points

I am drawing svg line using D3.js. I want to improve performance by removing duplicate (x,y) points before adding to line path. What is the best way to do this in D3 or javascript. I am loading data from json file, for this test, but it might come in an array on server later.

See code snip and console output below.

Thanks for any help

        var x = d3.scale.linear().domain([xMin, xMax]).rangeRound([0, width]);
        var y = d3.scale.linear().domain([yMin, yMax]).rangeRound([height, 0]);

        var line = d3.svg.line()
            .x(function(d, i) { 
                var xPoint = x((i + 1) * xMult);
                console.log("xPoint= " + xPoint.toString()); 
                return xPoint; })
            .y(function(d) { 
                var yPoint = y(d);
                console.log("yPoint= " + yPoint.toString()); 
                return yPoint; })
            .interpolate("basis-open")
            .tension(0);


            ...


        // Add the valueline path.
        g.append("path")
            .attr("class", "line")
            .attr("d", line(data));


--------------------------------------------------
Console Output from two lines in code above
console.log("xPoint= " + xPoint.toString()); 
console.log("yPoint= " + yPoint.toString());
----------------------------------------------

xPoint= 0
yPoint= 24
xPoint= 0
yPoint= 24
xPoint= 1
yPoint= 24
xPoint= 1
yPoint= 24
xPoint= 1
yPoint= 24
xPoint= 1
yPoint= 24
xPoint= 2
yPoint= 24
xPoint= 2
yPoint= 25
xPoint= 2
yPoint= 25
xPoint= 2
yPoint= 24
xPoint= 3
yPoint= 25
xPoint= 3
yPoint= 25
xPoint= 3
yPoint= 25
xPoint= 3
yPoint= 25
xPoint= 4
yPoint= 25 
like image 330
Joe Cane Avatar asked Mar 09 '26 16:03

Joe Cane


1 Answers

The issue with uniq though is that you are removing duplicate points regardless of their positioning within the array. For a polygon of points or lines of points, you only really want to remove consecutive duplicate points. Here is an example of how to do that using JavaScript.

var point1 = { x: 0, y: 24 };
var point2 = { x: 1, y: 24 };
var point3 = { x: 2, y: 24 };
var point4 = { x: 2, y: 25 };

var points = [point1, point1, 
              point2, point2, point2, point2, 
              point3, 
              point4, point4, 
              point3];

var pointsAreEqual = function(point1, point2) {
    return point1.x === point2.x && point1.y === point2.y;
}

var removeConsecDupes = function(items, itemsAreEqual) {
    var previous, results = []; 

    var callback = function(value, index) {
        if (index === 0 || !itemsAreEqual(previous, value)) {
            results.push(value);
        }   
        previous = value;
    }   
    items.forEach(callback);

    return results;
}

var results = removeConsecDupes(points, pointsAreEqual);

console.dir(results);

results:

[ { x: 0, y: 24 },
  { x: 1, y: 24 },
  { x: 2, y: 24 },
  { x: 2, y: 25 },
  { x: 2, y: 24 } ]
like image 87
Ryan O'Neill Avatar answered Mar 11 '26 05:03

Ryan O'Neill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!