我正在使用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;
});
你缺少一個'}'從更新的代碼的結束。 – 2013-02-20 06:31:43
我有圖表顯示。 'if(!$。jqplot.use_excanvas){'代碼在頁面加載時執行,但不在圖表上的任何點擊事件中執行。我怎樣才能解決這個問題? – user2023359 2013-02-20 23:53:52