我想在node.js服務器上渲染dcCharts。我有一個d3.js和node.js的例子。但我的代碼不起作用。我是一個初學者與node.js,希望yoe有一個想法?dc.js與node.js服務器端
的用於d3.js與node.js的代碼:example
,在這裏我嘗試用dc.js和node.js的:
var d3 = require('d3')
, dc = require('dc')
, jsdom = require('jsdom')
, fs = require('fs')
, htmlStub = '<html><head></head><body><div id="dataviz-container"></div><script src="js/d3.v3.min.js"></script></body></html>'
jsdom.env({
features : { QuerySelector : true }
, html : htmlStub
, done : function(errors, window) {
// this callback function pre-renders the dataviz inside the html document, then export result into a static file
var el = window.document.querySelector('#dataviz-container')
, body = window.document.querySelector('body')
var data = [
{date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
{date: "12/28/2012", http_404: 2, http_200: 10, http_302: 100},
{date: "12/29/2012", http_404: 1, http_200: 300, http_302: 200},
{date: "12/30/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "12/31/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "01/01/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/02/2013", http_404: 1, http_200: 10, http_302: 1},
{date: "01/03/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/04/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/05/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/06/2013", http_404: 2, http_200: 200, http_302: 1},
{date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
];
var ndx = crossfilter(data);
var parseDate = d3.time.format("%m/%d/%Y").parse;
data.forEach(function(d) {
d.date = parseDate(d.date);
d.total= d.http_404+d.http_200+d.http_302;
});
var dateDim = ndx.dimension(function(d) {return d.date;});
var hits = dateDim.group().reduceSum(function(d) {return d.total;});
var hitslineChart = dc.pieChart('dataviz-container');
hitslineChart
.width(500).height(200)
.transitionDuration(500)
.colors(d3.scale.category10())
.radius(90)
.dimension(dateDim)
.group(hits);
dc.renderAll();
// save result in an html file, we could also keep it in memory, or export the interesting fragment into a database for later use
var svgsrc = window.document.innerHTML
fs.writeFile('index.html', svgsrc, function(err) {
if(err) {
console.log('fehler beim speichern', err)
} else {
console.log('Datei wurde gespeichert!')
}
})
} // end jsDom done callback
})
我覺得var hitslineChart = dc.pieChart('dataviz-container');
是錯誤的。
更改知道我htmlStub和dc.pieChart:
var hitslineChart = dc.pieChart('el');
htmlStub = '<html><head></head><body><div id="dataviz-container"></div></script><script type="text/javascript" src="js/d3.js"></script><script type="text/javascript" src="js/crossfilter.js"></script><script type="text/javascript" src="js/dc.js"></script></body></html>'
不幸的是,我仍然得到這個錯誤:
C:\Users\kasse\Code\node-modules\dc\dc.js:2366
var _colors = d3.scale.category20c();
ReferenceError: d3 is not defined
at Object.dc.colorChart (C:\Users\kasse\Code\node_modules\dc\dc.js:2366:19)
at Object.dc.pieChart (C:\Users\kasse\Code\node_modules\dc\dc.js:2971:31)
at jsdom.env.done (C:\Users\kasse\Code\pre_render.js:44:25)
at C:\Users\kasse\Code\node_modules\jsdom\lib\jsdom.js:255:9
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:902:3
感謝您的幫助。
有沒有人有想法?我非常需要幫助......謝謝 – user3181885
http://bigsnarf.wordpress.com/2012/12/28/multi-dimensional-charting-built-to-work-natively-with-crossfilter-rendered-using-d3- js/ – Ketan
試試這個博客文章顯示節點js上的d3/dc和交叉過濾器。 http://bigsnarf.wordpress.com/2012/12/28/multi-dimensional-charting-built-to-work-natively-with-crossfilter-rendered-using-d3-js/ – Ketan