I'm trying to save a flot chart to an image and eventually to a pdf, but can't quite figure out how.
Online I see that I can do
canvas.toDataURL("image/png")
But the trouble is how do I get the canvas in the first place, the examples say to use
document.getElementById("canvas");
but for my code I'm using a div with id="placeholder" (per all the flot examples) and there's nothing labeled with a canvas tag in my html, and this doesn't seem to work. My javascript for flot looks like
$.plot($("#placeholder"), [ d1, d2, d3 ], { series: {
lines: { show:false },
bars: { show: true, barWidth: 0.6, horizontal:true } } });
Does anyone have a working example that plots a flot graph and then has a button to either save/view as image or pdf?
There are several questions that sort of already answer this, but they are missing some key details as far as I can tell -- my apologies for being thick on this.
I did this by using a third party application called cutyCapt http://cutycapt.sourceforge.net/ This takes snapshots of webpages. So i had to create a dummy webpage containig the flot image and then fed the website to this function:
public bool FlotToImage(string website, string destinationFile)
{
string cutyCaptPath = ConfigurationManager.AppSettings["CutyCaptPath"];
if (!File.Exists(cutyCaptPath))//check if found cutyCapt application
{
return false;
}
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = cutyCaptPath; // path to executable file
startInfo.Arguments = " --url=" + website + " --out=" + destinationFile;// the arguments
startInfo.CreateNoWindow = true; //optional
startInfo.WindowStyle = ProcessWindowStyle.Hidden; //optional
Process process =Process.Start(startInfo);
process.WaitForExit();
return true;
}
Hope this helps someone. This actually took me a while to do since I had to also implement a login bypass to the "dummy" website with the flot.
You're not going to like the flot v 0.7 solution for this. The labels are not on the canvas, they are HTML elements that are positioned around it. So forget about having them written to your image, it's not going to happen with flot as is.
If you are really keen, what you need to do is get the latest version of flot out of github to write labels to the canvas (which means you won't be able to style them beyond the basics - i.e. no CSS and no making them links), and then you'll be OK (and don't forget to see the new API.txt). If you need to get the canvas, flot provides a method for that:
var plot = $.plot($("#placeholder"), [ d1, d2, d3 ], { series: {
lines: { show:false },
bars: { show: true, barWidth: 0.6, horizontal:true } } });
var ctx = plot.getCanvas();
//do whatever with ctx.toDataURL("image/png")
The only change I've really suggested here is to upgrade flot to the latest (unreleased) version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With