Hi all.
I’m working in a small web application where I have to display some sensors data, static and dynamic
I’m using YII as php framework. for the static it’s ok I can display 5 series from different 5 sensors in the same chart with this code
(user should choose a rand of time and the data will be fetched from the corresponding mysql table)
YII controller
public function actionInfo()
{
if(isset($_POST['beamId']))
$beamId = $_POST['beamId'];
$modelBeam=Beam::model()->findBySql('SELECT * FROM poutre where poutre_id = "'.$beamId.'"');
if(count($modelBeam)<1)
throw new CHttpException(' : Beam not found !','Make sure that you entered a valid beam');
$modelResponsible=Responsible::model()->findBySql('SELECT * FROM responsable where responsable_id = "'.$modelBeam['responsable_id'].'"');
$modelHistory=BeamHistory::model()->findAllBySql('SELECT * FROM historique_poutre where poutre_id = "'.$beamId.'" ORDER BY historique_poutre_date DESC ');
$this->render('info',array('modelBeam'=>$modelBeam,'modelResponsible'=>$modelResponsible,'modelHistory'=>$modelHistory,));
}
public function actionGetSensorsData($beamId,$sDate,$eDate)
{
$result = array();
header('Content-Type: application/json; charset="UTF-8"');
// get all sensors
$sensorsIds=Sensor::model()->findAllBySql('SELECT capteur_id FROM capteur where poutre_id="'.$beamId.'";');
foreach ($sensorsIds as $s)
{
$sResult = array();
$modelSensors = SensorInfo::model()->findAllBySql('SELECT * FROM information_capteur where capteur_id= "'.$s['capteur_id'].'" and
"'.$sDate.'" <= information_capteur_temps and
information_capteur_temps <= "'.$eDate.'"
order by information_capteur_temps ASC;');
foreach($modelSensors as $res){
$sResult[] = array(($res['information_capteur_temps']),intval($res['information_capteur_valeur']));
}
$data= array('data'=>$sResult);
$name = array('name'=>$s['capteur_id']);
$result[]=array('name'=>$s['capteur_id'],'data'=>$sResult);
}
$json = CJSON::encode($result);
echo $json ;
Yii::app()->end();
}
and in my view info.php
$(document).ready(function(){
var startDateTextBox = $('#startDate');
var endDateTextBox = $('#endDate');
$("#btnSubmit").click(function() {
if(startDateTextBox.val()!="" && endDateTextBox.val()!=""){
$.ajax({
type: "get",
url: "<?php echo CController::createUrl('beam/GetSensorsData'); ?>",
data: {"beamId" : "<?php echo $modelBeam['poutre_id'] ;?>" , "sDate": $('#startDate').val() , "eDate": $('#endDate').val()},
dataType:"json",
success: function(response, status) {
var chart;
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'spline'
},
title: {
text: 'Snow depth in the Vikjafjellet mountain, Norway'
},
subtitle: {
text: 'An example of irregular time data in Highcharts JS'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
second: '%H:%M:%S',
minute: '%H:%M',
hour: '%H:%M',
// month: '%e. %b',
//year: '%b'
},
tickPixelInterval: 150,
maxZoom: 10 * 1,
},
yAxis: {
title: {
text: 'Snow depth (m)'
},
},
tooltip: {
formatter: function() {
return '<b>'+ 'Lead' + this.series.name +'</b><br/>'+
Highcharts.dateFormat('%H:%M:%S:%m',this.x) +'<br/>'+
Highcharts.numberFormat(this.y,0);
}
},
series: response
});
},
error: function (response, status) {
alert("An error occured while loading data!");
}
});
}
else
{
alert("Choose date range!")
}
});
//
////
for dynamic update using ajax call (from only one sensor ) I get always an error with this code
error here PHP Error [8] Undefined index: information_capteur_temps , information_capteur_temps is the column where I store the time
controller
public function actionLive()
{
if(isset($_POST['beamId']))
$beamId = $_POST['beamId'];
$modelBeam=Beam::model()->findBySql('SELECT * FROM poutre where poutre_id = "'.$beamId.'"');
if(count($modelBeam)<1)
throw new CHttpException(' : Beam not found !','Make sure that you entered a valid beam');
$modelSensors = SensorInfo::model()->findAllBySql('SELECT * FROM information_capteur where capteur_id= "I" order by information_capteur_temps ASC;' );
$this->render('live',array('modelSensors'=>$modelSensors ,'modelBeam'=>$modelBeam)) ;
}
public function actionGetSensorsDataLive($beamId,$sensId='I')
{
$result = array();
header('Content-Type: application/json; charset="UTF-8"');
$sResult = array();
$modelSensors = SensorInfo::model()->findAllBySql('SELECT * FROM information_capteur where capteur_id= "I" ');
$sResult[] = array(($modelSensors['information_capteur_temps']),intval($modelSensors['information_capteur_valeur'])); // error here PHP Error [8] Undefined index: information_capteur_temps
$data= array('data'=>$sResult);
//$name = array('name'=>$s['capteur_id']);
$result[]=array('data'=>$sResult);
$json = CJSON::encode($result);
echo $json ;
Yii::app()->end();
}
my view live.php
function requestData() {
$.ajax ({
type:"get" ,
url: "<?php echo CController::createUrl('beam/GetSensorsDataLive'); ?>",
data: {"beamId" : "<?php echo $modelBeam['poutre_id'] ;?>"},
dataType: "json",
success: function(response,point) {
var series = chart.series[0],
shift = series.data.length > 20;// shift if the series is longer than 20
chart.series[0].addPoint( eval(point),true, false); // add the point
setTimeout(requestData, 1000); // call it again after one second
},
cache: false
});
}
$(document).ready(function() {
var chart;
chart = new Highcharts.Chart({
chart: {
renderTo: 'graph',
type: 'spline',
marginRight: 70,
events: {
load: requestData
}
},
title: {
text: 'ID 1000000 sensors 0'
},
xAxis: {
type: 'datetime',
//tickPixelInterval: 100
tickPixelInterval: 150,
maxZoom: 10 * 1
},
yAxis: {
title: {
text: 'Peaks Values'
},
/* plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]; */
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%H:%M:%S:%m', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
series: [{
name: 'Live Data ',
data:[]
}]
});
});
Any help or comments would be appreciated
I’m pretty sure that is related to my db query but I couldn’t find it
many thanks in advance