0

我创建了一个数据记录器,它每天都可以保存我,并在午夜将其名称更改为数据文件。每个文件都有当天的名称。我创建了以下代码,我从 .csv 文件生成了一个带有谷歌图表的图表

<!DOCTYPE html>
<head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->


 <meta name="HandheldFriendly" content="True">
 <meta name="MobileOptimized" content="320"> 
 <meta name="description" content="test">


 <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
 <script src="jquery.csv-0.71.min.js"></script>
 <script type="text/javascript" src="http://www.google.com/jsapi"></script>


<script type="text/javascript"> 
    
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);


function drawChart() {


   $.get("dati/today_log.csv", function(csvString) {
  
      var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});


      var data = new google.visualization.arrayToDataTable(arrayData);


      var view = new google.visualization.DataView(data);
      
      view.setColumns([12,0,1]);


      var options = {
        title: "Temperatura (°C)",
   
        legend: 'none'
     };


     var chart = new google.visualization.LineChart(document.getElementById("chart"));
     chart.draw(view, options);
  })
}


</script>


 </head>
 
  <body>
    <div id="chart" </div>
  </body>
</html>

我在 php 中找到了一个脚本,它在下拉列表中显示了一个目录中的所有文件......

<?php

$path = "dati/"; //change this if the script is in a different dir that the files you want
$show = array( '.csv' ); //Type of files to show


$select = "<select name=\"content\" id=\"content\">";


$dh = @opendir( $path );


while( false !== ( $file = readdir( $dh ) ) ){
    $ext=substr($file,-4,4);
        if(in_array( $ext, $show )){       
            $select .= "<option value='$path/$file'>$file</option>\n";
    }
}  


$select .= "</select>";
closedir( $dh );


echo "$select";
?>

我会从下拉列表中选择文件,按一个按钮创建图表并在图表输出中查看。

4

0 回答 0