Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:50:57+00:00 2026-06-17T08:50:57+00:00

You can see this code in action here: http://bl.ocks.org/2626142 This code draws a line

  • 0

You can see this code in action here: http://bl.ocks.org/2626142

This code draws a line chart, then transitions between 3 sample data sets. When moving from a small data set to a larger one, the extra data points suddenly appear instead of smoothly unfolding from the existing line.

When moving from a larger data set to a smaller one, the line is suddenly truncated before transitioning to fill the whole chart.

With this code there are sudden additions and deletions to the line and gridlines. How do I eliminate those?

var data = [
    [0,2,3,2,8],
    [2,4,1,5,3],
];
var data2 = [
    [0,1,2,3,4,5],
    [9,8,7,6,5,6],
];
var data3 = [
    [1,3,2],
    [0,8,5],
];

var w = 300,
    h = 100;

var chart = d3.select('body').append('div')
    .attr('class', 'chart')
    .append('svg:svg')
    .attr('width', w)
    .attr('height', h);

var color = d3.scale.category10();

function drawdata(data, chart) {
    var num = data[0].length-1;
    var x = d3.scale.linear().domain([0, num]).range([0,w]);
    var y = d3.scale.linear().domain([0, 10]).range([h, 0]);

    var line = d3.svg.line()
        .x(function(d, i) { return x(i); })
        .y(function(d) { return y(d); });

    var flat = d3.svg.line()
        .x(function(d, i) { return x(i); })
        .y(y(-1));

    var lines = chart.selectAll('.line')
        .data(data);

    lines.enter().append('path')
            .attr('class', 'line')
            .style('stroke', function(d,i) { return color(i); })
            .attr('d', line);

    lines.transition()
        .ease('linear')
        .duration(500)
        .attr('d', line);

    lines.exit().remove();

    // legend
    var ticks = chart.selectAll('line')
        .data(x.ticks(num));

    ticks.enter().append('line')
            .attr('x1', x)
            .attr('x2', x)
            .attr('y1', 0)
            .attr('y2', h)
            .attr('class', 'rule');
    ticks.transition()
        .ease('linear')
        .duration(500)
        .attr('x1', x)
        .attr('x2', x)
        .attr('y1', 0)
        .attr('y2', h);
    ticks.exit().remove();
}
var dats = [data, data2, data3];
function next() {
    var it = dats.shift();
    dats.push(it);
    drawdata(it, chart);
}
setInterval(next, 2000);
next();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:50:58+00:00Added an answer on June 17, 2026 at 8:50 am

    I faced a similar problem recently, and solved it using a custom interpolator for paths:

    // Add path interpolator to d3
    d3.interpolators.push(function(a, b) {
      var isPath, isArea, interpolator, ac, bc, an, bn;
    
      // Create a new array of a given length and fill it with the given value
      function fill(value, length) {
        return d3.range(length)
          .map(function() {
            return value;
          });
      }
    
      // Extract an array of coordinates from the path string
      function extractCoordinates(path) {
        return path.substr(1, path.length - (isArea ? 2 : 1)).split('L');
      }
    
      // Create a path from an array of coordinates
      function makePath(coordinates) {
        return 'M' + coordinates.join('L') + (isArea ? 'Z' : '');
      }
    
      // Buffer the smaller path with coordinates at the same position
      function bufferPath(p1, p2) {
        var d = p2.length - p1.length;
    
        // Paths created by d3.svg.area() wrap around such that the 'end'
        // of the path is in the middle of the list of coordinates
        if (isArea) {
          return fill(p1[0], d/2).concat(p1, fill(p1[p1.length - 1], d/2));
        } else {
          return fill(p1[0], d).concat(p1);
        }
      }
    
      // Regex for matching the 'd' attribute of SVG paths
      isPath = /M-?\d*\.?\d*,-?\d*\.?\d*(L-?\d*\.?\d*,-?\d*\.?\d*)*Z?/;
    
      if (isPath.test(a) && isPath.test(b)) {
        // A path is considered an area if it closes itself, indicated by a trailing 'Z'
        isArea = a[a.length - 1] === 'Z';
        ac = extractCoordinates(a);
        bc = extractCoordinates(b);
        an = ac.length;
        bn = bc.length;
    
        // Buffer the ending path if it is smaller than the first
        if (an > bn) {
          bc = bufferPath(bc, ac);
        }
    
        // Or, buffer the starting path if the reverse is true
        if (bn > an) {
          ac = bufferPath(ac, bc);
        }
    
        // Create an interpolater with the buffered paths (if both paths are of the same length,
        // the function will end up being the default string interpolator)
        interpolator = d3.interpolateString(bn > an ? makePath(ac) : a, an > bn ? makePath(bc) : b);
    
        // If the ending value changed, make sure the final interpolated value is correct
        return bn > an ? interpolator : function(t) {
          return t === 1 ? b : interpolator(t);
        };
      }
    });
    

    Here’s what the original gist looks like with the new interpolator: http://bl.ocks.org/4535474.

    Its approach is to ‘buffer’ the smaller dataset’s path by inserting zero-length line segments at the beginning. The effect is that new segments expand out of a single point at the start of the line, and unused segments similarly collapse down to a single point.

    Transitioning between datasets of different sizes (apparently) isn’t a common problem, and doesn’t have a universal solution. Because I was visualizing time-series data and transitioning between daily/weekly/monthly intervals, I needed the segments towards the end of the path to maintain visual continuity. I can imagine a case in which you’d want to do the same for the beginning of the path, or perhaps expand/contract the path by uniformly buffering segments throughout. Either way the same approach will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a fairly weird case, you can see the code here: http://jsfiddle.net/zpZtH/2/
I have this code below. As you can see I am passing two variables
Can anybody see what is wrong with this code. it does not show up
I can't see a clear mistake in this code. Instead of validating my fields,
I just can't see what's wrong with this code.. it seems to be the
in this question I posted you can see the originating code sample for this
I'm pulling my hair our here and really can't figure out why this code
As far as I can see this is not the case, however, I thought
This is not a programming question but I hope Microsoft programmer can see this
Morning all. I can see this has been discussed elsewhere but was wondering if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.