CanvasJS线图生成?

时间:2015-05-21 19:53:13

标签: javascript php canvasjs

我正忙着制作一个用于实时数据显示的javascript图表。我发现了这个CanvasJS API。他们网页上specific chart的默认代码很不错,但当然是他们获取数据的部分,他们将变量数组留空并使用数学随机数据函数代替,以便特定数据将是随机生成并在图表中使用。

但是我想用我希望使用的特定表来填充数据库数据!

我尝试了很多东西但是如果我实现了我的部分,图表将不再显示。我做错了什么?

以下是我使用的3个代码:

默认代码(仅供比较):

println("1234".toUInt()) // Optional(1234)
// This is UInt.max on a 64-bit platform:
println("18446744073709551615".toUInt()) // Optional(18446744073709551615)
println("18446744073709551616".toUInt()) // nil (overflow)
println("1234x".toUInt()) // nil (invalid character)
println("-12".toUInt()) // nil (invalid character)

我自己的图表代码,基于以上代码:

<!DOCTYPE HTML>
<html>

<head>
    <script type="text/javascript">
    window.onload = function () {

        var dps = []; // dataPoints

        var chart = new CanvasJS.Chart("chartContainer",{
            title :{
                text: "Live Random Data"
            },          
            data: [{
                type: "line",
                dataPoints: dps 
            }]
        });

        var xVal = 0;
        var yVal = 100; 
        var updateInterval = 20;
        var dataLength = 500; // number of dataPoints visible at any point

        var updateChart = function (count) {
            count = count || 1;
            // count is number of times loop runs to generate random dataPoints.

            for (var j = 0; j < count; j++) {   
                yVal = yVal +  Math.round(5 + Math.random() *(-5-5));
                dps.push({
                    x: xVal,
                    y: yVal
                });
                xVal++;
            };
            if (dps.length > dataLength)
            {
                dps.shift();                
            }

            chart.render();     

        };

        // generates first set of dataPoints
        updateChart(dataLength); 

        // update chart after specified time. 
        setInterval(function(){updateChart()}, updateInterval); 

    }
    </script>
    <script type="text/javascript" src="/assets/script/canvasjs.min.js"></script>
</head>
<body>
    <div id="chartContainer" style="height: 300px; width:100%;">
    </div>
</body>
</html>

和我的data.php 显示以下输出:

  

[{ “×”: “1”, “Y”: “5”},{ “×”: “2”, “Y”: “5”},{ “×”: “3”,” Y “:” 4 “},{” × “:” 4" , “Y”: “1”},{ “×”: “5”, “Y”: “8”},{ “×”:” 6" , “Y”: “9”},{ “×”: “7”, “Y”: “5”},{ “×”: “8”, “Y”: “6”},{” X “:” 9" , “Y”: “4”},{ “×”: “10”, “Y”: “7”}]

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script src="jquery.js"></script>
    <script src="jquery.canvasjs.js" ></script>
    <script src="canvasjs.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {

            $.getJSON("data.php", function (result) {

                var chart = new CanvasJS.Chart("chartContainer", {
                    data: [
                        {
                            dataPoints: result
                        }
                    ]
                });

                chart.render();
            });
        });
    </script>
</head>
<body>

    <div id="chartContainer" style="width: 800px; height: 380px;"></div>

</body>
</html>

希望你有足够的信息!

谢谢

Mieer

修改

我根据来自以下内容的@ feedback-comment交换了来自“我自己的图表”代码的代码: Anjali Jain

- &GT;

 <?php
    //header('Content-Type: application/json');
    $con = mysqli_connect("localhost","root","","WebApplication");

    // Check connection
    if (mysqli_connect_errno($con))
    {
        echo "Failed to connect to DataBase: " . mysqli_connect_error();
    } 
    else
    {
    $data_points = array();

    $result = mysqli_query($con, "SELECT * FROM info");

    while($row = mysqli_fetch_array($result))
    {        
        $point = array("x" => $row['id'] , "y" => $row['acceleration']);

        array_push($data_points, $point);        
    }

        echo json_encode($data_points, 32); //define('JSON_NUMERIC_CHECK',32);   // Since PHP 5.3.3

    }
    mysqli_close($con);



    ?>

然而,我仍然一无所获。

1 个答案:

答案 0 :(得分:1)

当您直接传递字符串时,CanvasJS要求x和y值为数字。您可以将它们转换为数字,如下所示

$.getJSON("data.php", function (result) {

    var dataPoints = [];

    for (var i = 0; i <= result.length - 1; i++) {
        dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
    }

    var chart = new CanvasJS.Chart("chartContainer", {
        data: [
        {
            dataPoints: dataPoints
        }
        ]
    });

    chart.render();
});