2013-08-20 36 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

相关问题