当前位置: 首页 > 面试题库 >

Chart.js-使用mysql和php从数据库获取数据

艾焱
2023-03-14
问题内容

我正在尝试将静态数据转换为使用数据库结果。我将使用 MySQLPHP

示例代码:

var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var lineChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            label: "My First dataset",
            fillColor : "rgba(220,220,220,0.2)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeColor : "#fff",
            pointHighlightFill : "#fff",
            pointHighlightStroke : "rgba(220,220,220,1)",
            data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
        },
        {
            label: "My Second dataset",
            fillColor : "rgba(151,187,205,0.2)",
            strokeColor : "rgba(151,187,205,1)",
            pointColor : "rgba(151,187,205,1)",
            pointStrokeColor : "#fff",
            pointHighlightFill : "#fff",
            pointHighlightStroke : "rgba(151,187,205,1)",
            data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
        }
    ]
    }
window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myLine = new Chart(ctx).Line(lineChartData, {
        responsive: true
    });
}

以下是我的php / msql:

$result = mysql_query("SELECT COUNT(*) FROM customer WHERE month='january'") or die(mysql_error());
$row1 = mysql_fetch_array( $result );

$result = mysql_query("SELECT COUNT(*) FROM customer WHERE month='february'") or die(mysql_error());
$row2 = mysql_fetch_array( $result );

$result = mysql_query("SELECT COUNT(*) FROM customer WHERE month='march'") or die(mysql_error());
$row3 = mysql_fetch_array( $result );

如何使用count()我的MySQL查询中的那些并将其实现到chartjs上的数据集?我也希望标签也可以从我的MySQL查询中生成。我应该在jQuery代码内部循环数据集吗?

这是我正在使用的插件:http :
//www.chartjs.org/docs/#line-chart-
introduction


问题答案:

请将您的php代码放入另一个文件中,api.php并使用 JSON*
格式$.ajax获取这些数据。要将数据转换为JSON格式的数据,应使用php函数
*json_encode()

我提供了示例示例,您可以在这里看到。

请参考以下代码示例:

  1. api.php

    $arrLabels = array("January","February","March","April","May","June","July");
    

    $arrDatasets = array(‘label’ => “My First dataset”,’fillColor’ => “rgba(220,220,220,0.2)”, ‘strokeColor’ => “rgba(220,220,220,1)”, ‘pointColor’ => “rgba(220,220,220,1)”, ‘pointStrokeColor’ => “#fff”, ‘pointHighlightFill’ => “#fff”, ‘pointHighlightStroke’ => “rgba(220,220,220,1)”, ‘data’ => array(‘28’, ‘48’, ‘40’, ‘19’, ‘86’, ‘27’, ‘90’));

    $arrReturn = array(array(‘labels’ => $arrLabels, ‘datasets’ => $arrDatasets));

    print (json_encode($arrReturn));

  2. example.html

    $.ajax({
    

    type: ‘POST’,
    url: ‘api.php’,
    success: function (data) {
    lineChartData = data;//alert(JSON.stringify(data));
    var myLine = new Chart(document.getElementById(“canvas”).getContext(“2d”)).Line(lineChartData);

    var ctx = document.getElementById(“canvas”).getContext(“2d”);
    window.myLine = new Chart(ctx).Line(lineChartData, {responsive: true});
    }
    });

请注意,您应该传递价值randomScalingFactor()api.php

请检查并告知我是否需要其他帮助。



 类似资料:
  • 问题内容: 我想使用php和jquery ajax从mysql数据库中获取数据。“ process.php”是连接到数据库并获取mysql数据的php文件。当它单独运行时它可以工作,但是当使用ajax调用时它不起作用。有人可以帮忙纠正错误吗?这是我的html文件: 这是我的process.php文件 问题答案: 您的ajax调用中有两个语法错误: 请记住,jQuery的ajax需要一个对象作为参数

  • 问题内容: 我上传了多张图片,并且所有图片的路径都已存储在一起。 使用我已经将它们分开,现在我希望在轮播中回显它们 我正在使用的代码是: 但它只显示一张图像。另外,当我使用next控件时,即使我尝试向前或向后移动,此控件也不会显示任何图像。 问题答案: 这里可能有一些问题… 1.爆炸() 首先,如果您的字符串在文件名之间没有星号,则您可能无法工作。在对OP的回复评论中,您给了我们一个示例的内容,该

  • 我想操作PHP代码来搜索sql数据库中匹配的特定数据。

  • 问题内容: 我正在尝试创建一个用于在PHP上执行oracle sql语句的类。 这是我的index.php,我正在尝试调用我的函数 和我的dbcontrol.php用于我的功能 我不确定似乎有什么问题。但是每次我运行这个。页面上未显示任何内容。没有结果,没有数据。但是我确信数据库中有数据。 问题答案: 您总是得到空白页的原因是: oci_num_rows()函数不会返回您可能认为的所选行数。它返回

  • 我的数据库表如下所示 我想要得到所有的一周的时间,有多少任务是在星期一,星期二...星期五创建的。如有任何帮助,我们将不胜感激。

  • 我试过下面的代码 但我在这一行收到警告 新数组