2013-08-20 39 views
2

此頁面需要顯示從CSV文件讀取數據的圖形。繪製來自csv文件的數據

我一直在關注TheCodingTutorials的教程。

我也試圖按照Multi-Column Data教程,以便我可以將名稱添加到圖形。這是我迷失的地方,教程讓它聽起來很簡單,但我不明白。每次我嘗試編輯代碼時,它都會出錯。

如果您只想讀取單列csv文件,它可以很好地工作。

但是我想讀取多列csv文件。

此外,如果有什麼可以讓它更好,請讓我知道。

<html> 
    <head> 
    <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script> 
    <script type="text/javascript"> 

    <html> 
<head> 
<meta http-equiv="Expires" content="-1"> 
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script> 
<script type="text/javascript"> 

function timedRefresh(timeoutPeriod) { 
    setTimeout("location.reload(true);",timeoutPeriod); 
    { 
    d3.text("data2.csv", function(unparsedData) 
    { 
    var data = d3.csv.parseRows(unparsedData); 

    //Create the SVG graph. 
    var svg = d3.select("body").append("svg").attr("width", "100%").attr("height", "100%"); 


    var dataEnter = svg.selectAll("rect").data(data).enter(); 
    var graphHeight = 450; 
    var barWidth = 20; 
    var barSeparation = 10; 
    var maxData = 105; 
    var horizontalBarDistance = barWidth + barSeparation; 
    var textYOffset = horizontalBarDistance/2 - 12; 
    var textXOffset = 20; 
    var barHeightMultiplier = graphHeight/maxData; 


    //Draw the bars. 
    dataEnter.append("rect").attr("y", function(d, i) 
    { 
    return i * horizontalBarDistance; 
    }).attr("x", function(d) 
    { 
    return 100; 
    }).attr("height", function(d) 
    { 
    return barWidth; 
    }).attr("width", function(d) 
    { 
    return d * barHeightMultiplier; 
    }); 


    //Draw the text. 
    dataEnter.append("text").text(function(d) 
    { 
    return d; 
    }).attr("y", function(d, i) 
    { 
    return i * horizontalBarDistance + textXOffset; 
    }).attr("x"); 
}); 
}; 
} 
</script> 
</head> 
<body onLoad="JavaScript:timedRefresh(10000);"> 
</body> 
</html> 

我的CSV文件現在看起來是這樣

names,data 
john,78 
brad,105 
amber,103 
james,2 
dean,74 
pat,45 
matt,6 
andrew,18 
ashley,15 

============================= ================================================== ===

UPDATE

==================================== ==============================================

感謝您的全力幫助,這是我更新的代碼。

<html> 
<head> 
<meta http-equiv="Expires" content="-1"> 

<script type="text/javascript" src=".\JavaScripts\d3.v3.min.js"></script> 
<script type="text/javascript">setTimeout(function(){window.location.href='index2.html'},120000); 

    d3.csv("./data/data.csv", function(data){ 

    //Create the SVG graph. 
    var svg = d3.select("#graph").append("svg").attr("width", "1800").attr("height", "600"); 

    var dataEnter = svg.selectAll("rect").data(data).enter(); 
    var graphWidth = 800; 
    var barWidth = 40; 
    var barSeparation = 30; 
    var maxData = 2; 
    var horizontalBarDistance = barWidth + barSeparation; 
    var textYOffset = 25; 
    var barXOffset = 260; 
    var barYOffset = 5; 
    var numXOffset = 230; 
    var barHeightMultiplier = graphWidth/maxData; 
    var fontSize = "30px"; 

    var color = d3.scale.category10(); 


    //Draw the bars. 
    dataEnter.append("rect") 
    .attr("fill",function(d,i){return color(i);}) 
    .attr("y", function(d, i){return i * horizontalBarDistance - barYOffset;}) 
    .attr("x", barXOffset) 
    .attr("height", function(d){return barWidth;}) 
    .attr("width", function(d){return d.data * barHeightMultiplier;}); 


    //Draw the text. 
    dataEnter.append("text") 
    .text(function(d){return d.Name;}) 
    .attr("font-size", fontSize) 
    .attr("font-family", "sans-serif") 
    .attr("y", function(d, i){return i * horizontalBarDistance + textYOffset;}) 
    .attr("x"); 

    //Draw the numbers. 
    dataEnter.append("text") 
    .text(function(d){return d.data;}) 
    .attr("font-size", fontSize) 
    .attr("font-family", "sans-serif") 
    .attr("y", function(d, i){return i * horizontalBarDistance + textYOffset;}) 
    .attr("x", numXOffset); 

    //Draw the Target bar 
    dataEnter.append("rect") 
    .attr("fill", "red") 
    .attr("y", function(d, i){return i * horizontalBarDistance;}) 
    .attr("x", barXOffset + graphWidth) 
    .attr("height", 70) 
    .attr("width", 10); 

}); 

</script> 

<style type="text/css"> 
#title { 
    font-family:sans-serif; 
    font-size: 50px; 
    color:#000; 
    text-decoration: underline; 
    text-align: center; 
    width: 100%; 
    position:relative; 
    margin-top:20; 
} 
#graph { 
    overflow:hidden; 
    margin-top:40; 
} 
</style> 

</head> 
<body> 
<div id="title">Graph 1</div> 
<div id="graph"></div> 
</body> 
</html> 
+0

你得到的錯誤是什麼。你能鏈接到一些正在運行的代碼嗎? –

+0

當我說出錯我的意思是,它只是不起作用。我想這是因爲我不知道如何分隔列。我只需要更改代碼,以便數據引用「數字」列。 [編碼教程](http://thecodingtutorials.blogspot.com.au/2012/09/using-multi-column-data-with-d3-part-2.html)示例以文本格式顯示所有數據。我需要把它放到一個數組中供圖使用。 – Brad

+0

我明白了。我發佈了一個有希望幫助的答案。 –

回答

1

因爲你的數據包含標題行作爲第一行,你應該使用d3.csv.parse,而不是d3.csv.parseRowsCSV documentation解釋了這些差異。

解析的結果將是東西,看起來像這樣:

[ 
    {"names": "john", "data": 78}, 
    {"names": "brad", "data": 105}, 
    ... 
] 

所以,當你用這個數據來創建你的rect元素,你綁定到每個矩形的對象。然後當您使用selection.attrselection.style時,您傳遞的d值將是綁定的對象。這意味着你將需要引用你想要的財產,或者作爲d.namesd.data。文件中的每一列都是對象上的不同屬性(如上所示)。

要考慮的另一件事是可能用d3.csv代替d3.text來檢索文件並在一個步驟中解析數據。

+0

好的,我已將'd3.text'更改爲'd3.csv','d3.csv.parseRows'更改爲'd3.csv.parse',並且'data'更改爲'd.data'。所以爲什麼我得到這個錯誤...'未捕獲TypeError:對象[對象數組]沒有方法'charCodeAt'' .......只是猜測,但是,這是因爲我使用d3.v3 .mim。js不是d3.v3.js ?? – Brad

+0

您應該閱讀我引用的文檔鏈接。如果您使用d3.csv,則不需要也調用parseRows,因爲解析是自動進行的。回調通過可直接用來創建rects的對象數組。 –

+0

好吧,我現在看到。我試圖解析已經解析的數據......現在像魅力一樣工作,謝謝。 – Brad

相關問題