Home forums Using CanvasJS Creating Dynamic Graphs on a PHP page based off MySQL data

Tagged: , ,

This topic contains 5 replies, has 2 voices, and was last updated by  Indranil Deo 1 year, 5 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
  • #19847

    I looked at “How can I use PHP MySQL Dynamic data” Forum and attempted to get my graph to work but am having trouble even displaying a dynamic graph with data. Eventually I want to be able to sum my count value and have the dynamic graph tick every second with the new query sum(count) value. Before I get there, I was hoping to just get a dynamic graph to work with my MySQL table. Here is my PHP code:

    header(‘Content-Type: application/json’);

    $con = mysqli_connect(“″,”lindsay”,”McGelin123!”,”python”);

    // Check connection
    if (mysqli_connect_errno($con))
    echo “Failed to connect to DataBase: ” . mysqli_connect_error();
    $data_points = array();

    $result = mysqli_query($con, “SELECT count, timestamp FROM skelCount”);

    while($row = mysqli_fetch_array($result))
    $point = array(“label” => $row[‘timestamp’] , “y” => $row[‘count’]);

    array_push($data_points, $point);

    echo json_encode($data_points, JSON_NUMERIC_CHECK);


    And here is my HTML code:
    <!DOCTYPE html>
    <!– javascript –>
    <script type=”text/javascript” src=”js/jquery.canvasjs.min.js”></script>
    <script type=”text/javascript” src=”js/canvasjs.min.js”></script>

    <script type=”text/javascript”>
    $(document).ready(function () {

    $.getJSON(“canvas-json-return.php”, function (result) {

    var chart = new CanvasJS.Chart(“chartContainer”, {
    data: [
    dataPoints: result


    <div id=”chartContainer” style=”width: 800px; height: 380px;”></div>





    Please download the PHP sample from the download page which includes example for creating chart from database. In case it doesn’t help your out can you please create a sample project with a sample database and share it over Google-Drive or Onedrive so that we can understand your code better and help you out?

    Indranil Deo,
    Team CanvasJS



    I think you forgot to share the link to the sample project. The link shared is for the ReadMe file. Can you kindly share the link to the sample project, so that we can look into it and help you out!

    Indranil Deo,
    Team CanvasJS



    The query seems to have some issue. Comparing timestamp properly should work fine in your case, i.e. SELECT timestamp, count FROM skelCount WHERE timestamp >= TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 day)) should work fine in this scenario. Please take a look at this link for more info. Please take a look at the updated project. Below is the screenshot of the chart when its run in local-server.
    php charts with data from mysql db

    Indranil Deo
    Team CanvasJS

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.