2014-01-31 93 views
0

我想在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 

感謝您的幫助。

+0

有沒有人有想法?我非常需要幫助......謝謝 – user3181885

+0

http://bigsnarf.wordpress.com/2012/12/28/multi-dimensional-charting-built-to-work-natively-with-crossfilter-rendered-using-d3- js/ – Ketan

+0

試試這個博客文章顯示節點js上的d3/dc和交叉過濾器。 http://bigsnarf.wordpress.com/2012/12/28/multi-dimensional-charting-built-to-work-natively-with-crossfilter-rendered-using-d3-js/ – Ketan

回答

1

你需要讓D3你的模塊中的全局變量:如果你分手的弦

htmlStub = '<html><head></head><body><div id="dataviz-container"></div><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>'; 

象這樣的錯誤是比較明顯的:

global.d3 = d3; 

... chart rendered here 
dc.renderAll(); 

    var svgsrc = window.document.querySelector('#bubbleChart'); 
    fs.writeFile('chart.svg', svgsrc.innerHTML, function(err) { 
     if (err) { 
      console.log('error saving document', err); 
     } else { 
      console.log('The file was saved!'); 
     } 
    }); 

然後使用像ImageMagick這樣的命令行工具將svg轉換爲png。看到這個帖子的例子: http://eng.wealthfront.com/2011/12/converting-dynamic-svg-to-png-with.html

+0

謝謝..錯誤按摩是走了=) – user3181885

+0

不幸的是我沒有得到結束的圖表....你有一個想法如何我在最後得到一個PNG圖表? – user3181885

+0

你現有的代碼應該產生了一個html文件。您可以通過使用Web瀏覽器查看文件來手動處理文件。如果你想要一個更自動化的過程,那麼你可以將輸出保存爲.svg文件而不是HTML。代碼看起來像我上面添加的代碼。 –

0

您的htmlStub文件錯誤,它還有一個額外的</script>;這幾乎肯定會導致node.js無法運行它。

應該(可能)是:

htmlStub = '<html><head></head><body>'; 
    htmlStub += '<div id="dataviz-container"></div>'; 
    htmlStub += '<script type="text/javascript" src="js/d3.js"></script>'; 
    htmlStub += '<script type="text/javascript" src="js/crossfilter.js"></script>'; 
    htmlStub += '<script type="text/javascript" src="js/dc.js"></script>'; 
    htmlStub += '</body></html>';