javascriptanimationd3.jsplottable

Custom animation in plottable.js line chart


I am trying to use custom animation in plottable.js when data updates. Below is my code : -

<script type="text/javascript">

    var xScale = new Plottable.Scales.Category();
    var yScale = new Plottable.Scales.Linear().domain([0,30]);

    var xAxis = new Plottable.Axes.Category(xScale, "bottom");
    var yAxis = new Plottable.Axes.Numeric(yScale, "left");

    var dataset;

    var data;

    function createChart() {
        data = [];

        for(var i=0; i<10; i++) {
            data.push({x:"Data" + (i + 1),y:Math.abs(Math.random() * 10)});
        }

        dataset = new Plottable.Dataset(data);
        makeChart();
    }

    function updateChart() {
        data = [];

        for(var i=0; i<10; i++) {
            data.push({x:"Data" + (i + 1),y:Math.abs(Math.random() * 10)});
        }

        dataset.data(data);
    }

    function makeChart() {
        var linePlot = new Plottable.Plots.Line()
            .addDataset(dataset)
            .x(function(d) { return d.x; }, xScale)
            .y(function(d) { return d.y; }, yScale)
            .attr("stroke","#FA8116")
            .animated(true)
            .animator("test",new Plottable.Animators.Easing().easingMode("bounce"));

        var label_y = new Plottable.Components.AxisLabel("Parameter 2", -90);
        var label_x = new Plottable.Components.AxisLabel("Parameter 1", 0);

        var chart = new Plottable.Components.Table([
        [label_y, yAxis, linePlot],
        [null, null, xAxis],
        [null, null, label_x]
        ]);

        chart.renderTo("svg#lineChart");

        // Responsive Layout
        window.addEventListener("resize", function() {
            chart.redraw();
        });
    }   

    $(document).ready(function() {
        createChart();
        setInterval(function(d) {
            updateChart();
        },5000);
    });

</script>

I want to animate lineplot other than default and I did this :-

var linePlot = new Plottable.Plots.Line()
            .addDataset(dataset)
            .x(function(d) { return d.x; }, xScale)
            .y(function(d) { return d.y; }, yScale)
            .attr("stroke","#FA8116")
            .animated(true)
            .animator("test",new Plottable.Animators.Easing().easingMode("bounce"));

I don`t understand where I am in correct and since I am new to plottable can you guys help me out, also is there a way to use d3 based animation with plottable ?? If yes can you provide a code snippet

Thanx in advance


Solution

  • Plots normally have two Animators: MAIN and RESET. You need to specify that you want to change the primary Animator on the Plot:

    plot.animator(Plottable.Plots.Animator.MAIN,
        new Plottable.Animators.Easing().easingMode("bounce"));