2

您好我是网页设计的新手,我的一些行话可能是错误的。我正在尝试为讲座创建一个图表,显示每个讲座获得的平均评分。我已经计算了 18 节课中每节课的平均值。现在我想获取每个值并将其作为值插入到图中。

以下是我查询和输出讲座的方式:

<?php
    ini_set('display_errors', 1);
    include ('connection.php');
    $queryLecture = "SELECT DISTINCT lectureID FROM LectureReview ORDER BY lectureID";
    $resultLecture = $mysqli->query($queryLecture);

    while ($rowLecture = $resultLecture->fetch_assoc()) {
        echo "<div class=\"row\">";
        echo "<div class=\"box\">Lecture{$rowLecture['lectureID']}:</div>";
        $lectureID = $rowLecture['lectureID'];

        $mysqli2 = new mysqli($hostname, $username, $password, $database);
        $stmt = $mysqli2->prepare("SELECT AVG( lectureReview ) AS lectureAvg FROM LectureReview WHERE lectureID = ?");

        $stmt->bind_param('i', $lectureID);
        $stmt->execute(); 
        $stmt->bind_result($lectureAvg); 
        $stmt->fetch();

        echo "<div class=\"box\">Average Lecture Rating: {$lectureAvg}</div>";
        echo "</div>";
    }
?>

然后,OnLoad我这样做:

var chart = new CanvasJS.Chart("chartContainer", {
    theme: "theme1",
    title:{
        text: "Average Lecture Score"              
    },
    animationEnabled: true,   
    data: [              
        {
            // Change type to "bar", "area", "spline", "pie",etc.
            type: "column",
            dataPoints: [
                { label: "Lecture 1", y: 4.5 },
                { label: "Lecture 2", y: 4.5 },
                { label: "Lecture 3", y: 5.0 },
                { label: "Lecture 4", y: 5.0 },
                { label: "Lecture 5", y: 5.0 },
                { label: "Lecture 6", y: 5.0 },
                { label: "Lecture 7", y: 5.0 },
                { label: "Lecture 8", y: 5.0 },
                { label: "Lecture 9", y: 5.0 },
                { label: "Lecture 10", y: 5.0 },
                { label: "Lecture 11", y: 5.0 },
                { label: "Lecture 12", y: 5.0 },
                { label: "Lecture 13", y: 5.0 },
                { label: "Lecture 14", y: 5.0 },
                { label: "Lecture 15", y: 5.0 },
                { label: "Lecture 16", y: 5.0 },
                { label: "Lecture 17", y: 5.0 },
                { label: "Lecture 18", y: 5.0 },
            ]
        }
    ]
});
chart.render();

我的目标容器:

<div id="chartContainer"
    style="height: 300px; width: 90%; position: absolute; padding-top:50px;">
</div>

目前它看起来像这样

如何将数据库中的值用于 CanvasJS 图形?

4

1 回答 1

0

我会这样做:

首先,在循环之外创建一个数组。while这将保存您的图表的总数据集。所以:

$dataset = array();

然后,您的while循环中,创建另一个数组。该数组将存储讲座名称及其平均评分。该数组将具有关联名称labely(在图表数据集中使用)。

$lectureRating = array();

在循环结束时,然后将$lectureIDand添加$lectureAvg到该数组中。然后将其添加到$dataset数组中。

$lectureRating['label'] = $lectureID;
$lectureRating['y'] = $lectureAvg;
array_push($dataset, $lectureRating);

在 while 循环完成后,$dataset数组应该具有与以下类似的结构:

Array[n] (
    [0] Array[2](
        ['label'] => "Lecture ID 1"
        ['y'] => 4.3
    )

    [1] Array[2](
        ['label'] => "Lecture ID 2"
        ['y'] => 3.7
    )

    // And so on
)

我们现在需要将其转换为 Javascript 对象 (JSON),以便 js 可以解释它。这是通过对其进行编码来完成的。

$graphData = json_encode($dataset);

这会将上面的数组变成这样的东西(看起来很熟悉?):

[{"label": "Lecture ID 1", "y": 4.3}, {"label": "Lecture ID 2", "y": 3.7}]

然后可以将其输出到dataPoints图表的对象中。

data: [
    {
        type: "column",
        dataPoints: <?php echo $graphData; ?>
    }
于 2016-01-15T16:56:55.593 回答