美文网首页
使用D3.js读取csv内容

使用D3.js读取csv内容

作者: 0a1d1c9 | 来源:发表于2017-11-02 16:47 被阅读0次

csv文件(399905.XSHE.PEP.csv)预览:
399905.XSHE.PEP.csv

day,close,pe,pep,p20,p50,p80
2007-01-15,nan,46.59,0.00,46.59,46.59,46.59
2007-01-16,1986.53,47.81,50.00,46.83,47.20,47.57
2007-01-17,2055.01,47.30,33.33,46.87,47.30,47.61
2007-01-18,2035.65,49.31,75.00,47.02,47.55,48.41
2007-01-19,2085.40,51.01,80.00,47.16,47.81,49.65
2007-01-22,2159.63,52.88,83.33,47.30,48.56,51.01
2007-01-23,2258.98,52.63,71.43,47.40,49.31,52.31
2007-01-24,2261.78,53.23,87.50,47.50,50.16,52.78
2007-01-25,2283.69,50.72,44.44,47.61,50.72,52.73
2007-01-26,2174.23,51.02,60.00,47.71,50.86,52.68
2007-01-29,2186.27,52.39,63.64,47.81,51.01,52.63
2007-01-30,2260.59,52.34,58.33,48.11,51.02,52.58
2007-01-31,2255.93,49.75,30.77,48.41,51.01,52.53
2007-02-01,2142.89,50.96,42.86,48.71,50.98,52.49

html文件,ps:chrome现在貌似不支持file://协议,得用http://x.x.x.x/demo.html浏览
demo.html

<script src="https://d3js.org/d3.v4.min.js"></script>

<script>
//获取csv对象
d3.csv("399905.XSHE.PEP.csv",function(error,csvdata){  

    if(error){  
        console.log(error);  
    }  
    console.log(csvdata);  
    
    //输出csv内容
    for( var i=0; i<csvdata.length; i++ ){  
        var day = csvdata[i].day;  
        var close = csvdata[i].close;  
        var pe = csvdata[i].pe;  
        var pep = csvdata[i].pep;  
        var p20 = csvdata[i].p20;  
        var p50 = csvdata[i].p50;  
        var p80 = csvdata[i].p80;  
        document.write(
            "name:" + day + "," +  
            "close:" + close + "," +
            "pe:" + pe + "," +  
            "pep:" + pep + "," +  
            "p20:" + p20 + "," +  
            "p50:" + p50 + "," +  
            "p80:" + p80 +"<br />" 
            );  
    }
});  
</script>

<!--
//将csv数据转换为字符串
<script>
    d3.csv("399905.XSHE.PEP.csv",function(error,csvdata){       
        var str = d3.csv.format( csvdata );           
        console.log(str.length);  
        console.log(str);     
    }); 
</script>
-->

参考:

https://github.com/d3/d3/wiki/CSV

http://blog.csdn.net/lzhlzz/article/details/39755649

相关文章

网友评论

      本文标题:使用D3.js读取csv内容

      本文链接:https://www.haomeiwen.com/subject/mdcapxtx.html