1

Good day. Help send the data in JSON format in JS Highcharts. There are two fields that are selected: Temperature and Humidity. On them should be based on the schedule. With one field everything worked as the only two added all died.

PHP:

<?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);

        echo json_encode($result, JSON_NUMERIC_CHECK);

    /* /Query */
/* /SQL */
?>

JS:

$(function(){
var options = {
    chart: {
        renderTo: 'mychart',
        type: 'spline'
    },
    title: {
        text: 'Temperature'
    },
    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: 'Values'
        }
    },
    series: [{}]
};

$.getJSON('../ajax/get_weather_day.php', function(data){
    options.series[0].name = "Temperature";
    options.series[0].data = data;
    options.series[1].name = "Humidity";
    options.series[1].data = data;
    var chart = new Highcharts.Chart(options);
});
});

1 Answer 1

2

You are assigning the result of both fields of your mysql table to both series.

options.series[0].data = data;
options.series[1].data = data;

You should split data and assign the right values to the right series (or you can make two ajax calls to get both seperately - check what's less expensive in your case).

By the way, data now holds the mysql result rows (value pairs) but highcharts expectes the values as numerical sequence.

See here:

http://api.highcharts.com/highcharts#series

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.