无法将.CSV文件中的数据加载到Amcharts

时间:2012-12-27 12:11:11

标签: javascript csv charts

我是javascript的新手,我正在创建一个从逗号分隔文件data.txt加载数据的饼图,但是当我打开html文件时,我在浏览器中看不到任何内容。

data.txt包含以下数据 -

美国,9252
中国,1882年
日本,1809
德国,1322
英国,1122年 印度,984

我的HTML代码是 -

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>amCharts examples</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <script src="amcharts/amcharts.js" type="text/javascript"></script>         
    <script type="text/javascript">
        var chart;
        var dataProvider;

        window.onload = function() {
        createChart();            
        loadCSV("data.csv");                                    
    }

    // method which loads external data
    function loadCSV(file) {
        if (window.XMLHttpRequest) {
            // IE7+, Firefox, Chrome, Opera, Safari
            var request = new XMLHttpRequest();
        }
        else {
            // code for IE6, IE5
            var request = new ActiveXObject('Microsoft.XMLHTTP');
        }
        // load
        request.open('GET', file, false);
        request.send();
        parseCSV(request.responseText);
    }

    // method which parses csv data
    function parseCSV(data){ 
        //replace UNIX new lines
        data = data.replace (/\r\n/g, "\n");
        //replace MAC new lines
        data = data.replace (/\r/g, "\n");
        //split into rows
        var rows = data.split("\n");
        // create array which will hold our data:
        dataProvider = [];

        // loop through all rows
        for (var i = 0; i < rows.length; i++){
            // this line helps to skip empty rows
            if (rows[i]) {                    
                // our columns are separated by comma
                var column = rows[i].split(",");  

                // column is array now 
                // first item is date
                var country = column[0];
                // second item is value of the second column
                var visits = column[1];
                // third item is value of the fird column 


                // create object which contains all these items:
                var dataObject = {country:country, visits:visits};
                // add object to dataProvider array
                dataProvider.push(dataObject);
            }
        }
        // set data provider to the chart
        chart.dataProvider = dataProvider;
        // this will force chart to rebuild using new data            
        chart.validateData();
    }


        function createChart(){   // PIE CHART
            chart = new AmCharts.AmPieChart();

            // title of the chart
            chart.addTitle("Visitors countries", 16);


            chart.titleField = "country";
            chart.valueField = "visits";
            chart.sequencedAnimation = true;
            chart.startEffect = "elastic";
            chart.innerRadius = "30%";
            chart.startDuration = 2;
            chart.labelRadius = 15;
            chart.pullOutEffect = "elastic";
            chart.pullOutDuration = 1;


            // the following two lines makes the chart 3D
            chart.depth3D = 10;
            chart.angle = 15;

            // WRITE                                 
            chart.write("chartdiv");
        }
    </script>
</head>

<body>
    <div id="chartdiv" style="width:600px; height:400px;"></div>
</body>

我从http://blog.amcharts.com/2011/03/amcharts-javascript-tutorials-part-2.html获取了代码并根据饼图更改了它。 请告诉我我做错了什么。 我已将data.txt放在与html文件相同的文件夹中,并且未在Web服务器上托管该文件

2 个答案:

答案 0 :(得分:0)

您正在调用loadCSV来加载data.csv,但您说您的数据位于data.txt中。

答案 1 :(得分:0)

您应该尝试更新您的JS,Chrome会抛出此错误:
"AmCharts.AmPieChart is not a functioncreateChart @ Pie Charts.html:75window.onload @ Pie Charts.html:15"
尝试使用DATALOADER:

AmCharts.makeChart("chartdiv", {
      "type": "pie",
      "dataLoader": {
        "url": "data/pie.csv",
        "format": "csv",
        "delimiter": ",",
        "useColumnNames": true
     },