我有以下問題:我正在轉換由d3.js創建的svg plot,但是png與svg完全不同。前 SVG轉換後轉換將svg轉換爲png時添加css樣式
如何申請相同的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>
我不是專家,但也許你可以嘗試像不同的庫https://github.com/exupero/saveSvgAsPng – makshh
我正在嘗試這個,但不幸的是不工作;/ –