11

In the latest JqPlot examples (see here, there are buttons underneath some charts that you can click, and a div slides down with the chart as an image, allowing you to right click and save as.

I've checked the source and I just can't see myself where this is happening. I've seen various discussions about this (see here however my javascript is basic at best. This is however something I would like to implement in my current project.

Is anyone aware of a complete tutorial as to how to do this, i.e. from the actual jquery code right down to implementation in html code.

109221793
  • 16,477
  • 38
  • 108
  • 160

5 Answers5

29

Here's the simplest example I can code up:

//after creating your plot do
var imgData = $('#chart1').jqplotToImageStr({}); // given the div id of your plot, get the img data
var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
$('#imgChart1').append(imgElem);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ // append the img to the DOM

Fiddle here.

Mark
  • 106,305
  • 20
  • 172
  • 230
  • @Kiran, for IE7 you'd have to add excanvas into the mix – Mark Feb 01 '13 at 23:08
  • @Mark can you please elaborate? I need to get this working in IE7/8 – wonza Jul 18 '13 at 14:17
  • @Mark, I have been experimenting with the code you porvided on jsFiddle and have found it breaks with jquery 1.9.1. I am currently using that version. Any suggestions? – Chopo87 Jul 19 '13 at 09:59
  • 1
    @Chopo87, it's not the code I provided but jplot itself: https://bitbucket.org/cleonello/jqplot/issue/710/compatibility-with-jquery-v19 – Mark Jul 19 '13 at 13:53
  • @Wonza, IE 6/7/8 require the use of excanvas (http://www.jqplot.com/docs/files/usage-txt.html) with jqPlot (really any canvas drawing). I have no idea if the code I provided will work with those version of IE. I've purged them from all my systems and well never look back :) – Mark Jul 19 '13 at 13:56
  • @Mark I have that include added, and the plot itself works in IE, but I get a broken image jqplotToImageStr({}) returns null in IE but works in firefox – wonza Jul 22 '13 at 15:34
  • 3
    @wonza, sorry you are out of luck, from the jqplot source code: ' // excanvas and hence IE < 9 do not support toDataURL and cannot export images. if ($.jqplot.use_excanvas) { return null; } ' – Mark Jul 22 '13 at 16:16
  • @Mark please can you show me a solution for this ? http://stackoverflow.com/questions/21905400/jqplot-image-download-by-a-button-click-in-ie sorry for post this here – DilanG Feb 21 '14 at 05:52
  • @Mark I have a jqplot that I want to convert to an image. I tried your solution in my code. I'm using FireFox 42.0 , however, `$('#imgChart1').append(imgElem);` line of code is causing me problems because no javascript code after the said line of code is getting executed. Could you please suggest how I can correct the problem? – CS Lewis Dec 08 '15 at 10:16
3

Mark thanks for your contribution ,Just an Addition sometimes you may have mixed in(included) the cursor and Zoom functionality and you may need to create an image of a section of the graph, hoping to revert back to zoom back to create images of other sections. this may not be easy since jqPlot will not revert the graph for you to the original plot,so you have to this for yourself manually.

My Remedy

  1. Enrich your $.jqplot options with

    cursor: { show: true, zoom: true, looseZoom: true, showTooltip: false, dblClickReset:true, }

    this allows users to be in a position to revert back to the graphs initial look. if you choose this approach remember to advice your users on how to revert back via an advice note such as

    Double click on the Graph to Reset Zoom back to 100% for usability purposes.

For Those more inclined to Coding Here is a remedy , it includes Some of the code introduced by Mark(Thanks Again)

  1. Create a button right below the graph, furnish it with an id attribute and attach an even handler to its click function,

            <button id="show_revert_graph_btn" class="jqplot-replot-button" title="Reset Zoom back to 100%">Revert the Graph</button>
    
  2. attach an event listener and implement/register a handler like this

$('#show_revert_graph_btn').click(function(){
  // simulating a double click on the  canvas
  // not that the selecting expression includes 
  // the div id of where i chose to plot the chart "chart104" and the default class
  // provided to the canvas holding my chart i.e "canvas.jqplot-event-canvas"
  // then i double click it
        $('#chart104 canvas.jqplot-event-canvas').dblclick();
    });

Image Creation after zoom In my application i needed to create multiple images, out of different portions of the chart, so zoom allows me to magnify this parts, and the canvas to image functionality allows me to save the current data being shown in the canvas after i have zoomed in on a point. challenge was,how to reload my new zoom point as a new image for copying Remedy

  1. Create your button for plot Image
  2. attach a listener, to jquery's toggle event allow for you to show and hide the image
  3. Handle the image to manage the zoom events, i.e when i zoom in generate a new image, so that when i click i see the image of the zoomed-in part and not the old image of the whole chart

 $('#show_plotted_image_btn').toggle(
        function(){
            console.log('showing graph');
            // get the image
            function genImg(){
            var imgData = $('#chart104').jqplotToImageStr({});
       // given the div       id of your plot, get the img data
            var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
            $('#plotted_image_div').empty(); // remove the old graph
            $('#plotted_image_div').append(imgElem);
            };
            genImg();
            // show the image
            $('#plotted_image_div').css('display','block');

        },
        function(){
            // hide the image
            console.log('hiding graph');
            $('#plotted_image_div').css('display','none');
        }
    );

*In My implementation i only wanted to show the latest image,hence every time i ask for a new image i get rid of the old one via $('#plotted_image_div').empty();, which is simply emptying the old image and then appending the new one. *

*Here is my HTML for those who may need further clarity *

<div id="chart104" class=""></div>

            <button id="show_plotted_image_btn" class="jqplot-image-button">View Plot Image</button>
            <span style="font-weight: bold; color:#FC2896;"> Double click on the Graph to Reset Zoom back to 100%</span>
            <button id="show_revert_graph_btn" class="jqplot-replot-button" title="Reset Zoom back to 100%">Revert the Graph</button>
            <div id="plotted_image_div" class="" style="display: none;"></div>

Good Luck

chitwarnold
  • 1,331
  • 2
  • 8
  • 11
2

It looks like they're using a feature of Canvas to render to an image:

https://bitbucket.org/cleonello/jqplot/src/0d4d1a4fe522/src/jqplot.toImage.js

disperse
  • 1,216
  • 10
  • 22
2

When you got problems with the image output you have to change the jquery.jqplot.js. On some browsers the script stops of infinte loop (Chrome and Firefox).

change this code:

for (var i=0; i<wl; i++) {
    w += words[i];
    if (context.measureText(w).width > tagwidth) {
        breaks.push(i);
        w = '';
        i--;
    }   
}

to this:

for (var i=0; i<wl; i++) {
    w += words[i];
    if (context.measureText(w).width > tagwidth && w.length > words[i].length) {
        breaks.push(i);
        w = '';
        i--;
    }   
}

add this to your html:

<div id="chart"></div>
<div id="imgChart"></div>

and this to jquery after your jqplot-code:

$(document).ready(function(){
    //after creating your plot do
    var imgData = $('#chart').jqplotToImageStr({}); // given the div id of your plot, get the img data
    var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
    $('#imgChart').append(imgElem);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ //
});

See a demo here

bernte
  • 1,184
  • 2
  • 19
  • 34
1

This solution worked well for me. Simple and quick.

//after creating your plot do
var imgData = $('#chart1').jqplotToImageStr({}); // given the div id of your plot, get the img data
var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
$('#imgChart1').append(imgElem);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ //

I am using primefaces 3.2 and hence dont have the ability to use the new feature available in primefaces

Brent Waggoner
  • 548
  • 3
  • 14
Sharan Rajendran
  • 3,549
  • 2
  • 19
  • 6