【问题标题】:How to fix issue with svg element not rendering into image (dom2img)如何解决 svg 元素未呈现为图像的问题 (dom2img)
【发布时间】:2019-04-22 10:07:37
【问题描述】:

我正在使用 amcharts4 构建热图,然后通过dom2img 将其转换为图像。一切都很好,地图本身也被渲染了,但图例没有被 dom2img 捕获。图例本身是一个 svg 元素。

我认为它可能与渐变有关,因为这是唯一的元素。

svg 元素:

<g fill-opacity="1" fill="url(&quot;http://localhost:4200/campaigns/pptx/2#gradient-id-257&quot;)" style="pointer-events: none;"><path d="M0,0 L455,0 a0,0 0 0 1 0,0 L455,20 a0,0 0 0 1 -0,0 L0,20 a0,0 0 0 1 -0,-0 L0,0 a0,0 0 0 1 0,-0 Z"></path></g> 

Dom2img 应该渲染图例元素。

【问题讨论】:

  • 你试过不同浏览器的输出吗?
  • 请尝试将渐变(以及您可能拥有的任何其他内容)从 localhost 移动到您的 svg 的 &lt;defs&gt;
  • @MangeshSathe 我试过 Firefox 和 Chrome。结果相同
  • @enxaneta 我真的不知道该怎么做。我正在使用自己构建一切的库。

标签: javascript svg amcharts html2canvas amcharts4


【解决方案1】:

改用 amCharts 内置导出功能:https://www.amcharts.com/docs/v4/concepts/exporting/

// First enable export
chart.exporting.menu = new am4core.ExportMenu();

chart.exporting.extraSprites.push({
    "sprite": legendContainer,
    "position": "bottom",
    "marginTop": 20
});

// Then you can get the Base64 image
var imgData = chart.exporting.getImage("png");

您还可以使用extraSprites 包含图例,如上所示。阅读更多关于including external legend in export的信息。

【讨论】:

  • 谢谢。我试过这个,但不幸的是它有同样的结果。图例不渲染(没有颜色,只有数字)
猜你喜欢
  • 1970-01-01
  • 2014-01-30
  • 2020-08-26
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2020-11-10
  • 2018-02-09
相关资源
最近更新 更多