Vivek Raghav
Vivek Raghav

Reputation: 15

Working with Charts in motion [Animating chart]

I have created a areaspline chart using highcharts library and making it animated with play/pause button transition between weeks of data

Ref. https://www.highcharts.com/blog/tutorials/176-charts-in-motion/

jsfiddle Ref. https://jsfiddle.net/larsac07/wkev75nL/?utm_source=website&utm_medium=embed&utm_campaign=wkev75nL

in the above example, we are animating the chart week wise

dataSet used :

dataSequence = [
        {
            name: 'Week 1',
            data: [1, 2, 2, 1, 1, 2, 2]
        }, {
            name: 'Week 2',
            data: [6, 12, 2, 3, 3, 2, 2]
        }, {
            name: 'Week 3',
            data: [4, 5, 6, 5, 5, 4, 9]
        }, {
            name: 'Week 4',
            data: [5, 5, 6, 6, 5, 6, 6]
        }, {
            name: 'Week 5',
            data: [6, 7, 7, 6, 6, 6, 7]
        }, {
            name: 'Week 6',
            data: [8, 9, 9, 8, 8, 8, 9]
        }, {
            name: 'Week 7',
            data: [9, 10, 4, 10, 9, 9, 9]
        }, {
            name: 'Week 8',
            data: [1, 10, 10, 10, 10, 11, 11]
        }, {
            name: 'Week 9',
            data: [11, 11, 12, 12, 12, 11, 11]
        }
    ]

I don't want change chart on play button click I want animate the data points 11 data point for week1 to same data point but different value on y axis for week2

xAxis = ["week1", "Week2", ..... ],
yAxis = [[1,2,3,4,5,6,7,8,9,10,11], [3,5,7,8,2,1,5,7,6,1,10], ....]

on the play button it would transit between week1 then will go to week 2 and so till last week number available.

Trying to have something like this Ref. https://aatishb.com/covidtrends/

static chart

this chart is plotted using this dataset for series

Highcharts.chart("container", {
      chart: {
        type: "areaspline"
      },
      tooltip: {
        shared: true,
        valueSuffix: " units"
      },
      xAxis: {
        categories: [
          "Week 1",
          "Week 2",
          "Week 3",
          "Week 4",
          "Week 5",
          "Week 6",
          "Week 7"
        ]
      },
      yAxis: {
        title: {
          text: "Index"
        }
      },
      legend: {
        layout: "horizontal",
        align: "right",
        verticalAlign: "top",
        x: 50,
        y: 50,
        floating: true,
        borderWidth: 1,
        backgroundColor:
          (Highcharts.theme && Highcharts.theme.legendBackgroundColor) ||
          "#FFFFFF"
      },
      plotOptions: {
        areaspline: {
          fillOpacity: 0.5
        }
      },
      credits: {
        enabled: false
      },
      series: [
        {
          name: "By week",
          data: dataSequence[value].data.slice()
        },
        {
          type: "spline",
          name: "Topic 1",
          data: [3, 2, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 2",
          data: [1, 5, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 3",
          data: [3, 7, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 4",
          data: [5, 1, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 5",
          data: [7, 3, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 6",
          data: [9, 2, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 7",
          data: [11, 8, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 8",
          data: [13, 11, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 9",
          data: [15, 7, 1, 3, 4, 7, 8]
        },
        {
          type: "spline",
          name: "Topic 10",
          data: [7, 5, 1, 3, 4, 7, 8]
        }
      ],
      title: {
        text: ""
      },
      subtitle: {
        text: "Efficiency Index of Topics"
      }
    });

this my update function in react

update(increment) {
    var input = $("#play-range")[0];
    var output = $("#play-output")[0];

    if (increment) {
      input.value = parseInt(input.value) + increment;
    }
    output.innerHTML = this.state.dataSequence[input.value].name;
    this.setState({
      value: input.value
    });
    if (input.value >= input.max) {
      // Auto-pause
      this.pause();
      this.setState(
        {
          value: 0
        },
        () => {
          output.innerHTML = this.state.dataSequence[0].name;
        }
      );
    }
  }

the whole chart is plotted at once, I need something that it should transit first, it plots all data points for week1 then week 2 after that week 3 when I click on the play button

Upvotes: 0

Views: 360

Answers (1)

ppotaczek
ppotaczek

Reputation: 39099

You need to start with an empty data and use addPoint method in the update function:

function update(increment) {
    var input = $('#play-range')[0],
        output = $('#play-output')[0],
        increment;

            chart.series[0].addPoint(dataSequence[input.value].data[actualPointIndex]);
    actualPointIndex += increment;

    if (actualPointIndex === 6) {
            actualPointIndex = 0;
        input.value = parseInt(input.value) + increment;
    }

    output.innerHTML = dataSequence[input.value].name; // Output value
    if (input.value >= input.max) { // Auto-pause
        pause($('#play-pause-button')[0]);
    }
}

Live demo: https://jsfiddle.net/BlackLabel/stpxyfca/

API Reference: https://api.highcharts.com/class-reference/Highcharts.Series#addPoint

Upvotes: 1

Related Questions