2010-11-16 4 views
22

Sto usando un plugin per jQuery chiamati FLOT http://code.google.com/p/flot/Come salvare un grafico FLOT jQuery su un formato .png o altro?

Una volta che il grafico è reso Voglio che il cliente sia in grado di salvare il grafico in un file per più tardi. Qualche idea su come questo può essere fatto senza richiedere al client di scaricare qualche strumento o dispositivo di acquisizione di immagini?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="en"> 
    <head> 
     <!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <title>Graph</title> 
     <link href="layout.css" rel="stylesheet" type="text/css"></link> 
     <!--[if IE]><script language="javascript" type="text/javascript" src="lib/flot/excanvas.min.js"></script><![endif]--> 
     <script language="javascript" type="text/javascript" src="lib/canvas2image/base64.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/canvas2image/canvas2image.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/flot/jquery.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/gChart/jquery.gchart.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.selection.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.crosshair.js"></script> 
     <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.navigate.js"></script> 
     <style> 
     </style> 
    </head> 
    <body> 

<div id="placeholder" style="width:1000px;height:400px;"></div> 
<div id="test" style="display: none; background: #ff0; padding: 5px; border: 1px solid #ddd; position: absolute;"></div> 

<script type="text/javascript"> 
    $(function() { 
     var options0 = { 
     xaxis: {mode: "time"}, 
     legend: {position: "nw"}, 
     lines: {show: true}, 
     grid: {hoverable: true}}; 
     var grid_data0 = [[new Date(1286172000 * 1000), 1219], [new Date(1286258400 * 1000), 1583], [new Date(1286344800 * 1000), 1566], [new Date(1286431200 * 1000), 2191], [new Date(1286517600 * 1000), 2471], [new Date(1286604000 * 1000), 3128], [new Date(1286690400 * 1000), 2713], [new Date(1286776800 * 1000), 2117], [new Date(1286863200 * 1000), 3174], [new Date(1286949600 * 1000), 3051], [new Date(1287036000 * 1000), 3582], [new Date(1287122400 * 1000), 3472], [new Date(1287208800 * 1000), 3928], [new Date(1287295200 * 1000), 3688], [new Date(1287381600 * 1000), 2547], [new Date(1287468000 * 1000), 1549], [new Date(1287554400 * 1000), 3008], [new Date(1287640800 * 1000), 2309], [new Date(1287727200 * 1000), 2973], [new Date(1287813600 * 1000), 3805], [new Date(1287900000 * 1000), 3643], [new Date(1287986400 * 1000), 2310], [new Date(1288072800 * 1000), 2323], [new Date(1288159200 * 1000), 2399], [new Date(1288245600 * 1000), 2305], [new Date(1288332000 * 1000), 2393], [new Date(1288418400 * 1000), 3212], [new Date(1288504800 * 1000), 3348], [new Date(1288591200 * 1000), 2391], [new Date(1288677600 * 1000), 2130], [new Date(1288764000 * 1000), 1896], [new Date(1288850400 * 1000), 1765], [new Date(1288936800 * 1000), 2191], [new Date(1289023200 * 1000), 3008], [new Date(1289109600 * 1000), 3085], [new Date(1289199600 * 1000), 2151], [new Date(1289286000 * 1000), 2011], [new Date(1289372400 * 1000), 2016], [new Date(1289458800 * 1000), 1914]]; 
     var plot = $.plot($("#placeholder"), [{data: grid_data0}, ], options0); 
    }); 

$("#placeholder").mouseout(function(e){ 
$("#test").hide(); 
}); 

function test() { 
//var oCanvas = $("#placeholder");//document.getElementById("placeholder"); 
//img=document.createElement("img"); 
//Save 
//img.src=oCanvas.toDataUrl(); 
//Restore 
//oCanvas.drawImage(img, 0, 0); 
//var oCanvas = $("#placeholder");//document.getElementById("placeholder"); 
//document.write(document.getElementById('placeholder').toDataUrl()); 
    Canvas2Image.saveAsPNG(document.getElementById('placeholder')); 
//Canvas2Image.saveAsPNG(oCanvas, true); 
} 
</script> 
<a onClick="JavaScript:test();">Click</a> 
    </body> 
</html> 

risposta

11

Il modo più semplice per farlo sta usando canvas.toDataURL("image/png"); ma ho anche trovato questo link per voi

http://nihilogic.dk/labs/canvas2image/

non ho provato.

+0

Ottengo qualche errore "oScaledCanvas.toDataURL non è una funzione" quando provo a eseguirlo. Inoltre sembra funzionare solo in nessuno browser IE. (Sto usando FireFox, tuttavia, il client potrebbe utilizzare IE). – Josh

+3

Avrai molta cura nel supportare canvas in IE. (ricorda, ti avevo avvertito.) – Hogan

+1

toDataURL fa parte di HTML5: http://www.w3.org/TR/html5/the-canvas-element.html – Hogan

2

Si può cercare in HighCharts che ha una funzione di esportazione e stampa. Funziona generando tutti gli elementi grafici in SVG prima del rendering sulla tela. Quando un utente vuole un'immagine, il javascript invia l'SVG al server per la conversione in un'immagine. Vedi the Exporting Module per maggiori dettagli.

Le strutture dati per i diagrammi elevati sono ben documented e non troppo difficili per eseguire il port flot su diagrammi elevati. Hanno anche molti esempi che puoi giocare interattivamente usando jsFiddle.

3

Se si desidera utilizzare Firefox, ho scritto WWW::Mechanize::Firefox, che può salvare l'intera pagina o gli elementi in una pagina in PNG.

È inoltre possibile automatizzare completamente Firefox, cosa che faccio con ffeedflotr, un programma di tracciamento di dati di piccole dimensioni che crea diagrammi di allineamento.