动态创建一个图表,显示每个月的总数

时间:2019-08-05 16:22:02

标签: javascript php arrays chart.js

我试图显示使用chart.js每月生成的总报价,我该如何实现?

我创建了一个JSON数组,并且我正在使用ajax动态尝试并创建chart.js所需的数组。我能够创建一些数据,但是我想动态显示每月的总报价,如果月份(n)没有报价,则显示0。我很沮丧地试图弄清楚ro如何做到这一点。请告诉我我在做什么错。

我的javascript使用

向服务器请求信息

JavaScript

function displayQuotationsJSON() {
"use strict";
    $.ajax({
        url: "process.php",
        method: "POST",
        dataType: "JSON",
        data: {
            displayQuotationsJSON: 'true'
        },
        success: function (results) {
            if (results.success) {
                console.log(results);
                var ctx = document.getElementById("myChart").getContext('2d');

                var labels = results.quotation.map(function (e) {
                    return e.month;
                });
                var totalGenertePM = results.quotation.map(function (e) {
                    return e.num_quotations;
                });

                var chartData = {
                        labels: labels,
                        datasets: [
                            {
                                fillColor: "#79D1CF",
                                strokeColor: "#79D1CF",
                                data: totalGenertePM
                            }
                        ]
                };
}

然后PHP将执行请求并创建要返回的数组

PHP代码

public function displayQuotationSJSON() {
    $sql = "year";

    switch ($sql) {
    case "year":
        $sql = "SELECT  COUNT(orders.oid) AS num_quotations, 
                        MONTHNAME(orders.orderDate) AS month
                FROM KIT.orders
                WHERE MONTH(orders.orderDate) >= 0 
                AND MONTH(orders.orderDate) <= 12 
                AND orders.uid = ?
                GROUP BY { fn MONTHNAME(orders.OrderDate) }, 
                    MONTH(orders.OrderDate), YEAR(orders.OrderDate)
                ORDER BY Year(orders.orderDate),month(orders.OrderDate);";
        break;
    case "month":
        $sql = "SELECT orders.oid, orders.orderDate 
                FROM orders 
                WHERE orders.uid = ? 
                AND MONTH(orders.orderDate) = 4 
                AND YEAR(orders.orderDate) = 2019";
        break;
    case "green":
        $sql = "Your favorite color is green!";
        break;
    default:

    }

    if( !$this->stmt = $this->mysqli->prepare($sql) )
        throw new Exception("MySQL Prepare statement failed: ".$this->mysqli->error);

        $this->stmt->bind_param("i", $_SESSION[$this->sessionName]['userId']);
        $this->stmt->execute();
        $this->stmt->store_result();

        if( $this->stmt->num_rows == 0)
            return array();

        $this->stmt->bind_result($num_quotations, $month);

        $quotation = array();           
        $i = 0;
        $sum = 0;
        while( $this->stmt->fetch() )
        {   
            $quotation[$i]["num_quotations"] = $num_quotations;
            $quotation[$i]["month"] = $month;
            $i++;
        }
        $quotation[$i]["totalGenertePM"] = $i;

        return $quotation;
    }

0 个答案:

没有答案