Home forums Using CanvasJS x-axis date from php not working

This topic contains 2 replies, has 2 voices, and was last updated by  sona2017 3 weeks, 1 day ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14124

    sona2017
    Member

    Hi,

    I have the date in a CSV file, I am reading in php, I am not able to convert date (Ex: 05/01/2016) to a format readable by canvas JS. The Date function is PHP is not working. Can someone please help?

    <?php
    $dataPoints1 = $dataPoints2 = array();
    $csv = array_map(‘str_getcsv’, file(‘UUdata.csv’));
    for($i=1;$i<count($csv);$i++)
    {
    //$x_axis = date(‘m-d-Y’,strtotime($csv[$i][0]));
    //$x_axis = date(‘D M d Y H:i:s O’, strtotime($csv[$i][0]));

    $y_2016uu = bd_nice_number($csv[$i][4]);
    $y_2017uu = bd_nice_number($csv[$i][5]);
    array_push($dataPoints1, array(“x” => $x_axis, “y” => $y_2016uu));
    array_push($dataPoints2, array(“x” => $x_axis, “y” => $y_2017uu));
    }

    Here is the js code:

    $(function () {
    var chart = new CanvasJS.Chart(“chartContainer”, {
    theme: “theme2”,
    zoomEnabled: true,
    animationEnabled: true,
    title: {
    text: “Line Chart”
    },
    subtitles:[
    { text: “” }
    ],
    data: [
    {
    type: “line”,
    dataPoints: <?php echo json_encode($dataPoints1); ?>
    },
    {
    type: “line”,
    dataPoints: <?php echo json_encode($dataPoints2); ?>
    }
    ]
    });
    chart.render();
    });

    #14128

    Vishwas R
    Keymaster

    @sona2017,

    In JavaScript, Date instance is created using new Date().

    In your case, changing $x_axis = date(‘D M d Y H:i:s O’, strtotime($csv[$i][0])); to $x_axis = new Date(‘D M d Y H:i:s O’, strtotime($csv[$i][0])); should work fine.


    Vishwas R

    #14137

    sona2017
    Member

    Thank you! I forgot to add xValueType: “dateTime”.

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

You must be logged in to reply to this topic.