0

Im trying to get some database temperture values to plot inside a graph , But it seems the json format is not right for it to render ,(maybe something with the quotes?) but cant seem to fix the problem , any help would pointing in the right direction would be greatly appriciated .

Would love to see the date/time data plotted on the xasis as well

Here is my code html graph

   <!DOCTYPE HTML>

Groendak

   <script type="text/javascript"   
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
   <script type="text/javascript">

$(function () { var chart; $(document).ready(function() { $.getJSON("data3.php", function(json) {

       chart = new Highcharts.Chart({
           chart: {
              renderTo: 'container',  
             type: 'line',
               marginRight: 130,
               marginBottom: 25
           },
           title: {
               text: 'Sensor1 vs. Sensor2',
               x: -20 //center
           },
           subtitle: {
               text: '',
               x: -20
           },
           xAxis: {


   },
          yAxis: {
               title: {
                   text: 'Temperatuur'
               },
               plotLines: [{
                   value: 0,
                   width: 1,
                   color: '#808080'
               }]
           },
           tooltip: {
               formatter: function() {
                       return '<b>'+ this.series.name +'</b><br/>'+
                       this.x +': '+ this.y;
               }
           },
           legend: {
               layout: 'vertical',
               align: 'right',
               verticalAlign: 'top',
               x: -10,
               y: 100,
               borderWidth: 0
           },
           series: json
       });
   });

});

});

The data3.php part

$series3['data'][] = $r['sensor10']; $series4['data'][] = $r['time']; } $result = array(); array_push($result,$category); array_push($result,$series1); array_push($result,$series3); array_push($result,$series4); print json_encode($result, JSON_NUMERIC_CHECK); mysql_close($con); ?>

