在将jqPlot图形保存到图像文件时隐藏轴标签(Axis label hidden while saving a jqPlot graph to an image file)

编程入门 行业动态 更新时间:2024-10-28 17:16:13
在将jqPlot图形保存到图像文件时隐藏轴标签(Axis label hidden while saving a jqPlot graph to an image file)

编辑 :我正在使用jqplotToImageStr({})来保存图表图像。 但是,生成的图像不包括轴标签。 它只输出图表本身。 我发现轴标签实际上在图表后面。 我已将轴标签移动到图表边界内并设置z-index,以便标签显示在图表的顶部。 但是当调用jqplotToImageStr({})时,轴标签会落后于图表。 如何在捕获时确保轴标签包含在图像中?

这是创建图像的代码。

var imageData = $('#chart_div').jqplotToImageStr({}); var copyImage = $('<img/>').attr('src', imageData); $('#copy-container').html(copyImage);

Edit: I am using jqplotToImageStr({}) to save the chart image. However the resulting image does not include the axis labels. It only outputs the chart itself. I found out that the axis labels are actually behind the chart. I had moved the axis labels inside the chart boundary and set z-index so that the labels are shown on top of the chart. But when jqplotToImageStr({}) is called the axis labels fall behind the chart. How do I make sure that the axis labels are included in the image at time of capture?

here is the code to create the image.

var imageData = $('#chart_div').jqplotToImageStr({}); var copyImage = $('<img/>').attr('src', imageData); $('#copy-container').html(copyImage);

最满意答案

好吧,我设法解决了这个问题。 在绘制图表后,我使用了画布并在画布上绘制了轴标签。 灵感来自这篇文章 。

function jqplotToImg(obj) { var newCanvas = document.createElement("canvas"); newCanvas.width = obj.find("canvas.jqplot-base-canvas").width(); newCanvas.height = obj.find("canvas.jqplot-base-canvas").height(); var baseOffset = obj.find("canvas.jqplot-base-canvas").offset(); // make white background for pasting var context = newCanvas.getContext("2d"); context.fillStyle = "rgba(255,255,255,1)"; context.fillRect(0, 0, newCanvas.width, newCanvas.height); obj.children().each(function () { if ($(this)[0].tagName.toLowerCase() == 'canvas') { // all canvas from the chart var offset = $(this).offset(); newCanvas.getContext("2d").drawImage(this, offset.left - baseOffset.left, offset.top - baseOffset.top ); } // for the div's with the X and Y axis }); obj.children().each(function () { if ($(this)[0].tagName.toLowerCase() == 'div') { if ($(this).attr('class') == "jqplot-axis jqplot-yaxis") { $(this).children("canvas").each(function () { var offset = $(this).offset(); newCanvas.getContext("2d").drawImage(this, offset.left - baseOffset.left, offset.top - baseOffset.top ); }); } else if ($(this).attr('class') == "jqplot-axis jqplot-xaxis") { $(this).children("canvas").each(function () { var offset = $(this).offset(); newCanvas.getContext("2d").drawImage(this, offset.left - baseOffset.left, offset.top - baseOffset.top ); }); } } });

Ok I managed to sort this out. I used a canvas and drew the axis labels on the canvas after drawing the chart. Inspired by this post.

function jqplotToImg(obj) { var newCanvas = document.createElement("canvas"); newCanvas.width = obj.find("canvas.jqplot-base-canvas").width(); newCanvas.height = obj.find("canvas.jqplot-base-canvas").height(); var baseOffset = obj.find("canvas.jqplot-base-canvas").offset(); // make white background for pasting var context = newCanvas.getContext("2d"); context.fillStyle = "rgba(255,255,255,1)"; context.fillRect(0, 0, newCanvas.width, newCanvas.height); obj.children().each(function () { if ($(this)[0].tagName.toLowerCase() == 'canvas') { // all canvas from the chart var offset = $(this).offset(); newCanvas.getContext("2d").drawImage(this, offset.left - baseOffset.left, offset.top - baseOffset.top ); } // for the div's with the X and Y axis }); obj.children().each(function () { if ($(this)[0].tagName.toLowerCase() == 'div') { if ($(this).attr('class') == "jqplot-axis jqplot-yaxis") { $(this).children("canvas").each(function () { var offset = $(this).offset(); newCanvas.getContext("2d").drawImage(this, offset.left - baseOffset.left, offset.top - baseOffset.top ); }); } else if ($(this).attr('class') == "jqplot-axis jqplot-xaxis") { $(this).children("canvas").each(function () { var offset = $(this).offset(); newCanvas.getContext("2d").drawImage(this, offset.left - baseOffset.left, offset.top - baseOffset.top ); }); } } });

更多推荐

本文发布于:2023-07-04 03:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1016819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像文件   图形   标签   Axis   jqPlot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!