chart.js、php 和 infomix 数据库有问题?

I have a issue with chart.js, php and infomix database?

你好 chart.js、php 和 informix 数据库有问题。

首先我用简单的 mysql 数据库测试脚本,它们运行良好。

但是当我在 pdo 中使用 informix 数据库时,他在未定义的图表标签上显示给我。

=> 我的 data.php(用于连接到数据库):

<?php
header('Content-Type: application/json');


$db=new pdo("informix:host=uccxpub;service=1504;database=db_cra;server=uccxpub_uccx;protocol=onsoctcp;EnableScrollabelCursors=1;client_local=en_US.UTF8;db_locale=en_US.UTF8","uccxwallboard","cisco2016"
) or die("Impossible de se connecter !!!");


$sql0 = sprintf("SELECT * FROM  RtCSQsSummary where CSQName = 'BMCE BANK'"); 

/*$stmt = $db->prepare($sql0);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
*/

$result = $db->query($sql0);
//loop through the returned data
$data = array();
foreach ($result as $row) {
    $data[] = $row;
}

//now print the data


print json_encode($data);
?>

=> data.php 的输出:

enter image description here

=> 我的 bargraph.js :

$(document).ready(function(){
    $.ajax({
        url: "../data.php",
        method: "GET",


        success: function(data) {

            console.log(data);
            var startdatetime = [];
            var loggedInAgents = [];

            for(var i in data) {
                startdatetime.push("startdatetime " + data[i].startdatetime);
                loggedInAgents.push(data[i].loggedInAgents);
            }


            var chartdata  {
                labels: startdatetime,
                datasets : [
                    {
                        label: 'loggedInAgents',
                        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: loggedInAgents,
                    }
                ]
            };


            var ctx = $("#mycanvas");

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

=> 输出:

enter image description here => 我的 index.html :

<!DOCTYPE html>
<html>
    <head>
        <title>ChartJS - BarGraph</title>
        <style type="text/css">
            #chart-container {
                width: 640px;
                height: auto;
            }
        </style>
    </head>
    <body>
        <div id="chart-container">
            <canvas id="mycanvas"></canvas>
        </div>

        <!-- javascript -->
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/Chart.min.js"></script>
        <script type="text/javascript" src="js/bargraph.js"></script>
    </body>
</html>

!!条形图没有显示 callshandled undefinded ..

使用 mysql 数据库进行测试:

=> data.php :

<?php
//setting header to json
header('Content-Type: application/json');

//database
define('DB_HOST', '127.0.0.1');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root123');
define('DB_NAME', 'mydb');

//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

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

//query to get data from the table
$query = sprintf("SELECT playerid, score FROM score ORDER BY playerid");

//execute query
$result = $mysqli->query($query);

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

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

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

//now print the data
print json_encode($data);

=>bargraph.js :

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

            for(var i in data) {
                player.push("Player " + data[i].playerid);
                score.push(data[i].score);
            }

            var chartdata = {
                labels: player,
                datasets : [
                    {
                        label: 'Player Score',
                        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: score
                    }
                ]
            };

            var ctx = $("#mycanvas");

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

=> index.html :

<!DOCTYPE html>
<html>
    <head>
        <title>ChartJS - BarGraph</title>
        <style type="text/css">
            #chart-container {
                width: 640px;
                height: auto;
            }
        </style>
    </head>
    <body>
        <div id="chart-container">
            <canvas id="mycanvas"></canvas>
        </div>

        <!-- javascript -->
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/Chart.min.js"></script>
        <script type="text/javascript" src="js/bargraph.js"></script>
    </body>
</html>

他工作得很好。

我 运行 你的代码(有一些小的修正)在我的 Linux 盒子里。我可以用来自 Informix 数据库的一些简单测试数据显示条形图:

html 页数:

informix@irk:/var/www/html# cat test.html
<!DOCTYPE html>
<html>
    <head>
        <title>ChartJS - BarGraph</title>
        <style type="text/css">
            #chart-container {
                width: 640px;
                height: auto;
            }
        </style>
    </head>
    <body>
        <div id="chart-container">
            <canvas id="mycanvas"></canvas>
        </div>

        <!-- javascript -->
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
        <script src="http://irk/b.js"></script>
    </body>
</html>
informix@irk:/var/www/html# 

javascript:

informix@irk:/var/www/html# cat b.js
$(document).ready(function(){
    $.ajax({
        url: "http://irk/data.php",
        method: "GET",

        success: function(data) {

    var obj=JSON.parse(data);
    data=obj;
            var startdatetime = [];
            var loggedInAgents = [];

            for(var i in data) {
                startdatetime.push( data[i].STARTDATETIME);
                loggedInAgents.push(data[i].LOGGEDINAGENTS);
            }

            var chartdata = {
                labels: startdatetime,
                datasets : [
                    {
                        label: 'loggedInAgents',
                        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: loggedInAgents 
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var barGraph = new Chart(ctx, {
                type: 'bar',
                data: chartdata
            });
        },
        error: function(data) {
            console.log(data);
        }
    });
});
informix@irk:/var/www/html# 

php 从 Informix 获取数据的脚本:

informix@irk:/var/www/html# cat data.php
<?php

$db=new pdo("informix:database=enusutf8;server=irk1210;CLIENT_LOCALE=en_US.UTF8;DB_LOCALE=en_US.UTF8","informix","testpwd") or die("Impossible de se connecter !!!");

$sql0 = sprintf("SELECT * FROM  RtCSQsSummary where CSQName = 'BMCE BANK'"); 

$result = $db->query($sql0);
$data = array();
foreach ($result as $row) {
    $data[] = $row;
}

print json_encode($data);
?>

informix@irk:/var/www/html# 

除了前面提到的uppercase/lowercase之外,JS函数得到的JSON数据 必须先解析才能使用。否则你将在 'for(var i in data)' 循环中得到 "undefied" 值,无论你使用什么情况。