从csv数据创建折线图

时间:2014-02-02 19:26:16

标签: javascript csv

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

虽然认为看起来不错,但它没有显示正确的图表。有人能告诉我这里有什么问题吗?

图代码:

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

1 个答案:

答案 0 :(得分:1)

问题在于放置非结构化的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>