and the json data that i get from the data3.php

    [{"name":"sensor1","data":[" 245 "," 245 "," 286 "," 289 "," 292 "," 290 "," 289 "," 287 "," 288 "," 286 "," 281 "," 279 "," 280 "," 277 "," 272 "," 270 "," 270 "," 267 "," 265 "," 264 "," 263 "," 260 "," 258 "," 259 "," 258 "," 257 "," 255 "," 252 "," 251 "," 252 "," 248 "," 247 "," 245 "," 244 "," 241 "," 239 "," 241 "," 237 "," 238 "," 234 "," 235 "," 234 "," 232 "," 230 "," 229 "," 230 "," 228 "," 228 "," 228 "," 227 "," 225 "," 226 "," 224 "," 225 "," 224 "," 223 "," 220 "," 220 "," 222 "," 222 "," 218 "," 220 "," 220 "," 218 "," 219 "," 218 "," 217 "," 219 "," 218 "," 216 "," 218 "," 215 "," 217 "," 218 "," 214 "," 216 "," 214 "," 214 "," 217 "," 214 "," 215 "," 214 "," 216 "," 215 "," 214 "," 217 "," 215 "," 215 "," 215 "," 216 "," 218 "," 217 "," 218 "," 219 "," 219 "," 221 "," 220 "," 221 "," 224 "," 225 "," 224 "," 226 "," 225 "," 228 "," 229 "," 229 "," 231 "," 230 "," 230 "," 231 "," 232 "]},{"name":"sensor2","data":[" 313 "," 312 "," 291 "," 285 "," 277 "," 278 "," 273 "," 271 "," 266 "," 261 "," 261 "," 258 "," 251 "," 249 "," 251 "," 249 "," 245 "," 243 "," 243 "," 239 "," 237 "," 239 "," 238 "," 232 "," 230 "," 229 "," 229 "," 230 "," 229 "," 223 "," 225 "," 222 "," 220 "," 217 "," 219 "," 219 "," 212 "," 216 "," 210 "," 214 "," 209 "," 209 "," 209 "," 210 "," 209 "," 206 "," 206 "," 204 "," 202 "," 202 "," 204 "," 199 "," 200 "," 197 "," 195 "," 196 "," 199 "," 198 "," 193 "," 192 "," 197 "," 193 "," 192 "," 195 "," 192 "," 194 "," 195 "," 191 "," 191 "," 193 "," 191 "," 196 "," 192 "," 193 "," 198 "," 196 "," 200 "," 200 "," 199 "," 204 "," 204 "," 208 "," 206 "," 209 "," 214 "," 211 "," 218 "," 221 "," 224 "," 227 "," 228 "," 233 "," 236 "," 239 "," 244 "," 248 "," 252 "," 255 "," 255 "," 258 "," 266 "," 266 "," 273 "," 271 "," 273 "," 278 "," 279 "," 284 "," 288 "," 291 "," 291 "]},{"name":"sensor10","data":[" 240 "," 178 "," 187 "," 198 "," 201 "," 204 "," 203 "," 189 "," 186 "," 188 "," 191 "," 193 "," 195 "," 203 "," 198 "," 196 "," 196 "," 196 "," 195 "," 196 "," 196 "," 197 "," 196 "," 193 "," 190 "," 192 "," 194 "," 194 "," 194 "," 189 "," 183 "," 180 "," 180 "," 180 "," 180 "," 178 "," 177 "," 180 "," 182 "," 184 "," 184 "," 184 "," 184 "," 184 "," 183 "," 183 "," 182 "," 182 "," 180 "," 182 "," 181 "," 180 "," 180 "," 180 "," 180 "," 182 "," 182 "," 182 "," 181 "," 183 "," 183 "," 182 "," 182 "," 181 "," 181 "," 182 "," 183 "," 184 "," 185 "," 186 "," 186 "," 188 "," 191 "," 199 "," 202 "," 200 "," 202 "," 203 "," 209 "," 206 "," 210 "," 210 "," 206 "," 214 "," 224 "," 234 "," 235 "," 238 "," 232 "," 247 "," 276 "," 277 "," 255 "," 243 "," 242 "," 257 "," 242 "," 254 "," 258 "," 269 "," 264 "," 265 "," 254 "," 263 "," 278 "," 277 "," 259 "," 259 "," 266 "," 278 "," 284 "]},{"name":"time","data":[" 2014-07-10 19:26:52"," 2014-07-10 19:36:52"," 2014-07-10 19:46:52"," 2014-07-10 19:56:52"," 2014-07-10 20:06:52"," 2014-07-10 20:16:52"," 2014-07-10 20:26:52"," 2014-07-10 20:36:52"," 2014-07-10 20:46:52"," 2014-07-10 20:56:52"," 2014-07-10 21:06:52"," 2014-07-10 21:16:52"," 2014-07-10 21:26:52"," 2014-07-10 21:36:53"," 2014-07-10 21:46:53"," 2014-07-10 21:56:53"," 2014-07-10 22:06:53"," 2014-07-10 22:16:53"," 2014-07-10 22:26:53"," 2014-07-10 22:36:53"," 2014-07-10 22:46:53"," 2014-07-10 22:56:53"," 2014-07-10 23:06:53"," 2014-07-10 23:16:54"," 2014-07-10 23:26:54"," 2014-07-10 23:36:54"," 2014-07-10 23:46:54"," 2014-07-10 23:56:54"," 2014-07-11 00:06:54"," 2014-07-11 00:16:54"," 2014-07-11 00:26:54"," 2014-07-11 00:36:54"," 2014-07-11 00:46:54"," 2014-07-11 00:56:54"," 2014-07-11 01:06:54"," 2014-07-11 01:16:54"," 2014-07-11 01:26:54"," 2014-07-11 01:36:55"," 2014-07-11 01:46:55"," 2014-07-11 01:56:55"," 2014-07-11 02:06:55"," 2014-07-11 02:16:55"," 2014-07-11 02:26:55"," 2014-07-11 02:36:55"," 2014-07-11 02:46:55"," 2014-07-11 02:56:55"," 2014-07-11 03:06:55"," 2014-07-11 03:16:55"," 2014-07-11 03:26:56"," 2014-07-11 03:36:56"," 2014-07-11 03:46:56"," 2014-07-11 03:56:56"," 2014-07-11 04:06:56"," 2014-07-11 04:16:56"," 2014-07-11 04:26:56"," 2014-07-11 04:36:56"," 2014-07-11 04:46:56"," 2014-07-11 04:56:56"," 2014-07-11 05:06:56"," 2014-07-11 05:16:56"," 2014-07-11 05:26:56"," 2014-07-11 05:36:56"," 2014-07-11 05:46:56"," 2014-07-11 05:56:57"," 2014-07-11 06:06:57"," 2014-07-11 06:16:57"," 2014-07-11 06:26:57"," 2014-07-11 06:36:57"," 2014-07-11 06:46:57"," 2014-07-11 06:56:57"," 2014-07-11 07:06:57"," 2014-07-11 07:16:57"," 2014-07-11 07:26:57"," 2014-07-11 07:36:57"," 2014-07-11 07:46:57"," 2014-07-11 07:56:58"," 2014-07-11 08:06:58"," 2014-07-11 08:16:58"," 2014-07-11 08:26:58"," 2014-07-11 08:36:58"," 2014-07-11 08:46:58"," 2014-07-11 08:56:58"," 2014-07-11 09:06:58"," 2014-07-11 09:16:58"," 2014-07-11 09:26:58"," 2014-07-11 09:36:58"," 2014-07-11 09:46:58"," 2014-07-11 09:56:58"," 2014-07-11 10:06:58"," 2014-07-11 10:16:58"," 2014-07-11 10:26:59"," 2014-07-11 10:36:59"," 2014-07-11 10:46:59"," 2014-07-11 10:56:59"," 2014-07-11 11:06:59"," 2014-07-11 11:16:59"," 2014-07-11 11:26:59"," 2014-07-11 11:36:59"," 2014-07-11 11:46:59"," 2014-07-11 11:56:59"," 2014-07-11 12:07:00"," 2014-07-11 12:17:00"," 2014-07-11 12:27:00"," 2014-07-11 12:37:00"," 2014-07-11 12:47:00"," 2014-07-11 12:57:00"," 2014-07-11 13:07:00"," 2014-07-11 13:17:00"," 2014-07-11 13:27:00"," 2014-07-11 13:37:00"," 2014-07-11 13:47:00"]}]        

