我正在从csv数据创建折线图。图看起来像这样:


尽管看起来不错,但未显示正确的图形。有人可以告诉我这里有什么问题吗?

图形代码:

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script src="http://jquery-csv.googlecode.com/files/jquery.csv-0.71.js"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);

    function drawChart() {
       // grab the CSV
       $.get("Chart3-data1.csv", function(csvString) {
          // transform the CSV string into a 2-dimensional array
          var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
        alert(arrayData);
          // this new DataTable object holds all the data
          var data = new google.visualization.arrayToDataTable(arrayData);

          // this view can select a subset of the data at a time
          var view = new google.visualization.DataView(data);
          view.setColumns([0,1]);

         // set chart options
         var options = {
        title: "A Chart from a CSV!",
        legend: 'none'
         };

        var chart = new google.visualization.LineChart(document.getElementById('gauge'));
        chart.draw(data, options);
});
      }
    </script>
  </head>
  <body>
    <div id="gauge" style="width: 900px; height: 500px;"></div>
  </body>


CSV数据:

"Date,Term,Segment,Visitor"
"2014/01/01,2014/01/01 - 2014/01/31,All visitors,2"
"2014/01/01,2014/01/01 - 2014/01/31,New user,0"
"2014/01/01,2014/01/01 - 2014/01/31,Mobile traffic,0"
"2014/01/02,2014/01/01 - 2014/01/31,All visitors,7"
"2014/01/02,2014/01/01 - 2014/01/31,New user,1"
"2014/01/02,2014/01/01 - 2014/01/31,Mobile traffic,0"
"2014/01/03,2014/01/01 - 2014/01/31,All visitors,5"
"2014/01/03,2014/01/01 - 2014/01/31,New user,0"

最佳答案

问题在于放置非结构化的CSV数据。处理完CSV数据后,便可以显示所需的图形。

看到下面的代码:

<html>
        <head>
                <script type="text/javascript" src="https://www.google.com/jsapi"></script>
                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
                <script src="http://jquery-csv.googlecode.com/files/jquery.csv-0.71.js"></script>
                <script type="text/javascript">
                        google.load("visualization", "1", {packages:["corechart"]});
                        google.setOnLoadCallback(drawChart);
                        function drawChart()
                        {
                                $.get("Chart3-data1.csv", function(csvString)
                                {
                                        var transData = [['Date', 'All visitors', 'New user', 'Mobile traffic']];
                                        var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
                                        for (var i = 1; i < arrayData.length; i++)
                                        {
                                                //console.log(arrayData[i][0]);
                                                var fields = arrayData[i][0].split(',');
                                                console.log(fields);
                                                switch ( (i - 1) % 3)
                                                {
                                                        case 0:
                                                                var allVisitors = parseInt(fields[3]);
                                                                break;
                                                        case 1:
                                                                var newUser = parseInt(fields[3]);
                                                                break;
                                                        case 2:
                                                                var mobile = parseInt(fields[3]);
                                                                transData.push([ fields[0], allVisitors, newUser, mobile] );
                                                                break;
                                                }
                                        }
                                        var data = new google.visualization.arrayToDataTable(transData);
                                        var options =
                                        {
                                                title: 'Website traffic chart from CSV data3!'
                                        };
                                        var chart = new google.visualization.LineChart(document.getElementById('gauge'));
                                        chart.draw(data, options);
                                });
                        }
                </script>
        </head>
        <body>

09-20 09:47