Change/Swap Highcharts Data





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've set-up a Pie Chart using Highcharts and I need to swap the data out via buttons. The second button (Distant) swaps out the data no problem but when I try to swap back to the original data with the first button (Immediate) it doesn't work.



Here is my Codepen:
https://codepen.io/doitliketyler/pen/mQMyGe?editors=1011



My buttons look like this:



$('#immediate').click(function() {
chart.series[0].setData(immediate);
});

$('#distant').click(function() {
chart.series[0].setData(distant);
});


Any ideas on how this is done properly?










share|improve this question





























    0















    I've set-up a Pie Chart using Highcharts and I need to swap the data out via buttons. The second button (Distant) swaps out the data no problem but when I try to swap back to the original data with the first button (Immediate) it doesn't work.



    Here is my Codepen:
    https://codepen.io/doitliketyler/pen/mQMyGe?editors=1011



    My buttons look like this:



    $('#immediate').click(function() {
    chart.series[0].setData(immediate);
    });

    $('#distant').click(function() {
    chart.series[0].setData(distant);
    });


    Any ideas on how this is done properly?










    share|improve this question

























      0












      0








      0








      I've set-up a Pie Chart using Highcharts and I need to swap the data out via buttons. The second button (Distant) swaps out the data no problem but when I try to swap back to the original data with the first button (Immediate) it doesn't work.



      Here is my Codepen:
      https://codepen.io/doitliketyler/pen/mQMyGe?editors=1011



      My buttons look like this:



      $('#immediate').click(function() {
      chart.series[0].setData(immediate);
      });

      $('#distant').click(function() {
      chart.series[0].setData(distant);
      });


      Any ideas on how this is done properly?










      share|improve this question














      I've set-up a Pie Chart using Highcharts and I need to swap the data out via buttons. The second button (Distant) swaps out the data no problem but when I try to swap back to the original data with the first button (Immediate) it doesn't work.



      Here is my Codepen:
      https://codepen.io/doitliketyler/pen/mQMyGe?editors=1011



      My buttons look like this:



      $('#immediate').click(function() {
      chart.series[0].setData(immediate);
      });

      $('#distant').click(function() {
      chart.series[0].setData(distant);
      });


      Any ideas on how this is done properly?







      javascript highcharts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 19:02









      nunyanunya

      123114




      123114
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Here you go. The problem was that when you set one of your data array, it passes by reference so both data sets are pointing at the same thing. So first make a copy of the data you want to set. Then set an empty array as your data to clear highcharts data then set it with the copy array.



          var chart = Highcharts.chart('container', {
          chart: {
          type: 'pie'
          },
          title: {
          text: 'Swap Pie Data'
          },
          series: [
          {
          id: 'relative',
          name: 'Immediate',
          type: 'pie',
          size: '100%',
          innerSize: '30%',
          colorByPoint: true,
          data: immediate
          },
          {
          type: 'pie',
          size: '30%',
          name: 'Original',
          colorByPoint: true,
          data: original
          }
          ]
          });

          $('#immediate').click(function() {
          var temp = immediate.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);

          });

          $('#distant').click(function() {
          var temp = distant.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);
          });





          share|improve this answer


























          • Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

            – nunya
            Nov 16 '18 at 21:24






          • 1





            @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

            – nitrodmr
            Nov 16 '18 at 21:29








          • 1





            Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

            – nitrodmr
            Nov 16 '18 at 21:34











          • Oops...that's exactly what it was. Thanks so much. This works perfectly :)

            – nunya
            Nov 16 '18 at 21:57












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343913%2fchange-swap-highcharts-data%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Here you go. The problem was that when you set one of your data array, it passes by reference so both data sets are pointing at the same thing. So first make a copy of the data you want to set. Then set an empty array as your data to clear highcharts data then set it with the copy array.



          var chart = Highcharts.chart('container', {
          chart: {
          type: 'pie'
          },
          title: {
          text: 'Swap Pie Data'
          },
          series: [
          {
          id: 'relative',
          name: 'Immediate',
          type: 'pie',
          size: '100%',
          innerSize: '30%',
          colorByPoint: true,
          data: immediate
          },
          {
          type: 'pie',
          size: '30%',
          name: 'Original',
          colorByPoint: true,
          data: original
          }
          ]
          });

          $('#immediate').click(function() {
          var temp = immediate.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);

          });

          $('#distant').click(function() {
          var temp = distant.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);
          });





          share|improve this answer


























          • Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

            – nunya
            Nov 16 '18 at 21:24






          • 1





            @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

            – nitrodmr
            Nov 16 '18 at 21:29








          • 1





            Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

            – nitrodmr
            Nov 16 '18 at 21:34











          • Oops...that's exactly what it was. Thanks so much. This works perfectly :)

            – nunya
            Nov 16 '18 at 21:57
















          1














          Here you go. The problem was that when you set one of your data array, it passes by reference so both data sets are pointing at the same thing. So first make a copy of the data you want to set. Then set an empty array as your data to clear highcharts data then set it with the copy array.



          var chart = Highcharts.chart('container', {
          chart: {
          type: 'pie'
          },
          title: {
          text: 'Swap Pie Data'
          },
          series: [
          {
          id: 'relative',
          name: 'Immediate',
          type: 'pie',
          size: '100%',
          innerSize: '30%',
          colorByPoint: true,
          data: immediate
          },
          {
          type: 'pie',
          size: '30%',
          name: 'Original',
          colorByPoint: true,
          data: original
          }
          ]
          });

          $('#immediate').click(function() {
          var temp = immediate.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);

          });

          $('#distant').click(function() {
          var temp = distant.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);
          });





          share|improve this answer


























          • Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

            – nunya
            Nov 16 '18 at 21:24






          • 1





            @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

            – nitrodmr
            Nov 16 '18 at 21:29








          • 1





            Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

            – nitrodmr
            Nov 16 '18 at 21:34











          • Oops...that's exactly what it was. Thanks so much. This works perfectly :)

            – nunya
            Nov 16 '18 at 21:57














          1












          1








          1







          Here you go. The problem was that when you set one of your data array, it passes by reference so both data sets are pointing at the same thing. So first make a copy of the data you want to set. Then set an empty array as your data to clear highcharts data then set it with the copy array.



          var chart = Highcharts.chart('container', {
          chart: {
          type: 'pie'
          },
          title: {
          text: 'Swap Pie Data'
          },
          series: [
          {
          id: 'relative',
          name: 'Immediate',
          type: 'pie',
          size: '100%',
          innerSize: '30%',
          colorByPoint: true,
          data: immediate
          },
          {
          type: 'pie',
          size: '30%',
          name: 'Original',
          colorByPoint: true,
          data: original
          }
          ]
          });

          $('#immediate').click(function() {
          var temp = immediate.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);

          });

          $('#distant').click(function() {
          var temp = distant.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);
          });





          share|improve this answer















          Here you go. The problem was that when you set one of your data array, it passes by reference so both data sets are pointing at the same thing. So first make a copy of the data you want to set. Then set an empty array as your data to clear highcharts data then set it with the copy array.



          var chart = Highcharts.chart('container', {
          chart: {
          type: 'pie'
          },
          title: {
          text: 'Swap Pie Data'
          },
          series: [
          {
          id: 'relative',
          name: 'Immediate',
          type: 'pie',
          size: '100%',
          innerSize: '30%',
          colorByPoint: true,
          data: immediate
          },
          {
          type: 'pie',
          size: '30%',
          name: 'Original',
          colorByPoint: true,
          data: original
          }
          ]
          });

          $('#immediate').click(function() {
          var temp = immediate.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);

          });

          $('#distant').click(function() {
          var temp = distant.slice(0);

          chart.series[0].setData();
          chart.series[0].setData(temp);
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 19:54

























          answered Nov 16 '18 at 19:45









          nitrodmrnitrodmr

          16316




          16316













          • Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

            – nunya
            Nov 16 '18 at 21:24






          • 1





            @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

            – nitrodmr
            Nov 16 '18 at 21:29








          • 1





            Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

            – nitrodmr
            Nov 16 '18 at 21:34











          • Oops...that's exactly what it was. Thanks so much. This works perfectly :)

            – nunya
            Nov 16 '18 at 21:57



















          • Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

            – nunya
            Nov 16 '18 at 21:24






          • 1





            @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

            – nitrodmr
            Nov 16 '18 at 21:29








          • 1





            Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

            – nitrodmr
            Nov 16 '18 at 21:34











          • Oops...that's exactly what it was. Thanks so much. This works perfectly :)

            – nunya
            Nov 16 '18 at 21:57

















          Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

          – nunya
          Nov 16 '18 at 21:24





          Hey there. Thanks for the answer. I gave that shot and the data is still not changing after the first click. I've updated my original pen. Do you happen to have an example of this working? Maybe I overlooked something?

          – nunya
          Nov 16 '18 at 21:24




          1




          1





          @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

          – nitrodmr
          Nov 16 '18 at 21:29







          @nunya Did you remove the disable=true on <button id="immediate" disabled="true">Immediate</button>?

          – nitrodmr
          Nov 16 '18 at 21:29






          1




          1





          Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

          – nitrodmr
          Nov 16 '18 at 21:34





          Here is an example. codepen.io/nitrodmr/pen/xQLVEK?editors=1010

          – nitrodmr
          Nov 16 '18 at 21:34













          Oops...that's exactly what it was. Thanks so much. This works perfectly :)

          – nunya
          Nov 16 '18 at 21:57





          Oops...that's exactly what it was. Thanks so much. This works perfectly :)

          – nunya
          Nov 16 '18 at 21:57




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343913%2fchange-swap-highcharts-data%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues