<?
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";



$json = file_get_contents('php://input');

//get json array
//$jsonArr = json_decode($json,true);
//$email=$jsonArr['email'];
//$data=$jsonArr['data'];
//$data=json_encode($data);
//$graphname=$jsonArr['graph'];
//echo $data;
$data=[["2000,08,06",10,10],[ "2001,08,04",66,10],[ "2002,08,03",55,10 ],[ "2003,08,02", 18,10],[ "2004,08,01",20,10 ],[ "2005,08,11",17,10],[ "2020,08,05",14,10]]; 
if(!empty($data))
{
    error_log("\n myData:".print_r($data,true),3,"my-errors.log");
?>
<div id='chart_div' style="width: 100%; height: 100%;"></div>
  

<div>Core Chart</div>

<div id="linechart_core" style="width: 100%; height: 100%;"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


  <script type="text/javascript">
  	var myData = <?php echo(json_encode($data)); ?>;
   

      
     

        myData.forEach(function(element) {
        
        var d = element[0];

        
        
        element.splice(0,1);
        element.splice(0, 0,new Date(d) );

        });
//alert(myData);

    google.charts.load('current', {
    callback: function () {
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Dates');
    data.addColumn('number', 'Fuel Consumption');
    data.addColumn('number', 'Average Consumption');

    data.addRows(myData);

    var tickMarks = [];
    for (var i = 0; i < data.getNumberOfRows(); i++) {
      tickMarks.push(data.getValue(i, 0));
    }

    var options = {
      chart: {
        title: 'Fuel Buddy Charts',
        subtitle: 'fuel consumption chart'
      },
      chartArea: {
    'backgroundColor': {
        'fill':'#e5f4f9',
        'opacity': 50
     },
 },

      curveType: 'function',
      legend: { position:'none' },
      width: '100%',
      pointSize: 10,
      explorer: { actions: ['dragToZoom', 'rightClickToReset'] },
      hAxis: {
        format: 'MM/dd/yyyy',
        ticks: tickMarks
      },
      vAxis: {
        baseline: 0, 
        viewWindowMode: "explicit", 
        viewWindow:{ min: 0 },
        gridlines: { color: '#f3f3f3', count: 6} 
      }
      
    };

   // var chart = new google.charts.Line(document.getElementById('linechart_material'));
    //chart.draw(data, google.charts.Line.convertOptions(options));
    chart = new google.visualization.LineChart(document.getElementById('linechart_core'));
  chart.draw(data, options); 
    var myimagedata = chart.getImageURI();

     //var myimagedata = chart.toDataURL()
      var baseimagedata=myimagedata.replace(/^data:image\/(png|jpg);base64,/ ,"");
 $.ajax({
            type: "POST",
            url: "http://www.fuelbuddy-app.com/DatabaseScriptsTemp/sendtophp.php",
            data: {myData:baseimagedata},
            success: function(){console.log("sucess");},
            error: function(error){console.log("nooooo")}
             });
      //alert(baseimagedata);

  },
  packages: ['corechart', 'line']
});


  

     

  
  </script>
<p id="demo"></p>
</body>
</html>
<?
        
        $response["success"] = 1;
        $response["message"] = "Graph created successfully";
        // echoing JSON response
        echo json_encode($response);
}
else{
        // required field is missing
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";
        // echoing JSON response
        echo json_encode($response);
}
?>

  

