How to print flot graph
Asked Answered
I

5

8

Can any body help me to find out, how to print dynamic graph as example generated by flot. I tried this one but it's printing whole page, but I want only graph portion.

function printGraph(){
    $('<img src="../images/button_refresh.png" alt="Print Graph" style="">').appendTo(controlholder).click(function (e) {                        
        //Canvas2Image.saveAsPNG(document.getElementById('placeholder'));
        //canvas.toDataURL("image/png");
        window.print('placeholder');
    });
}
Ignoble answered 23/11, 2011 at 12:49 Comment(1)
canvas2image may be this is what you are looking forFlare
T
6

This article describes how to copy the canvas to a normal img which can then easily be printed or saved as an image.

The important part:

img.src = canvas.toDataURL();

See the great answer below from Ignacio Correia for more details.

Thermostatics answered 6/8, 2013 at 8:58 Comment(0)
L
4

Launch a new window with only the graph or with alternate css similar to what google maps does when you print.

Longitudinal answered 23/11, 2011 at 13:42 Comment(0)
A
2

Attention this post have been edited, please see all possible solutions


From what I have searched and found you have only 2 choices or try to print the CANVAS, or EXPORT as an image or my idea is to separate the image from the content and try to print only the graph. Here is a FAQ by Flot, how can you export the image: Question Number 3

Q: Can I export the graph?

A: You can grab the image rendered by the canvas element used by Flot as a PNG or JPEG (remember to set a background). Note that it won't include anything not drawn in the canvas (such as the legend). And it doesn't work with excanvas which uses VML, but you could try Flashcanvas.

SOLUTION 1 - Export image:

Export image to computer and then print it

SOLUTION 2 - FLOT to CANVAS:

SOLUTION 3 - My own, Modal printing

This is not the best solution by far but it works, here is a demo:

Steps

  1. Load Fancybox
  2. Open using INLINE FRAME the graph
  3. PRINT on callback after show
  4. Reload the page after print to redesign the page

Here is the necessary code:

$(document).ready(function() {
  $(".various").fancybox({
    maxWidth    : 800,
    maxHeight   : 600,
    fitToView   : false,
    width       : '70%',
    height      : '70%',
    autoSize    : false,
    closeClick  : false,
    openEffect  : 'none',
    closeEffect : 'none',
    afterShow   : function() {
        alert('You are about to print the graph!');
        window.print();
    },
    afterClose  : function() {
        alert('We need to refresh the page!');
        window.location.reload(false); 
    }        
  });
});

Extra This related question is about exporting Flot to PDF, don't know if you may be interested: Export Flot to PDF

EDIT - WORKING SOLUTION Here is a working demo of how to export the image: FLOT to IMAGE

Archil answered 8/8, 2013 at 21:31 Comment(0)
M
2

You could hide the parts of the page you don't wish to print by using a separate stylesheet that has media="print". This would also allow you to tweak the final printed output of the graph itself, for example making it larger.

Merriott answered 9/8, 2013 at 0:25 Comment(0)
E
1

Found a solution by using html2canvas. First assign the container div to have id like "theChart".

<div class="box" id="theChart">
    <div id="placeholder"></div>
</div>

Now we can create an image:

html2canvas($('#theChart')).then(function(canvas) {
    image = canvas.toDataURL("image/png");
    document.location.href=image;
});

This will also solve the problem when canvas.toDataURL() does not render axis labels.

Elvieelvin answered 12/6, 2015 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.