Permalink
Browse files

using new AnnotatedTimeLine chart from Google Viz API

  • Loading branch information...
1 parent 4871dab commit b223d67529ef450b9ac22ee95dcdf4d279e360ad @ronanguilloux committed Jul 16, 2013
Showing with 44 additions and 16 deletions.
  1. +28 −10 src/TemperaturePi/Logger.php
  2. +1 −1 thermometer
  3. +15 −5 web/js/main.js
@@ -10,12 +10,14 @@ class Logger extends \SQlite3
private $sensor;
private $currentTemperature;
private $dbPath;
+ private $dataPath;
private $db;
public function __construct()
{
$this->sensor = new DS18B20();
$this->dbPath = __DIR__ . '/../../resources/sqlite/log.db';
+ $this->dataPath = __DIR__ . "/../../web/js/data.js";
$this->db = 'temperature';
}
@@ -25,8 +27,7 @@ public function fetchAll()
$this->open($this->dbPath);
$result = $this->query('SELECT datetime, celsius FROM temperature');
while ($row = $result->fetchArray()) {
- //$return[$row['datetime']] = $row['celsius'];
- $return[] = $row['celsius'];
+ $return[$row['datetime']] = $row['celsius'];
}
$this->close();
@@ -39,14 +40,30 @@ public function fetchAll()
*/
public function writeJsDatas()
{
- $jsFile = __DIR__ . "/../../web/js/data.js";
$datas = $this->fetchAll();
- $jsContent = '{ "table": [["Date", "Celsius"]';
- foreach ($datas as $index=>$celsius) {
- $jsContent .= "\n,[$index,$celsius]";
+ $jsContent = '{ "table": [';
+ $index = 0;
+ foreach ($datas as $date=>$celsius) {
+ $date = date_parse_from_format("Y-m-d H:i:s", $date);
+ $date = sprintf("%d,%d,%d,%d,%d,%d",
+ $date['year']
+ , $date['month']
+ , $date['day']
+ , $date['hour']
+ , $date['minute']
+ , $date['second']);
+ $date = sprintf("new Date(%s)", $date);
+ $jsContent .= "\n";
+ if(0 < $index) {
+ $jsContent .= ",";
+ }
+ $jsContent .= "[".'"' . $date . '"' . "," . str_replace(',','.',$celsius) . "]";
+ $index++;
}
$jsContent .= "]}";
- file_put_contents($jsFile, $jsContent);
+ if(false === file_put_contents($this->dataPath, $jsContent)){
+ throw new \Exception("can't write into " . $this->dataPath);
+ }
return $this;
}
@@ -62,10 +79,11 @@ public function persist($trace = false, $reset = false)
$this->exec('DROP TABLE temperature');
}
$this->exec('CREATE TABLE IF NOT EXISTS temperature (datetime DATETIME, celsius FLOAT)');
- $this->exec("INSERT INTO temperature (datetime, celsius) VALUES (datetime('NOW'), " . $this->currentTemperature . ")");
-
- //echo "\n" . date("d/m/Y H:i:s") . "|" . $this->currentTemperature;
+ $insert = sprintf("INSERT INTO temperature (datetime, celsius) VALUES (datetime('NOW', 'localtime'), %s)", str_replace(',', '.', $this->currentTemperature));
+ $result = $this->exec($insert);
$this->close();
+
+ return $this;
}
}
View
@@ -18,4 +18,4 @@ if(!file_exists('/sys/bus/w1/devices/28-000003ced8f4/w1_slave')) {
$log = new Logger;
$log->persist(true);
$log->writeJsDatas();
-$result = exec('sudo ./blinker 17 200000');
+//$result = exec('sudo ./blinker 17 200000');
View
@@ -1,5 +1,5 @@
if ( ! window.console ) console = { log: function(){} };
-google.load("visualization", "1", {packages:["corechart"]});
+google.load("visualization", "1", {packages:["annotatedtimeline"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
$.ajax({
@@ -12,16 +12,26 @@ function drawChart() {
success: function(data) {
$('#chart_div').empty();
var lastTemp = data.table[data.table.length -1][1] + ' celsius';
- console.log('last temp: ' + lastTemp);
if('undefined' != data) {
- var dataTable = google.visualization.arrayToDataTable(data.table);
+ for(row in data){
+ for(col in data[row]) {
+ data[row][col][0] = new Date(eval(data[row][col][0]));
+ }
+ }
+ var dataTable = new google.visualization.DataTable();
+ dataTable.addColumn('datetime', 'Date');
+ dataTable.addColumn('number', 'Celsius');
+ for(key in data.table) {
+ dataTable.addRow([new Date(data.table[key][0]), parseFloat(data.table[key][1])]);
+ }
var options = {
title: $('title').text() + ' | Last temp: ' + lastTemp
+ , displayAnnotations: true
};
- var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
+ var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
chart.draw(dataTable, options);
}
- setTimeout("drawChart();", 5000); // recursion (milliseconds)
+ //setTimeout("drawChart();", 5000); // recursion (milliseconds) for dev purpose only
}
});
}

0 comments on commit b223d67

Please sign in to comment.