0

I'm currently building a system that will need to plot graph points, then export the graph as an image to be served in an email. This process is an addition to an existing process written in Java.

If there's a Java solution that would be best, but I have seen some graphing libraries, like d3.js, that are in JavaScript, and I may be able to work with that. From what I can tell d3.js does not have a feature to export graphs as a PNG or JPEG.

Colby
  • 313
  • 4
  • 13

1 Answers1

1

In the browser you can use a charting library that renders to <canvas> such as chart.js

http://www.chartjs.org

Then once you have rendered your chart on the canvas you can obtain the image data as PNG or JPG:

var canvas = document.getElementById('myCanvas');
var dataURL = canvas.toDataURL("image/png");

This will give you a base64 encoded uri of the image that you could use with an html image:

var imgElement = document.getElementById('myImg');
imgElement.src = dataURL;

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

You can also directly get the image data from the canvas:

var imgData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData

chardy
  • 1,233
  • 1
  • 10
  • 18