2013-02-17 72 views
0

我正在使用jqPlot來顯示一些圖表,並使用按鈕將圖表保存爲圖像。jqPlot在按鈕上單擊顯示圖像

我可以幫助我們修改代碼,以免點擊按鈕時顯示圖片。

這裏是我的代碼:

<script class="code" type="text/javascript"> 
$(document).ready(function(){ 
var cosPoints = []; 
for (var i=0; i<2*Math.PI; i+=0.1){ 
cosPoints.push([i, Math.cos(i)]); 
} 
var plot1 = $.jqplot('chart1', [cosPoints], { 
    series:[{showMarker:false}], 
    axes:{ 
    xaxis:{ 
     label:'Angle (radians)' 
    }, 
    yaxis:{ 
     label:'Cosine' 
    } 
    } 
}); 

if (!$.jqplot.use_excanvas) { 
var outerDiv = $(document.createElement('div')); 
var header = $(document.createElement('div')); 
var div = $(document.createElement('div')); 

outerDiv.append(header); 
outerDiv.append(div); 

outerDiv.addClass('jqplot-image-container'); 
header.addClass('jqplot-image-container-header'); 
div.addClass('jqplot-image-container-content'); 

header.html('Right Click to Save Image As...'); 

var close = $(document.createElement('a')); 
close.addClass('jqplot-image-container-close'); 
close.html('Close'); 
close.attr('href', '#'); 
close.click(function() { 
    $(this).parents('div.jqplot-image-container').hide(500); 
}) 
header.append(close); 

$('#chart1').after(outerDiv); 
outerDiv.hide(); 

outerDiv = header = div = close = null; 

var btn = $(document.createElement('button')); 
btn.text('View Plot Image'); 
btn.addClass('jqplot-image-button'); 
btn.bind('click', {chart: $('#chart1')}, function(evt) { 
    var imgelem = evt.data.chart.jqplotToImageElem(); 
    var div = $(this).nextAll('div.jqplot-image-container').first(); 
    div.children('div.jqplot-image-container-content').empty(); 
    div.children('div.jqplot-image-container-content').append(imgelem); 
    div.show(500); 
    div = null; 
}); 

$('#chart1').after(btn); 
btn.after('<br />'); 
btn = null; 
} 
}); 
</script> 

UPDATE

對於多個圖表只有一個圖像顯示。我需要使用此代碼來顯示多個圖表?此外,每個圖是在不同的div:

$('#chart1').after(outerDiv); 
outerDiv.hide(); 

outerDiv = header = div = close = null; 

UPDATE2

出於某種原因,沒有我的圖表現在顯示。我猜測我的代碼中存在一個簡單的錯誤。我可以幫我解釋我做錯了什麼嗎?此外,圖表被稱爲'FinancialsLineGraph'和'SalesMonthVsBudgetLineGraph'。

這裏是我的全碼:

