Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I am using JQplot for drawing Slope & Flat Chart (line graph) for Bus Time Table.
The chart component is created inside a DIV, And I have the div id.
According to my requirement the plotted graph has to be export into JPEG & PDF. Is there any JavaScript library or Jquery UI by passing the div id as parameter and it exports the div area to a JPEG & PDF file.
Any solution would be appreciated.
Posted

1 solution

Hi, after a quick look at this page: http://www.jqplot.com/tests/line-charts.php[^] it seems that the graphs are drawn onto a canvas.

If that's the case with your graph too, then I think you're possibly 1/2 of the way there. That is to say, if you get the canvas element, you can call toDataURL on it. This will return a base64 encoded version of the image. You can the set this as an img's source. Once it's an an image and not just a canvas, you can save it.

See here:
toDataURL method[^]


As an example, if you open the page I linked to, you can find the correct element and give it an id. Once this is done, entering the following into the console will insert a brand-new image onto the page.

JavaScript
{ var img=new Image; img.src = document.getElementById('mTgt3').toDataURL(); document.body.appendChild(img);}


As you can probably tell, it took me a couple of goes to work out what the correct element was.

The graphs are made up of a number of canvas elements stacked ontop of each other - each one just doing part of the job. The one I ended up choosing was the one that holds the blue data line.

The element I ended up using was this one - (its a canvas for the first graph on the page):

HTML
<canvas width="468" height="262" style="position: absolute; left: 22px; top: 10px; " class="jqplot-series-canvas" id="mTgt3"></canvas>


Note the id of mTgt3 - I added that by right-clicking the element in the Elements tab of Crome's dev tools and Add Attribute.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900