flyingk
flyingk

Reputation: 33

D3 transition between line interpolations

I have been searching for quite a while but I haven't been able to figure it out.

I want to have a transition between two interpolations "basis" and "step-after" for a single line.

var time_scale, kpi_scale;
var line;

var data = [
{"kpi": 100, "time": 1317448800000},
{"kpi": 200, "time": 1320127200000},
{"kpi": 250, "time": 1322719200000},
{"kpi": 180, "time": 1325397600000},
{"kpi": 230, "time": 1328076000000},
{"kpi": 360, "time": 1330581600000},
{"kpi": 140, "time": 1333260000000},
{"kpi": 120, "time": 1335852000000},
{"kpi": 240, "time": 1338530400000},
{"kpi": 190, "time": 1341122400000},
{"kpi": 185, "time": 1343800800000},
{"kpi": 130, "time": 1346479200000},
{"kpi": 340, "time": 1349071200000},
{"kpi": 320, "time": 1351749600000},
{"kpi": 250, "time": 1354341600000}
];

// set up the viewport, the scales, and axis generators      
var container_dimensions = {width: 900, height: 400},
    margins = {top: 10, right: 20, bottom: 30, left: 60},
    chart_dimensions = {
        width: container_dimensions.width - margins.left - margins.right,
        height: container_dimensions.height - margins.top - margins.bottom
    };

var time_extent = d3.extent(
    data,
    function(d){return d.time;}
);

time_scale = d3.time.scale()
    .domain(time_extent)
    .range([0, chart_dimensions.width]);

var kpi_extent = d3.extent(
    data,
    function(d){return d.kpi;}
);

kpi_scale = d3.scale.linear()
    .domain(kpi_extent)
    .range([chart_dimensions.height, 0]);

var container = d3.select('body')
    .append('svg')
        .attr("width", container_dimensions.width)
        .attr("height", container_dimensions.height)
    .append("g")
        .attr("transform", "translate(" + margins.left + "," + margins.top + ")")
        .attr("id","chart");

draw_timeseries (data);
transitionToStepped (data);

function draw_timeseries (data) {
    line = d3.svg.line()
        .x(function(d){return time_scale(d.time);})
        .y(function(d){return kpi_scale(d.kpi);})
        .interpolate("basis");

    d3.select('#chart').append('path')
        .attr('d', line(data))
        .attr('id', 'line')
        .attr('class', 'line');               
}

function transitionToStepped (data) {

    line.interpolate("step-after");

    d3.select("#chart").selectAll(".line")
        .transition()
        .duration(2000)
        .attr("d", line(data))
        .delay(2000);           
}

I have saved a small jsfiddle containing my code.

jsFiddle Interpolation Transition

I wanted to "morph" between the basis line and the step-after line. But d3 is sliding in the step-after line from the left instead of "bending" the basis line into a step-after one.

Thanks for your help. Christoph

Upvotes: 3

Views: 1783

Answers (1)

mg1075
mg1075

Reputation: 18155

There has got to be a way to do this, and certainly one better than the offering below (I am looking forward to seeing other answers), but I have seen no answers here as of yet, and this will at least do a "morph" for you, although it also makes the line parallel to the x-axis during the transition. Not a terrible thing, but I believe not precisely what you are after.

Here's a fiddle for the full code and demo, modified from your original:
http://jsfiddle.net/n5P6z/2/

The meat of the approach is here:

function xLineReduce() {
    line.x(function (d) {
        return time_scale(d.time);
    }).y(function (d) {
        return chart_dimensions.height;
    });
}

function resetInterpolator() {
    line.interpolate("cubic-in-out");
}

function stepItUp() {
    line.x(function (d, i) {
        return time_scale(d.time);
    })
        .y(function (d, i) {
        return kpi_scale(d.kpi);
    })
        .interpolate("step-after");
}

function transitionToStepped(myData) {
    path.data(myData)
        .call(xLineReduce)
        .transition()
        .delay(1000)
        .duration(500)
        .attr('d', line(myData));

    path.data(myData)
        .call(resetInterpolator)
        .transition()
        .delay(1500)
        .duration(0)
        .attr('d', line(myData));

    path.data(myData)
        .call(stepItUp)
        .transition()
        .delay(1500)
        .duration(500)
        .attr('d', line(myData));
}

Upvotes: 2

Related Questions