canvasjs显示空白,数据未显示

时间:2017-07-31 18:12:32

标签: javascript jquery canvasjs

我正在使用Canvasjs来创建烛台图表。这些值来自PHP,我使用JSON-encode转换为javascript数组。我以与示例相同的格式重新创建javascript数组,这是

 dataPoints=[{x: new Date(2012,01,01),y:[5198, 5629, 5159, 5385]}]

但画布是空白的?这是我的代码:

<?php


$chart_array[] = array("x"=>"2012-01-01","y"=>array("5198", "5629", "5159", "5385"));
$chart_array[] = array("x"=>"2012-01-02","y"=>array("5366", "5499", "5135", "5295"));

    $chart_array = json_encode($chart_array);

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


            var resultArray = <?php echo $chart_array; ?>;

            var new_array = [];
            jQuery.each(resultArray, function(index, item) {

                new_array.push({ x: new Date(item.x), y: item.y });

            });

console.log(new_array);
            var chart = new CanvasJS.Chart("chartContainer", {
                title: {
                    text: "Basic Candle Stick Chart"
                },
                zoomEnabled: true,
                axisY: {
                    includeZero: false,
                    title: "Prices",
                    prefix: "$ "
                },
                axisX: {
                    interval: 2,
                    intervalType: "month",
                    valueFormatString: "MMM-YY",
                },
                data: [
                {
                    type: "candlestick",
                    dataPoints: new_array
                }
                ]
            });
            chart.render();
        }
    </script>

console.log显示数组存在。为什么显示空白画布?我该如何解决?

1 个答案:

答案 0 :(得分:1)

代码中的

Y-value似乎是字符串,应该是数字。即使您将其存储为字符串,在将其传递给图表选项之前将其解析为数字也可以正常工作。

以下是工作代码:

<?php

$chart_array[] = array("x"=>"2012-01-01","y"=>array(5198, 5629, 5159, 5385));
$chart_array[] = array("x"=>"2012-01-02","y"=>array(5366, 5499, 5135, 5295));

$chart_array = json_encode($chart_array);
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>

<script type="text/javascript">
    window.onload = function () {
        var resultArray = <?php echo $chart_array; ?>;

        var new_array = [];
        jQuery.each(resultArray, function(index, item) {
            new_array.push({ x: new Date(item.x), y: item.y });
        });

        var chart = new CanvasJS.Chart("chartContainer", {
            title: {
                text: "Basic Candle Stick Chart"
            },
            zoomEnabled: true,
            axisY: {
                includeZero: false,
                title: "Prices",
                prefix: "$ "
            },
            axisX: {
                interval: 2,
                intervalType: "month",
                valueFormatString: "MMM-YY",
            },
            data: [
            {
                type: "candlestick",
                dataPoints: new_array
            }
            ]
        });
        chart.render();
    }
</script>

<div id="chartContainer" style="height: 300px; width: 100%;"></div>