【发布时间】:2016-01-16 07:53:52
【问题描述】:
我有以下问题:我正在转换由 d3.js 创建的 svg 图,但 png 与 svg 完全不同。 转换前的SVG
如何将相同的 .css 代码应用于转换?有没有像创建 svg 时那样链接到 .css 文件?
svgtopng.js 代码:
d3.select("#save").on("click", function(){
var html = d3.select('#h3 svg')
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
//HERE I WAS TRYING WITH .style but I want to link all the css classes from .css file below//
.node().parentNode.innerHTML;
var imgsrc = 'data:image/svg+xml;base64,'+ btoa(html);
var img = '<img src="'+imgsrc+'">';
d3.select("#svgdataurl").html(img);
css代码:
body {
font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
padding-left: 10px;
color: #222;
}
/* Histogram line areas */
.Histogram .line {
shape-rendering: crispEdges;
}
.LineChart .line {
stroke-width: 2px;
}
/* 2D Histogram bins */
.Histogram2D .tile {
shape-rendering: crispEdges;
}
/* Error bars */
.uncertainty {
stroke-width: 1px;
shape-rendering: crispEdges;
}
/* Axes */
.axis path {
fill: none;
stroke: #000;
stroke-width: 1px;
shape-rendering: crispEdges;
}
.axis .tick line {
stroke: #000;
stroke-width: 1px;
shape-rendering: crispEdges;
}
.grid {
fill: none;
stroke: #e0e0e0;
shape-rendering: crispEdges;
}
.axis-label {
font-size: 0.9em;
}
/* TextBox */
.TextBox rect {
shape-rendering: crispEdges;
}
/* Brush, i.e. zoom box */
.brush .extent {
fill: #000;
fill-opacity: 0.125;
stroke: #000;
stroke-opacity: 0.2;
stroke-width: 1px;
shape-rendering: crispEdges;
}
/* 'Clear zoom' text */
.clear-button {
cursor: pointer;
}
.clear-button rect {
fill: #eee;
stroke: #000;
stroke-width: 1px;
stroke-opacity: 0.125;
border-radius: 2px;
}
.clear-button:hover rect {
fill: #e4e4e4;
}
.clear-button:active rect {
fill: #ddd;
}
.zscale-box {
stroke: #000;
stroke-width: 1px;
shape-rendering: crispEdges;
}
HTML 代码:
<button id="save">Save as Image</button></h1>
<div id="svgdataurl"></div>
<span id="h3">
</span>
【问题讨论】:
-
我不是专家,但也许您可以尝试不同的库,例如 github.com/exupero/saveSvgAsPng
-
我正在尝试这个,但不幸的是不起作用;/
标签: javascript html css d3.js svg