JqPlot作为图片

11

在最新的JqPlot示例中(参见这里),有一些图表下面有按钮,您可以单击它们,然后一个div会滑下来显示图表的图像,让您右键单击并另存为。

我已经检查了源代码,但我无法看到这是在哪里发生的。我看到了各种关于此的讨论(请参见此处),但我的JavaScript水平最多只能算基础水平。然而,这是我想要在当前项目中实现的内容。

是否有人知道如何完成这个完整教程,即从实际的jquery代码到html代码的实现?

5个回答

29

这是我能编写的最简单的示例:

//after creating your plot do
var imgData = $('#chart1').jqplotToImageStr({}); // given the div id of your plot, get the img data
var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
$('#imgChart1').append(imgElem);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ // append the img to the DOM

这里尝试示例。


@Mark,你能否详细说明一下?我需要在IE7/8中使其正常工作。 - wonza
1
@Chopo87,这不是我提供的代码,而是jplot本身:https://bitbucket.org/cleonello/jqplot/issue/710/compatibility-with-jquery-v19 - Mark
@Wonza,IE 6/7/8需要使用excanvas(http://www.jqplot.com/docs/files/usage-txt.html)与jqPlot(实际上是任何画布绘图)一起使用。我不知道我提供的代码是否适用于这些IE版本。我已经从所有系统中清除了它们,永远不会再回头看了 :) - Mark
3
抱歉,@wonza,你运气不佳。根据jqplot源代码:因为excanvas和IE < 9不支持toDataURL并且无法导出图像。 如果使用了excanvas,将返回null。 - Mark
@Mark,请问你能给我展示一下这个问题的解决方案吗?http://stackoverflow.com/questions/21905400/jqplot-image-download-by-a-button-click-in-ie 对不起,在这里发帖。 - DilanG
显示剩余4条评论

3

马克,感谢您的贡献。顺便提一下,有时您可能会混合使用光标和缩放功能,并且您可能需要创建一个部分图形的图像,以便恢复缩放以创建其他部分的图像。这可能不容易,因为jqPlot不会为您将图形恢复到原始图形,所以您必须手动完成。

我的解决方案

  1. 在您的 $.jqplot 选项中添加以下代码:

    cursor: { show: true, zoom: true, looseZoom: true, showTooltip: false, dblClickReset:true, }

    这样可以使用户回到图表的初始状态。如果您选择这种方法,请记得通过提示信息告知用户如何通过建议注释还原,例如:

    双击图表将缩放还原至100% ,以提高可用性。

对于那些更倾向于编程的人,这里有一个解决方案,其中包括马克介绍的一些代码(再次感谢)

  1. Create a button right below the graph, furnish it with an id attribute and attach an even handler to its click function,

            <button id="show_revert_graph_btn" class="jqplot-replot-button" title="Reset Zoom back to 100%">Revert the Graph</button>
    
  2. attach an event listener and implement/register a handler like this

$('#show_revert_graph_btn').click(function(){
  // simulating a double click on the  canvas
  // not that the selecting expression includes 
  // the div id of where i chose to plot the chart "chart104" and the default class
  // provided to the canvas holding my chart i.e "canvas.jqplot-event-canvas"
  // then i double click it
        $('#chart104 canvas.jqplot-event-canvas').dblclick();
    });

缩放后的图像创建 在我的应用程序中,我需要创建多个图像,来自图表的不同部分,因此缩放允许我放大这些部分,而画布到图像功能允许我保存在我放大某个点后在画布上显示的当前数据。 挑战是如何将我的新缩放点重新加载为新的要复制的图像 解决方法

  1. 创建绘制图像的按钮
  2. 附加监听器到jquery的toggle事件,允许您显示和隐藏图像
  3. 处理图像以管理缩放事件,即当我缩放时生成一个新图像,这样当我单击时,我看到的是缩放的部分的图像,而不是整个图表的旧图像

 $('#show_plotted_image_btn').toggle(
        function(){
            console.log('showing graph');
            // get the image
            function genImg(){
            var imgData = $('#chart104').jqplotToImageStr({});
       // given the div       id of your plot, get the img data
            var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
            $('#plotted_image_div').empty(); // remove the old graph
            $('#plotted_image_div').append(imgElem);
            };
            genImg();
            // show the image
            $('#plotted_image_div').css('display','block');

        },
        function(){
            // hide the image
            console.log('hiding graph');
            $('#plotted_image_div').css('display','none');
        }
    );

*在我的实现中,我只想展示最新的图片,因此每次请求新图片时,我都会通过$('#plotted_image_div').empty();删除旧图片,这只是简单地清空旧图片,然后添加新图片。*

*以下是HTML代码,以便需要进一步了解的人参考。

<div id="chart104" class=""></div>

            <button id="show_plotted_image_btn" class="jqplot-image-button">View Plot Image</button>
            <span style="font-weight: bold; color:#FC2896;"> Double click on the Graph to Reset Zoom back to 100%</span>
            <button id="show_revert_graph_btn" class="jqplot-replot-button" title="Reset Zoom back to 100%">Revert the Graph</button>
            <div id="plotted_image_div" class="" style="display: none;"></div>

祝你好运


2

当你遇到图像输出问题时,你需要更改jquery.jqplot.js文件。在一些浏览器中(如Chrome和Firefox),该脚本会陷入无限循环。

将以下代码更改为:

for (var i=0; i<wl; i++) {
    w += words[i];
    if (context.measureText(w).width > tagwidth) {
        breaks.push(i);
        w = '';
        i--;
    }   
}

转换为:

for (var i=0; i<wl; i++) {
    w += words[i];
    if (context.measureText(w).width > tagwidth && w.length > words[i].length) {
        breaks.push(i);
        w = '';
        i--;
    }   
}

在你的html中添加以下内容:

<div id="chart"></div>
<div id="imgChart"></div>

在你的jqplot代码之后添加以下内容到jquery中:
$(document).ready(function(){
    //after creating your plot do
    var imgData = $('#chart').jqplotToImageStr({}); // given the div id of your plot, get the img data
    var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
    $('#imgChart').append(imgElem);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ //
});

请查看此处的演示: 点击这里

2

太好了。您有任何关于如何在HTML层面集成它的想法吗? - 109221793

1
这个解决方案对我很有帮助。简单而快速。
//after creating your plot do
var imgData = $('#chart1').jqplotToImageStr({}); // given the div id of your plot, get the img data
var imgElem = $('<img/>').attr('src',imgData); // create an img and add the data to it
$('#imgChart1').append(imgElem);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ //

我正在使用PrimeFaces 3.2,因此无法使用PrimeFaces中可用的新功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接