the parser

   <?php

$con = mysql_connect( 'localhost', 'root', 'xxxxx' );

if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db("groendak", $con);

$query = mysql_query("SELECT sensor1, sensor2, sensor10, time FROM opstelling1");

$category = array(); $category['name'] = 'sensor1';

$series1 = array(); $series1['name'] = 'sensor2';

$series3 = array(); $series3['name'] = 'Buiten Lucht';

$series4 = array(); $series4['name'] = 'time';

while($r = mysql_fetch_array($query)) { $category['data'][] = intval($r['sensor1']); $series1['data'][] = intval($r['sensor2']); $series3['data'][] = intval($r['sensor10']);
$series4['data'][] = 1000.0*strtotime($r['time']);
}

$result = array(); array_push($result,$category); array_push($result,$series1); array_push($result,$series3); array_push($result,$series4);

print json_encode($result, JSON_NUMERIC_CHECK);

mysql_close($con); ?>

42
  • To anwser my own question , the solution was to use intval to make the string into an int value with the folowing code $category['data'][] = intval( $r['sensor1']); this make the json return a [234, 234] format and not the ["234"," 234"] . making the line plot
    – Gringo
    Jul 13, 2014 at 15:26
  • In the json_encode() you can also set a JSON_NUMERIC_CHECK. Jul 14, 2014 at 9:58
  • yes i have done that with no result. the intval did the trick . but now im having trouble to get the date on the x-axis . Think it has to do with the way the data is formated from the data3.php --json Would love some help with that
    – Gringo
    Jul 14, 2014 at 12:05
  • So now, how your json looks like, is parsed correct? Jul 15, 2014 at 9:33
  • now it looks like this , [{"name":"sensor1","data":[245,245,286,289,292,290,289,287]}, {"name":"sensor2","data":[313,312,291,285,277,278,273,271]}, {"name":"time","data":[" 2014-07-10 19:26:52"," 2014-07-10 19:36:52"," 2014-07-10 19:46:52"]}] off course with allot more data inside , but in that format still have not found a way to put the time/date on the xas in the right format , something with converting the format i read somewhere but cant implement it . Thanks for your help
    – Gringo
    Jul 17, 2014 at 17:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.