【问题标题】:jqPlot show image on button clickjqPlot 在按钮单击时显示图像
【发布时间】:2013-02-17 22:07:49
【问题描述】:

我正在使用 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>

更新

对于多个图表,仅显示一张图片。我需要如何处理这段代码才能显示多个图表?此外,每个图表都在不同的 div 中:

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

outerDiv = header = div = close = null;

更新2

由于某种原因,我的图表现在都没有显示。我猜我的代码中有一个简单的错误。我可以对我做错了什么有帮助吗?此外,这些图表称为“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);
});

更新

我为每个单独的图表找到了一个可行的解决方案。这是我的代码:

    $('#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;

});       

【问题讨论】:

  • 更新后的代码末尾缺少}
  • 我有图表显示。 'if (!$.jqplot.use_excanvas) {' 代码在页面加载时执行,但不会在图表上的任何点击事件上执行。我该如何解决这个问题?

标签: javascript image button charts jqplot


【解决方案1】:

替换这段代码:

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);
    });
}

然后,您需要将$each 中出现的任何'#chart1' 替换为chartId

编辑 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;
});

【讨论】:

  • 谢谢。效果很好。但是,如果我在一个页面上有 3 个图表,分别称为 chart1、chart2、chart3,我能否请一些帮助以使显示的图像适用于每个图表?
  • @user2023359 试试这个。
  • @user2023359 检查我对问题的评论。我认为你最后缺少一个花括号。
  • 感谢 Nick_w 的帮助。我找到了一个可行的解决方案并将其放在原始帖子中。
猜你喜欢
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多