19
Окт
2021

Не рисуется график

проблема в том, что не отображается график, запросы написаны верно и отлично работают, в консоли так же все чисто.

HTML

<button class="val" name='param' value="day" >День</button>
<button class="val" name='param' value="month" >Месяц</button>
<button class="val" name='param' value="minute" >10 Минут</button>
<div class="nav">


</div>
<div class="chart-container pie-chart">
<canvas id="pie_chart"></canvas>
</div>

Контроллер:

if($_POST['status']=='month'){

        $data = array();

        foreach($selectHashMonth as $row)
        {
            $data[] = array(
                'hash'      =>  $row["suminday"],
                'time'  => date('m', strtotime($row["time"])).'-'.date('d', strtotime($row["time"])),
            );
        }

        echo json_encode($data);
    }
    if($_POST['status']=='day'){


        $data = array();

        foreach($selectHashDay as $row)
        {
            $data[] = array(
                'hash'      =>  $row["suminhour"],
                'time'  => date('H', strtotime($row["time"])).":"."00",
            );
        }

        echo json_encode($data);
    }

Модель

public function selectHashMonth() {
    $params = [
        'month' => date("m"),
    ];
    return $this->db->row('SELECT time,sum(summary_mhs_5s) AS suminday FROM miners_data 
    WHERE MONTH(time) = :month GROUP BY DAY(time)', $params);
}

public function selectHashDay() {
    $params = [
        'month' => date("m"),
        'day' => date("d"),
    ];
    return $this->db->row('SELECT time, sum(summary_mhs_5s) AS suminhour FROM miners_data WHERE MONTH(time) = :month AND DAY(time) = :day GROUP BY HOUR(time)', $params);
}

JS

 $(document).ready(function() {
    var hash = [];
    var time = [];
    var status = '';
    makechart();
  

    $(".val").on('click',function() {
       
      switch($(this).val()){
          case 'day':
              status = 'day';
              
              break;
          case 'month':
            status = 'month';
           
              break;
          case 'minute':
              status = 'minute';
              break;
      }
      $.ajax({
          url: "/miners/index",
          method:'POST',
          data:{
              action:'insert',
          },
          success:function(data){
            hash =[];
            time =[];
             makechart();
          }
      })
      
    });

    function makechart() {

        $.ajax({
            url: "/miners/index",
            method: "POST",
            data: {
                action: 'fetch',
                status: status
            },
            dataType: "JSON",
            cache: true,
            async: true,
            success: function(data) {

                $('#pie_chart').html(data); 

                for (var count = 0; count < data.length; count++) {
                    hash.push(data[count].hash);
                    time.push(data[count].time);
                }
                console.log(hash);
                console.log(time);
                console.log(status1);
                function graphic(){
                var chart_data = {
                    labels: time,
                    datasets: [{
                        label:'HASH',
                        data: hash,
                        borderColor:'#876ED7',
                        backgroundColor:'#dae5f7',
                        pointRadius:5,
                        // pointHitRadius:5,
                        pointBackgroundColor:'rgb(0,0,0)'
                    }],
                    options:{ legend:{display:true} }
                }
                
                var group_chart1 = $('#pie_chart');
                var graph1 = new Chart(group_chart1, {
                    type: "line",
                    data: chart_data,
                    options: {
                        plugins: {
                            legend: {
                                labels: {
                                      usePointStyle: true,
                                    font: {
                                        size: 24
                                    }
                                },

                            }
                        }
                    }
                });
                
            
            }
            setTimeout(graphic, 300);

            }
        })
    }

});

Источник: https://ru.stackoverflow.com/questions/1339839/%D0%9D%D0%B5-%D1%80%D0%B8%D1%81%D1%83%D0%B5%D1%82%D1%81%D1%8F-%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D0%BA

Тебе может это понравится...

Добавить комментарий