1
1
123 1232014-10-17 00:04:14
PHP
123 123, 2014-10-17 00:04:14

How to return json data in HighCharts?

Good day to all. Help send data in JSON format to JS Highcharts. There are two selectable fields: Temperature and Humidity. According to them, a schedule should be built further. With one field everything worked as soon as two added everything died.
PHP:

/* SQL */
    /* Connect */
      try
      {
        $connection = new PDO("mysql:host=localhost;dbname=my","root","");
      }
      catch (PDOException $e)
      {
        echo 'Connection error: ' . $e->getMessage();
      }
    /* /Connect */
    /* Query */
      $query = $connection->prepare("SELECT temperature, humidity FROM weather WHERE date >= CURDATE()");
      $query->execute();
      $result = $query->fetchAll(PDO::FETCH_ASSOC);
      
      $my = array();
      if($query->rowCount() < 1)
      {
        echo "Не найдено";
      }
      else
      {
        foreach ($result as $row)
        {
          $my[] = $row;
        }
      }
      echo json_encode($my);
    /* /Query */
  /* /SQL */

JS:
$(function(){
  var options = {
        chart: {
            renderTo: 'mychart',
            type: 'spline'
        },
    title: {
            text: 'Мониторинг температуры и влажности за день'
        },
        xAxis: {
            categories: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
        },
        yAxis: {
            title: {
                text: 'Значение'
            }
        },
        series: [{}]
    };
  
  $.getJSON('../ajax/get_weather_day.php', function(data){
    options.series[0].name = "Температура";
    options.series[0].data = data.temperature;
    options.series[1].name = "Влажность";
    options.series[1].data = data.humidity;
        var chart = new Highcharts.Chart(options);
    });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2014-10-17
@mistergalynsky

I don't know what will happen if you have options.series[1] which is undefined by default - add some value.
Try to specify 2 empty objects in the series key when creating a chart.

yAxis: {
      title: {
           text: 'Значение'
      }
  },
 series: [{}, {}]

In general, my highchart charts just work with two categories, but I already made it a rule for myself to first get the data, and then immediately make a chart on it, and not make a chart, then, having received the data, redefine its properties.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question