【问题标题】:how do i take a generated value and insert into a graph?如何获取生成的值并插入图表?
【发布时间】:2016-01-15 15:58:10
【问题描述】:

您好,我是网页设计新手,我的一些行话可能有误。我正在尝试为讲座创建一个图表,显示每个讲座获得的平均评分。我已经计算了 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>

At the moment it looks like this

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

【问题讨论】:

    标签: javascript php canvasjs


    【解决方案1】:

    我会这样做:

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

    $dataset = array();
    

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

    $lectureRating = array();
    

    在循环结束时,将$lectureID$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; ?>
        }
    

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2012-05-18
      • 2021-06-09
      • 2016-07-10
      相关资源
      最近更新 更多