Creating chart.js chart directly to PNG in Node js?











up vote
1
down vote

favorite












Is there any way I could use Chart.js to create a bar chart directly to a PNG file? I don't want to display the chart on my webpage, I want to send it directly to Facebook Messenger as an image.



I have used the following code to create a bar chart on my webpage:



var ctx = document.getElementById("myChart").msGetInputContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});


But in this particular case, my users would be interacting with my Node js app through Facebook messenger rather than a webpage. I would need a way to create a chart without needing the html canvas element and I can convert the chart to image using .toBase64Image().



Thanks in advance for any help!










share|improve this question






















  • You could try to render it using node-canvas, it supports both toDataURL() and createPNGStream() methods: github.com/Automattic/node-canvas#canvastodataurl
    – shkaper
    Nov 10 at 22:00










  • For what it's worth, you might find it a lot easier to do your charting with SVG, and render those server-side.
    – Brad
    Nov 11 at 1:55















up vote
1
down vote

favorite












Is there any way I could use Chart.js to create a bar chart directly to a PNG file? I don't want to display the chart on my webpage, I want to send it directly to Facebook Messenger as an image.



I have used the following code to create a bar chart on my webpage:



var ctx = document.getElementById("myChart").msGetInputContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});


But in this particular case, my users would be interacting with my Node js app through Facebook messenger rather than a webpage. I would need a way to create a chart without needing the html canvas element and I can convert the chart to image using .toBase64Image().



Thanks in advance for any help!










share|improve this question






















  • You could try to render it using node-canvas, it supports both toDataURL() and createPNGStream() methods: github.com/Automattic/node-canvas#canvastodataurl
    – shkaper
    Nov 10 at 22:00










  • For what it's worth, you might find it a lot easier to do your charting with SVG, and render those server-side.
    – Brad
    Nov 11 at 1:55













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Is there any way I could use Chart.js to create a bar chart directly to a PNG file? I don't want to display the chart on my webpage, I want to send it directly to Facebook Messenger as an image.



I have used the following code to create a bar chart on my webpage:



var ctx = document.getElementById("myChart").msGetInputContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});


But in this particular case, my users would be interacting with my Node js app through Facebook messenger rather than a webpage. I would need a way to create a chart without needing the html canvas element and I can convert the chart to image using .toBase64Image().



Thanks in advance for any help!










share|improve this question













Is there any way I could use Chart.js to create a bar chart directly to a PNG file? I don't want to display the chart on my webpage, I want to send it directly to Facebook Messenger as an image.



I have used the following code to create a bar chart on my webpage:



var ctx = document.getElementById("myChart").msGetInputContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});


But in this particular case, my users would be interacting with my Node js app through Facebook messenger rather than a webpage. I would need a way to create a chart without needing the html canvas element and I can convert the chart to image using .toBase64Image().



Thanks in advance for any help!







javascript node.js charts chart.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 21:23









user2573690

68731937




68731937












  • You could try to render it using node-canvas, it supports both toDataURL() and createPNGStream() methods: github.com/Automattic/node-canvas#canvastodataurl
    – shkaper
    Nov 10 at 22:00










  • For what it's worth, you might find it a lot easier to do your charting with SVG, and render those server-side.
    – Brad
    Nov 11 at 1:55


















  • You could try to render it using node-canvas, it supports both toDataURL() and createPNGStream() methods: github.com/Automattic/node-canvas#canvastodataurl
    – shkaper
    Nov 10 at 22:00










  • For what it's worth, you might find it a lot easier to do your charting with SVG, and render those server-side.
    – Brad
    Nov 11 at 1:55
















You could try to render it using node-canvas, it supports both toDataURL() and createPNGStream() methods: github.com/Automattic/node-canvas#canvastodataurl
– shkaper
Nov 10 at 22:00




You could try to render it using node-canvas, it supports both toDataURL() and createPNGStream() methods: github.com/Automattic/node-canvas#canvastodataurl
– shkaper
Nov 10 at 22:00












For what it's worth, you might find it a lot easier to do your charting with SVG, and render those server-side.
– Brad
Nov 11 at 1:55




For what it's worth, you might find it a lot easier to do your charting with SVG, and render those server-side.
– Brad
Nov 11 at 1:55












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










Chart.js is built on the HTML5 canvas element.



To use it on node.js you need to mimic this element in node.



There is a package that try to handle all the needed libraries for this porpuse, you can find it here
chartjs-node






share|improve this answer





















  • Thanks, this is what I ended up going with.
    – user2573690
    Nov 13 at 0:07


















up vote
1
down vote













Disclaimer: I’m image-charts founder.



I had the same issue — sending chart images through a messenger bot — I’ve built Image-charts to manage this and make it scale and fast.



https://image-charts.com/chart
?cht=p3
&chs=700x100
&chd=t:60,40
&chl=Hello|World
&chan
&chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1


a






