-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (108 loc) · 2.77 KB
/
index.js
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
//var http=require('http');
//D3
var d3 = require('d3');
var barChart = require('./barchart');
var fs = require('fs');
/*
//http://eng.wealthfront.com/2011/12/converting-dynamic-svg-to-png-with.html
var jsdom = require('jsdom');
var scripts = [
"file://"+__dirname+"/d3.min.js",
"file://"+__dirname+"/d3.layout.min.js",
"file://"+__dirname+"/nv.d3.min.js",
"file://"+__dirname+"/bar.js"
];
htmlStub = '<!DOCTYPE html><div id="pie" style="width:'+w+'px;height:'+h+'px;"></div>';
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'image/png'});
var convert = child_proc.spawn("convert", ["svg:", "png:-"]);
var values = (url.parse(req.url, true).query['values'] || ".5,.5")
.split(",")
.map(function(v){return parseFloat(v)});
convert.stdout.on('data', function (data) {
res.write(data);
});
convert.on('exit', function(code) {
res.end();
});
jsdom.env({
features:{QuerySelector:true},
html: htmlStub,
scripts: scripts,
done: function(errors, window) {
var svgsrc = window.insertPie("#pie", w, h, values).innerHTML;
//jsdom's domToHTML will lowercase element names
svgsrc = svgsrc.replace(/radialgradient/g,'radialGradient');
convert.stdin.write(svgsrc);
convert.stdin.end();
}
});
}).listen(8089, "0.0.0.0");
*/
var getBarChart = function (params) {
var chart = barChart()
.data(params.data)
.width(params.width)
.height(params.height)
.xAxisLabel(params.xAxisLabel)
.yAxisLabel(params.yAxisLabel)
.css(params.css);
d3.select('body').append('div').attr('id', 'tmp').call(chart);
var selector = '#tmp';
var svg = d3.select(selector).node().innerHTML;
return svg;
};
var css = "body {background-color: #ababab;font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size: 12px;}.svg-chart .background {shape-rendering: crispEdges;stroke: #ccc;fill: #fdfdfd;stroke-width: 1px;}.svg-chart .bar { fill: #4682B4; }"
var chartData=[{
"name": "1",
"count": 36
},{
"name": "2",
"count": 43
},{
"name": "3",
"count": 50
},{
"name": "4",
"count": 41
},{
"name": "5",
"count": 34
},{
"name": "6",
"count": 44
},{
"name": "7",
"count": 67
},{
"name": "8",
"count": 63
},{
"name": "9",
"count": 51
},{
"name": "10",
"count": 53
},{
"name": "11",
"count": 52
},{
"name": "12",
"count": 60
}];
var svg = getBarChart({
data: chartData,
width: 400,
height: 300,
xAxisLabel: '2012',
yAxisLabel: 'Views',
containerId: 'bar-chart',
css: css
});
console.log('SVG---------------------------');
console.log(svg);
console.log('SVG-END-----------------------');
fs.writeFile('svg.svg', svg, function (err) {
if (err) throw err;
console.log('svg saved!');
});