点击按钮jqPlot显示图像
jqPlot show image on button click
我正在使用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"answers"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;
});
替换此代码块:
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;
});
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- 使用javascript在Flash中加载外部图像
- 可以在这里为背景图像设置滤镜吗
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 使用 js 将背景图像设置为 HTML
- Javascript将图像src更改为淡入淡出
- 无法在本地计算机中将画布另存为图像
- 压缩phonegap中ios的图像插件
- 如何在鼠标悬停时在另一个图像上滑动图像.