同一变量有两个不同属性的条形图[chart.js]

py49o6xq  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(466)

大家好,我正在尝试显示一个条形图,其中显示了两个不同属性的计数,分别是“feedback”和“complain”,这两个属性是基于用户选择的。
我按照一些教程创建了一个json文件,将从mysql检索的数据显示在条形图上。这就是data.php,它接收基于查询的数据

<?php
//setting header to json
header('Content-Type: application/json');
$mysqli = mysqli_connect('localhost','root','','customercaremodule');

if(!$mysqli){
    die("Connection failed: " . $mysqli->error);
}

//query to get data from the table
$query1 = sprintf("SELECT FC_Category FROM fbcomplain where FC_Category ='Feedback'");
$Countsql1 = "SELECT count(FC_ID) AS total FROM fbcomplain WHERE FC_Category ='Feedback'";

//execute query
$result1 = $mysqli->query($query1);
$res1 = mysqli_query($mysqli,$Countsql1);
$value1 = mysqli_fetch_assoc($res1);
$feedbackrowcount = $value1['total'];
//loop through the returned data
$data1 = array();
foreach ($result1 as $row) {
    $data1[] = $row;
}

//free memory associated with result
$result1->close();

//query to get data from the table
$query2 = sprintf("SELECT FC_Category FROM fbcomplain where FC_Category ='Complain'");
$Countsql2 = "SELECT count(FC_ID) AS total FROM fbcomplain WHERE FC_Category ='Complain'";
//execute query
$result2 = $mysqli->query($query2);
$res2 = mysqli_query($mysqli,$Countsql2);
$value2 = mysqli_fetch_assoc($res2);
$complainrowcount = $value2['total'];

//loop through the returned data
$data2 = array();
foreach ($result2 as $row) {
    $data2[] = $row;
}

//free memory associated with result
$result2->close();

//close connection
$mysqli->close();

//now print the data
print json_encode($data1);
print json_encode($data2);
print json_encode($feedbackrowcount);
print json_encode($complainrowcount);
?>

下面是脚本函数,我面临着很多挑战。一般来说,我对chart js和php非常陌生,如果有任何安全漏洞,请接受我的建议。谢谢你的帮助

$(document).ready(function(){
    $.ajax({
        url: "http://localhost/customercare/data.php",
        method: "GET",
        success: function(data1) {
            console.log(data1);
            var feedback = [];
            var complain = [];

            for(var i in data1) {
                feedback.push(data1[i].$feedbackrowcount);
            }

            success: function(data2) {
                console.log(data2);
                var feedback = [];
                var complain = [];

                for(var i in data2) {
                    feedback.push(data2[i].$complainrowcount);
                }

            var chartdata = {

                datasets : [
                    {
                        label: 'Feedback',
                        backgroundColor: 'rgba(200, 200, 200, 0.75)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: feedback
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var barGraph = new Chart(ctx, {
                type: 'bar',
                data: chartdata
            });
        },
        error: function(data1) {
            console.log(data1);
        }
    });
});
xghobddn

xghobddn1#

您可以尝试将反馈推入对象,例如:

var bar_chart_obj_1 = {data: data1[i].$feedbackrowcount, label: "data1", id: 0};
feedback.push(bar_chart_obj_1);
    var bar_chart_obj_2 = {data: $data2[i].$complainrowcount, label: "data2", id: 1};
feedback.push(bar_chart_obj_2);

相关问题