0

我是 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 服务器上托管该文件

4

2 回答 2

0

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

于 2012-12-27T12:17:24.053 回答
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
     },
于 2015-12-18T14:54:58.597 回答