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.
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 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
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)
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>
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
$('#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
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
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