简体   繁体   English

ChartJS 未从 MySQL 显示

[英]ChartJS not showing from MySQL

PHP: chart_db.php PHP:chart_db.php

<?php
require_once ('dbh.inc.php');

$JSON_Response = array();

//Counts the number of Active
$count_active = mysqli_query($db, "SELECT client_id FROM client WHERE status = 1");
$JSON_Response['active'] = mysqli_num_rows($count_active);

//Counts the number of Inactive
$count_inactive = mysqli_query($db, "SELECT client_id FROM client WHERE status = 0");
$JSON_Response['inactive'] = mysqli_num_rows($count_inactive);

error_log('hello');
echo json_encode($JSON_Response);

?>

JS: chart.js $(document).ready(function(){ JS: chart.js $(document).ready(function(){

    $.ajax({
        url:"http://localhost/FAME/private/includes/chart_db.php",
        method: "GET",
        success: function(response){

            var data = JSON.parse(response);
            var activeData = text(data.active);
            var inactiveData = text(data.inactive);
            console.log(activeData);

            var ctx = document.getElementById('piechart').getContext('2d');
            var statusChart = new Chart(ctx, {
                type: 'doughnut',
                data: {
                    labels: ['Active', 'Inactive'],
                    datasets: [{ 
                        pointStyle: 'circle',
                        backgroundColor: [ 'rgb(78, 115, 223)', 'rgb(25, 179, 211)' ],
                        data: activeData, inactiveData
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    segmentShowStroke: false,
                    cutoutPercentage: 70,
                    legend: {
                        onClick: false,
                        position: 'bottom',
                        labels: {
                            usePointStyle: true
                        }
                    }
                }
            });
        }
    });
});

Question: The problem is that in the data in chart are not showing.问题:问题是图表中的数据没有显示。 The entire chart is not showing.没有显示整个图表。 And with the log from chrome inspection: it says that there is an error: "Uncaught ReferenceError: text is not defined".并使用 chrome 检查的日志:它说有一个错误:“未捕获的 ReferenceError:未定义文本”。

This has nothing to do with php, mysql or xampp.这与 php、mysql 或 xampp 无关。 You are using an undefined method called text .您正在使用一个名为text的未定义方法。 The error message says it all.错误消息说明了一切。 Check your third and fourth lines in the ajax success.检查你在 ajax 中的第三和第四行是否成功。 You have:你有:

var activeData = text(data.active);

Replace it with:将其替换为:

var activeData = data.active;

and see what happens.看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM