doutao6380 2018-12-04 08:41
浏览 51

条形图与同一变量的2个不同属性[Chart.Js]

Good day to everyone, I am trying to display a barchart that show the count of 2 different attribute with the name 'Feedback' VS 'Complain' and these two attribute are based on user selection.

I have followed some tutorial to create a Json file to same the data retrieve from mysql then to be display on the bar chart. So this is the data.php that receives the data based on query

   <?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);
?>

Then here is the script function I am facing a lot of challenge with. I am very new to Chart JS and PHP in general, if there's any security flaw please bear with me. THANK YOU FOR ANY KIND HELPERS

$(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);
        }
    });
});
  • 写回答

1条回答 默认 最新

  • du2229 2018-12-04 10:59
    关注

    you can try to push in feedback an object, for example :

        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);
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP