-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.html
More file actions
254 lines (213 loc) · 7.86 KB
/
1.html
File metadata and controls
254 lines (213 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js">
</script>
<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script>
<style type="text/css">
.floatTL{
position:absolute;
top:0px;
left:0px;
}
.bubble{
position:absolute;
top:0px;
left:0px;
}
</style>
<style>
#saveButton{
position: absolute;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>
<button id='saveButton' align="center-right">Export my D3 visualization to PNG</button>
</div>
<div id ="myImage" width="648" height="163">
<img src="download.jpg"/>
</div>
<script>
var diameter = 100, //max size of the bubbles
color = d3.scale.category20b(); //color category
var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5);
var svg = d3.select("body")
.append("svg")
.attr("width", 350)
.attr("height", 200)
.attr("class", "bubble");
d3.csv("fruit.csv", function(error, data){
//convert numerical values from strings to numbers
data = data.map(function(d){ d.value = +d["Amount"]; return d; });
//bubbles needs very specific format, convert data to this.
var nodes = bubble.nodes({children:data}).filter(function(d) { return !d.children; });
//setup the chart
var bubbles = svg.append("g")
.attr("transform", "translate(0,0)")
.selectAll(".bubble")
.data(nodes)
.enter();
//create the bubbles
//debugger;
//debugger;
bubbles.append("circle")
.attr("r", function(d){d.r=d["Radius"]; return d.r; })
.attr("cx", function(d){ d.x=d["Xcord"];return d.x; })
.attr("cy", function(d){ d.y=d["Ycord"];return d.y; })
//.style("fill", function(d) { return color(d.value); });
.style("fill","rgba(135, 206, 250, 0.3)");//transform bubbles
bubbles.append("circle")
.attr("r", function(d){d.r=d["Radius"]; return 4; })
.attr("cx", function(d){ d.x=d["Xcord"];return d.x; })
.attr("cy", function(d){ d.y=d["Ycord"];return d.y; })
//.style("fill", function(d) { return color(d.value); });
.style("fill","rgba(135, 206, 250, 1)");//transform bubbles
//format the text for each bubble
var a="";
bubbles.append("text")
.append('svg:tspan')
.attr("x", function(d){ d.x=d["Xcord"];return d.x; })
.attr("y", function(d){ d.y=d["Ycord"];return d.y; })
//.attr("text-anchor", "middle")
.text(function(d){ a=d["Fruit"];
return a;})
.style({
"font-weight":"bold",
"fill":"black",
"font-family":"Helvetica Neue, Helvetica, Arial, san-serif",
"font-size": "12px"
})
.append('svg:tspan')
.attr("x", function(d){ d.x=d["SecondLineX"];return d.x; })
.attr("y", function(d){ d.y=d["SecondLineY"];return d.y; })
.text(function(d){ a=d["SecondVal"];
return a;})
.style({
"font-weight":"normal",
"fill":"black",
"font-family":"Helvetica Neue, Helvetica, Arial, san-serif",
"font-size": "12px"
})
.append('svg:tspan')
.attr("x", function(d){ d.x=d["ThirdLineX"];return d.x; })
.attr("y", function(d){ d.y=d["ThirdLineY"];return d.y; })
.text(function(d){ a=d["ThirdVal"];
return a;})
.style({
"fill":"black",
"font-family":"Helvetica Neue, Helvetica, Arial, san-serif",
"font-size": "12px"
})
.append('svg:tspan')
.attr("x", function(d){ d.x=d["FourthLineX"];return d.x; })
.attr("y", function(d){ d.y=d["FourthLineY"];return d.y; })
.text(function(d){ a=d["FouthVal"];
return a;})
.style({
"fill":"black",
"font-family":"Helvetica Neue, Helvetica, Arial, san-serif",
"font-size": "12px"
});
})
</script>
<script>
// Set-up the export button
d3.select('#saveButton').on('click', function(){
var svgString = getSVGString(svg.node());
svgString2Image( svgString, 310, 163, 'png', save ); // passes Blob and filesize String to the callback
function save( dataBlob, filesize ){
saveAs( dataBlob, 'D3 vis exported to PNG.png' ); // FileSaver.js function
}
});
// Below are the functions that handle actual exporting:
// getSVGString ( svgNode ) and svgString2Image( svgString, width, height, format, callback )
function getSVGString( svgNode ) {
svgNode.setAttribute('xlink', 'http://www.w3.org/1999/xlink');
var cssStyleText = getCSSStyles( svgNode );
appendCSS( cssStyleText, svgNode );
var serializer = new XMLSerializer();
var svgString = serializer.serializeToString(svgNode);
svgString = svgString.replace(/(\w+)?:?xlink=/g, 'xmlns:xlink='); // Fix root xlink without namespace
svgString = svgString.replace(/NS\d+:href/g, 'xlink:href'); // Safari NS namespace fix
return svgString;
function getCSSStyles( parentElement ) {
var selectorTextArr = [];
// Add Parent element Id and Classes to the list
selectorTextArr.push( '#'+parentElement.id );
for (var c = 0; c < parentElement.classList.length; c++)
if ( !contains('.'+parentElement.classList[c], selectorTextArr) )
selectorTextArr.push( '.'+parentElement.classList[c] );
// Add Children element Ids and Classes to the list
var nodes = parentElement.getElementsByTagName("*");
for (var i = 0; i < nodes.length; i++) {
var id = nodes[i].id;
if ( !contains('#'+id, selectorTextArr) )
selectorTextArr.push( '#'+id );
var classes = nodes[i].classList;
for (var c = 0; c < classes.length; c++)
if ( !contains('.'+classes[c], selectorTextArr) )
selectorTextArr.push( '.'+classes[c] );
}
// Extract CSS Rules
var extractedCSSText = "";
for (var i = 0; i < document.styleSheets.length; i++) {
var s = document.styleSheets[i];
try {
if(!s.cssRules) continue;
} catch( e ) {
if(e.name !== 'SecurityError') throw e; // for Firefox
continue;
}
var cssRules = s.cssRules;
for (var r = 0; r < cssRules.length; r++) {
if ( contains( cssRules[r].selectorText, selectorTextArr ) )
extractedCSSText += cssRules[r].cssText;
}
}
return extractedCSSText;
function contains(str,arr) {
return arr.indexOf( str ) === -1 ? false : true;
}
}
function appendCSS( cssText, element ) {
var styleElement = document.createElement("style");
styleElement.setAttribute("type","text/css");
styleElement.innerHTML = cssText;
var refNode = element.hasChildNodes() ? element.children[0] : null;
element.insertBefore( styleElement, refNode );
}
}
function svgString2Image( svgString, width, height, format, callback ) {
var format = format ? format : 'png';
var imgsrc = 'data:image/svg+xml;base64,'+ btoa( unescape( encodeURIComponent( svgString ) ) ); // Convert SVG string to data URL
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
console.log("canvas dimensions: w,h"+canvas.width +","+ canvas.height);
var image = new Image();
var image2 = new Image();
image2.src="download.jpg";
image.onload = function() {
context.clearRect ( 50, 50, 310, 163 );
context.drawImage(image2,50,50,310,163);
context.drawImage(image, 50, 50, 310, 163);
canvas.toBlob( function(blob) {
var filesize = Math.round( blob.length/1024 ) + ' KB';
if ( callback ) callback( blob, filesize );
});
};
image.src = imgsrc;
}
</script>
</script>
</body>
</html>