Data for chart.js From database

1

I just start with charjs and I need to print values from a database, at the moment I'm doing the exercise with numbers placed directly to the code, which does not work for me. AJAX:

  $.ajax({
        data: { "cc":3},
        type: "POST",
        url: "qgetdata.php",

        success: function (data)
        {
            var ctx = document.getElementById("myChart").getContext("2d");

             var  lineChartData = data;
             //alert(JSON.stringify(data));
             var myLineChart = new Chart(ctx, {

               type: 'line',
               data: {
                labels: ['lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'Sabado', 'domingo'],
                datasets: [
                {
                 lineChartData
                }]
              }, options :{

              }
            });
        }
      });

PHP where I have the data that I want to draw on the graph:

$arrDatasets = array('label' => "elemento1",'fill' => "false", 'lineTension' => "0.1", 'pointBorderColor' => "rgba(75,192,192,1)", 'pointBorderWidth' => "3", 'pointHoverRadius' => "5", 'pointHoverBackgroundColor' => "rgba(220,220,220,1)", 'backgroundColor'=>"rgba(75,192,192,0.4)" , 'borderColor'=> "rgba(75,192,192,1)", 'spanGaps'=>"true", 'data' => array('28', '48', '40', '19', '86', '27', '90'));

echo (json_encode($arrDatasets));
    
asked by Andress Blend 26.12.2016 в 15:42
source

1 answer

1

After examining for a while I was able to make it work, I leave the solution in case someone else is useful:

 $.ajax({
        data: { "cc":3},
        type: "POST",
        dataType: "json",
        url: "qgetdata.php",

        success: function (dataR)
        {
            var ctx = document.getElementById("myChart").getContext("2d");


            //alert(JSON.stringify(data));
            var myLineChart = new Chart(ctx, {

               type: 'line',
               data: {
                labels: ['lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'Sabado', 'domingo'],
                datasets: [dataR] /// se debía ´pintar sin las llaves 
                 }, options :{

              }
            });
        }
      });
    
answered by 26.12.2016 в 16:55