0

首先,提前感谢您的帮助。几天来,我一直在努力弄清楚如何将角度图对象挂接到 mysql 数据库中。我目前有使用静态数据的图表。我的项目中有一个通用的 php 文件,看起来像这样:

// Create the connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

//the sql to run
$sql = "SELECT * FROM Test2";
//run it
$result = $conn->query($sql);

//check for results
if ($result->num_rows > 0) {
//create an array
$dataarray[] = array();

//loop em
while($row =mysqli_fetch_assoc($result)) {
    $dataarray[] = $row;
}
//make em json
echo json_encode($dataarray);
}
//} else {
//    echo "0 results";
//}

//close out our connection
$conn->close();
?> 

这工作正常。此表返回几行,其中包含标题名称和值。目前只有测试数据。

在我的 html/javascript 中,我有以下内容:

<script>
        angular.module("ChartApp", ["chart.js"]).controller("PieCtrl", function ($scope, $http) {
          $http.get('./GetTestData.php').
            success(function(data) {
                $scope.records = data;

            });

          $scope.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales"];
          $scope.data = [300, 500, 100];
        });
    </script>

我不确定从这里去哪里。任何帮助,将不胜感激。

4

2 回答 2

0

我收到了该代码的以下错误:

未捕获的类型错误:无法读取未定义的属性“绘制”

所以我必须插入[并使其工作]$scope.data

$scope.labels = names;
$scope.data = [values];
于 2015-11-05T03:13:04.127 回答
0

花了很长时间,但我终于想通了。我必须获取 JSON 对象并将值转换为单独的数组。以下是相关代码:

angular.module("ChartApp", ["chart.js"]).controller("PieCtrl", function ($scope, $http) {
 $http.get('./GetTestData.php').
success(function(data) {
    var names = [];

    for(var i = 0; i < data.length; i++) {
        var obj = data[i];

        if(obj.Name != undefined) {
            names.push(obj.Name);
        }
    }

    var values = [];
    for(var i = 0; i < data.length; i++) {
        var obj = data[i];

        if(obj.Name != undefined) {
            values.push(obj.Value);
        }
    }
    //$scope.records = data;
    $scope.labels = names;
    $scope.data = values;
    });

        });
    </script>
于 2015-09-11T00:23:55.800 回答