2013-02-17 33 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