darksantos

GGGG

Jan 19th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. public function actionGrafico(){
  2.  
  3.         $pacientes = Paciente::model()->findAll();
  4.  
  5.         $listaPacientes = array();
  6.  
  7.         //print_r($pacientes);die();
  8.  
  9.         foreach ($pacientes as $key => $value) {
  10.             $model = $value;//->attributes;
  11.             $nombreCompletoPaciente = $model->nombrePaciente." ".$model->apellidoPaciente;
  12.             $listaPacientes[$model->idpaciente] = $nombreCompletoPaciente;
  13.  
  14.         }
  15.  
  16.         $this->render('grafico',array(
  17.             'listaPacientes'=>$listaPacientes,
  18.             ));
  19.  
  20.     }
  21.  
  22.  
  23. <div id="wrapPrincipal">
  24.     <div id="formUsuario">
  25.         <?php
  26.             $model = new Paciente;
  27.             echo CHtml::label('Paciente',"Paciente");
  28.             echo CHtml::dropDownList('Paciente[idPaciente]',"",$listaPacientes,array(
  29.                     'id'=>'selectPaciente'
  30.                 ));
  31.         ?>
  32.     </div>
  33.  
  34.     <br><br>
  35.     <br><br><br>
  36.  
  37.     <div id="wrapGrafico">
  38.         <canvas id="grafico" class=""></canvas>
  39.     </div>
  40. </div>
  41.  
  42. <?php
  43.     Yii::app()->getClientScript()->registerScriptFile(Yii::app()->theme->baseUrl.'/js/Chart.bundle.js');
  44. ?>
  45.  
  46. <script>
  47.     var xdata=["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio","Julio","Agosto","Septiembre","Octubre"];
  48.     var ydata=[3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000];
  49.     var grafico;
  50.  
  51.  
  52.     $(document).ready(function(){
  53.         var ctx = $("#grafico");
  54.  
  55.          grafico = new Chart(ctx,{
  56.  
  57.             type: 'bar',
  58.             data: {
  59.                 labels: xdata,
  60.                 datasets:[{
  61.                     label: "Utilidad",
  62.                     data: ydata,
  63.                     borderWidth: 2,
  64.                     borderColor: "rgba(75,192,192,1)",
  65.                     fill: false,
  66.                     borderWidth: 4,
  67.  
  68.                     }
  69.  
  70.                 ]
  71.             },
  72.             options: {
  73.                 //responsive: true,
  74.                 maintainAspectRatio: true,
  75.                 scaleShowVerticalLines: false,
  76.                 scaleShowHorizontalLines: false,
  77.                 scaleStartValue : 0,
  78.                 scales: {
  79.                     xAxes : [ {
  80.                     gridLines : {
  81.                         display : false
  82.                     }
  83.                 } ],
  84.                     yAxes: [{
  85.                        
  86.                         gridLines : {
  87.                             display : false
  88.                         }, 
  89.                         ticks: {   
  90.                             suggestedMin: 20,
  91.                             callback: function(value, index, values) {
  92.                             return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
  93.                         },                  
  94.                         }
  95.                     }]
  96.                 }
  97.             },
  98.         });
  99.  
  100.         dibujarGrafico();
  101.  
  102.         setInterval(function(){
  103.             dibujarGrafico();
  104.         },1000)
  105.  
  106.     });
  107.  
  108.     function reverse(array){
  109.         var new_array = [];
  110.         for(var i = 0; i< array.length; i++){
  111.             new_array[i] = array[array.length -i - 1];
  112.         }
  113.         return new_array;
  114.     }
  115.  
  116.     var idPaciente;
  117.  
  118.     function dibujarGrafico(){
  119.  
  120.         idPaciente = $('#selectPaciente').val();
  121.         console.log(idPaciente)
  122.         $.ajax({
  123.             type: 'GET',
  124.             url: '<?php echo Yii::app()->createUrl('site/tempsPaciente');?>',
  125.             data: 'idPaciente='+idPaciente,
  126.             success: function(data){
  127.                 var json = JSON.parse(data);
  128.                 if ( json["estado"] == 1){
  129.  
  130.                     var valores = json["valores"];
  131.                     xdata=[];
  132.                     ydata=[];
  133.                     for (var i = 0; i < valores.length; i++) {
  134.                         ydata.push(Number(valores[i]["valor"])*1);
  135.                         xdata.push(valores[i]["fecha"]);
  136.                         //console.log("bitch"+valores[i]["valor"]);
  137.                         //console.log("bitch"+valores[i]["fecha"]);
  138.                     }
  139.                     console.log(ydata);
  140.                     console.log(xdata);
  141.                     console.log("FUCKKKK");
  142.                     //console.log(valores);
  143.                     grafico.data.datasets[0].data = reverse(ydata);
  144.                     grafico.data.labels = reverse(xdata);
  145.                     grafico.update();
  146.                 }else{
  147.                     var canvas = document.getElementById("grafico");
  148.                     canvasMensajeVacio(canvas);
  149.                    
  150.                 }
  151.                
  152.             },
  153.  
  154.         });
  155.     }
  156.  
  157.     function limpiarCanvas(canvas){
  158.         var contexto = canvas.getContext("2d");
  159.         contexto.clearRect(0, 0, canvas.width, canvas.height);
  160.     }
  161.  
  162.     function canvasMensajeVacio(canvas){
  163.         limpiarCanvas(canvas);
  164.         var contexto = canvas.getContext("2d");
  165.         contexto.font = "30px Arial";
  166.         contexto.fillText("JSON ESTADO 0",10,50);
  167.     }
  168. </script>
  169.  
  170.  
  171.  
  172.  
  173.  
  174. //////
  175.  
  176.  
  177.  
  178.  
  179. public function actionTempsPaciente(){
  180.  
  181.        
  182.  
  183.         $idPaciente = $_GET["idPaciente"];
  184.         $idSensor = 1; //temp
  185.  
  186.         $criteria = new CDbCriteria();
  187.         $criteria->addCondition("paciente_idpaciente =:idpaciente");
  188.         $criteria->addCondition("sensor_idSensor =:idSensor");
  189.         $criteria->params = array(
  190.             ':idpaciente' => $idPaciente,
  191.             ':idSensor'=>$idSensor);
  192.         $criteria->limit = 10;
  193.         $criteria->order = "fecha DESC";
  194.  
  195.         $Valores = Valores::model()->findAll($criteria);
  196.  
  197.         $json = array();
  198.         $valoresJson = array();
  199.  
  200.  
  201.         if ($Valores){
  202.             foreach ($Valores as $key => $value) {
  203.                 $valoresJson[] = $value->attributes;
  204.             }
  205.  
  206.             $json["estado"] = 1;
  207.             $json["mensaje"] = "OK";
  208.             $json["valores"] = $valoresJson;
  209.  
  210.         }else{
  211.             $json["estado"] = 0;
  212.             $json["mensaje"] = "No hay valores";
  213.         }
  214.  
  215.         echo json_encode($json);
  216.  
  217.     }
Advertisement
Add Comment
Please, Sign In to add comment