if (!$.jqplot.use_excanvas) { 
var charts = ['#FinancialsLineGraph', '#SalesMonthVsBudgetLineGraph']; 

$.each(charts, function(index, value) { 
    (function(chartId) { 
var outerDiv = $(document.createElement('div')); 
var header = $(document.createElement('div')); 
var div = $(document.createElement('div')); 

outerDiv.append(header); 
outerDiv.append(div); 

outerDiv.addClass('jqplot-image-container'); 

header.addClass('jqplot-image-container-header'); 
div.addClass('jqplot-image-container-content'); 

header.html('Right Click to Save Image As...'); 

var close = $(document.createElement('a')); 
close.addClass('jqplot-image-container-close'); 
close.html('Close'); 
close.attr('href', '#'); 
close.click(function() { 
    $(this).parents('div.jqplot-image-container').hide(500); 
}) 
header.append(close); 

$(chartId).after(outerDiv); 
outerDiv.hide(); 

outerDiv = header = div = close = null; 

    })(value); 
}); 

UPDATE

我已經找到了每個單獨的圖表工作方案。這裏是我的代碼:

$('#FinancialsLineGraph').bind('jqplotClick', function(ev, seriesIndex, pointIndex, data) {      
var outerDiv = $(document.createElement('div')); 
var header = $(document.createElement('div')); 
var div = $(document.createElement('div')); 

outerDiv.append(header); 
outerDiv.append(div); 

outerDiv.addClass('jqplot-image-container'); 
header.addClass('jqplot-image-container-header'); 
div.addClass('jqplot-image-container-content'); 

header.html('Right Click to Save Image As...'); 

var close = $(document.createElement('a')); 
close.addClass('jqplot-image-container-close'); 
close.html('Close'); 
close.attr('href', '#'); 
close.click(function() { 
    $(this).parents('div.jqplot-image-container').hide(500); 
}) 
header.append(close); 

$('#FinancialsLineGraph').after(outerDiv); 
outerDiv.hide(); 

outerDiv = header = div = close = null; 

var imgelem = $('#FinancialsLineGraph').jqplotToImageElem(); 
var div = $(this).nextAll('div.jqplot-image-container').first(); 
div.children('div.jqplot-image-container-content').empty(); 
div.children('div.jqplot-image-container-content').append(imgelem); 
div.show(500); 
div = null; 

});  
+0

你缺少一個'}'從更新的代碼的結束。 – 2013-02-20 06:31:43

+0

我有圖表顯示。 'if(!$。jqplot.use_excanvas){'代碼在頁面加載時執行,但不在圖表上的任何點擊事件中執行。我怎樣才能解決這個問題? – user2023359 2013-02-20 23:53:52

回答

0

替換的代碼塊:

var btn = $(document.createElement('button')); 
btn.text('View Plot Image'); 
btn.addClass('jqplot-image-button'); 
btn.bind('click', {chart: $('#chart1')}, function(evt) { 
    var imgelem = evt.data.chart.jqplotToImageElem(); 
    var div = $(this).nextAll('div.jqplot-image-container').first(); 
    div.children('div.jqplot-image-container-content').empty(); 
    div.children('div.jqplot-image-container-content').append(imgelem); 
    div.show(500); 
    div = null; 
}); 

$('#chart1').after(btn); 
btn.after('<br />'); 
btn = null; 

有了這一個:

$('#chart1').bind('jqplotClick', function(ev, seriesIndex, pointIndex, data) {     
    var imgelem = $('#chart1').jqplotToImageElem(); 
    var div = $(this).nextAll('div.jqplot-image-container').first(); 
    div.children('div.jqplot-image-container-content').empty(); 
    div.children('div.jqplot-image-container-content').append(imgelem); 
    div.show(500); 
    div = null; 
}); 

編輯:

要獲得代碼工作你可以嘗試如下的多個圖:

if (!$.jqplot.use_excanvas) { 
    var charts = ['#chart1', '#chart2', '#chart3']; 

    $.each(charts, function(index, value) { 
     (function(chartId) { 
      //The code that you currently have in the if-block. 

     })(value); 
    }); 
} 

然後您將需要chartId更換$each'#chart1'任何發生。

編輯2:

我覺得你完整的代碼丟失,其中的情節事件連線了一部分。

如果您目前有:

outerDiv = header = div = close = null; 

添加這一權利後:

$(chartId).bind('jqplotClick', function(ev, seriesIndex, pointIndex, data) {     
    var imgelem = $(chartId).jqplotToImageElem(); 
    var div = $(this).nextAll('div.jqplot-image-container').first(); 
    div.children('div.jqplot-image-container-content').empty(); 
    div.children('div.jqplot-image-container-content').append(imgelem); 
    div.show(500); 
    div = null; 
}); 
+0

謝謝。這很好。但是,如果我在一個頁面上有3個圖表,稱爲chart1,chart2,chart3,我可以請一些幫助讓圖像顯示爲適用於每個圖表嗎? – user2023359 2013-02-20 04:08:21

+0

@ user2023359看我的編輯。 – 2013-02-20 04:23:34

+0

你能看看我更新的帖子嗎? – user2023359 2013-02-20 04:36:43