share|improve this answer




























    up vote
    0
    down vote













    You can create image from Canvas using toDataURL, so after charts.js renders the graph into the canvas (myChart)



    var canvas = document.getElementById('myChart');
    var dataURL = canvas.toDataURL();


    dataURL will not contain the base64 string of the image. You can write it to file and use the file or use the string any other way.



    Check that reference for converting the string to image file if you will.
    https://gist.github.com/madhums/e749dca107e26d72b64d



    Hope this is what you mean you needed to do.






    share|improve this answer





















      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',
      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%2f53243544%2fcreating-chart-js-chart-directly-to-png-in-node-js%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Chart.js is built on the HTML5 canvas element.



      To use it on node.js you need to mimic this element in node.



      There is a package that try to handle all the needed libraries for this porpuse, you can find it here
      chartjs-node






      share|improve this answer





















      • Thanks, this is what I ended up going with.
        – user2573690
        Nov 13 at 0:07















      up vote
      1
      down vote



      accepted










      Chart.js is built on the HTML5 canvas element.



      To use it on node.js you need to mimic this element in node.



      There is a package that try to handle all the needed libraries for this porpuse, you can find it here
      chartjs-node






      share|improve this answer





















      • Thanks, this is what I ended up going with.
        – user2573690
        Nov 13 at 0:07













      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      Chart.js is built on the HTML5 canvas element.



      To use it on node.js you need to mimic this element in node.



      There is a package that try to handle all the needed libraries for this porpuse, you can find it here
      chartjs-node






      share|improve this answer












      Chart.js is built on the HTML5 canvas element.



      To use it on node.js you need to mimic this element in node.



      There is a package that try to handle all the needed libraries for this porpuse, you can find it here
      chartjs-node







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 10 at 22:03









      yeya

      450513




      450513












      • Thanks, this is what I ended up going with.
        – user2573690
        Nov 13 at 0:07


















      • Thanks, this is what I ended up going with.
        – user2573690
        Nov 13 at 0:07
















      Thanks, this is what I ended up going with.
      – user2573690
      Nov 13 at 0:07




      Thanks, this is what I ended up going with.
      – user2573690
      Nov 13 at 0:07












      up vote
      1
      down vote













      Disclaimer: I’m image-charts founder.



      I had the same issue — sending chart images through a messenger bot — I’ve built Image-charts to manage this and make it scale and fast.



      https://image-charts.com/chart
      ?cht=p3
      &chs=700x100
      &chd=t:60,40
      &chl=Hello|World
      &chan
      &chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1


      a






      share|improve this answer

























        up vote
        1
        down vote













        Disclaimer: I’m image-charts founder.



        I had the same issue — sending chart images through a messenger bot — I’ve built Image-charts to manage this and make it scale and fast.



        https://image-charts.com/chart
        ?cht=p3
        &chs=700x100
        &chd=t:60,40
        &chl=Hello|World
        &chan
        &chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1


        a






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Disclaimer: I’m image-charts founder.



          I had the same issue — sending chart images through a messenger bot — I’ve built Image-charts to manage this and make it scale and fast.



          https://image-charts.com/chart
          ?cht=p3
          &chs=700x100
          &chd=t:60,40
          &chl=Hello|World
          &chan
          &chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1


          a






          share|improve this answer












          Disclaimer: I’m image-charts founder.



          I had the same issue — sending chart images through a messenger bot — I’ve built Image-charts to manage this and make it scale and fast.



          https://image-charts.com/chart
          ?cht=p3
          &chs=700x100
          &chd=t:60,40
          &chl=Hello|World
          &chan
          &chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1


          a







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 16:04









          FGRibreau

          5,04612540




          5,04612540






















              up vote
              0
              down vote













              You can create image from Canvas using toDataURL, so after charts.js renders the graph into the canvas (myChart)



              var canvas = document.getElementById('myChart');
              var dataURL = canvas.toDataURL();


              dataURL will not contain the base64 string of the image. You can write it to file and use the file or use the string any other way.



              Check that reference for converting the string to image file if you will.
              https://gist.github.com/madhums/e749dca107e26d72b64d



              Hope this is what you mean you needed to do.






              share|improve this answer

























                up vote
                0
                down vote













                You can create image from Canvas using toDataURL, so after charts.js renders the graph into the canvas (myChart)



                var canvas = document.getElementById('myChart');
                var dataURL = canvas.toDataURL();


                dataURL will not contain the base64 string of the image. You can write it to file and use the file or use the string any other way.



                Check that reference for converting the string to image file if you will.
                https://gist.github.com/madhums/e749dca107e26d72b64d



                Hope this is what you mean you needed to do.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can create image from Canvas using toDataURL, so after charts.js renders the graph into the canvas (myChart)



                  var canvas = document.getElementById('myChart');
                  var dataURL = canvas.toDataURL();


                  dataURL will not contain the base64 string of the image. You can write it to file and use the file or use the string any other way.



                  Check that reference for converting the string to image file if you will.
                  https://gist.github.com/madhums/e749dca107e26d72b64d



                  Hope this is what you mean you needed to do.






                  share|improve this answer












                  You can create image from Canvas using toDataURL, so after charts.js renders the graph into the canvas (myChart)



                  var canvas = document.getElementById('myChart');
                  var dataURL = canvas.toDataURL();


                  dataURL will not contain the base64 string of the image. You can write it to file and use the file or use the string any other way.



                  Check that reference for converting the string to image file if you will.
                  https://gist.github.com/madhums/e749dca107e26d72b64d



                  Hope this is what you mean you needed to do.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 23:09









                  user6437700

                  458




                  458






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243544%2fcreating-chart-js-chart-directly-to-png-in-node-js%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

                      Lugert, Oklahoma