Cannot load data from .CSV file to Amcharts

2019-09-11 19:35发布

问题:

I am new to javascript and am creating a piechart which loads data from a comma separated file data.txt but I cannot see anything in the browser when I open the html file.

data.txt consists the following data -

United States,9252
China,1882
Japan,1809
Germany,1322
United Kingdom,1122
India,984

My html code is-

<!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>

I have taken the code from http://blog.amcharts.com/2011/03/amcharts-javascript-tutorials-part-2.html and changed it according to piechart. Please tell me what am I doing wrong. I have placed data.txt in the same folder as the html file and have not hosted the file on a web server

回答1:

You're calling loadCSV to load data.csv, but you said your data is in data.txt.



回答2:

You should try to update your JS, Chrome throws this error :
"AmCharts.AmPieChart is not a functioncreateChart @ Pie Charts.html:75window.onload @ Pie Charts.html:15"
Try to Use DATALOADER :

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