0

我已经阅读了这里试图获取从 PHP 传递到 javascript 的数值的几乎所有问题。如您所见,我成功生成了随机数并且效果很好,但是当我尝试将变量传递给 graph.update 函数时,即使两个变量都确实具有根据 firebug 的值(分别为 14060116 和 0,这是正确的)他们没有进入更新功能......有什么想法吗?如果有帮助,这是完整的脚本!

<script>
(function () {

    function createCanvas(divName) {

        var div = document.getElementById(divName);
        var canvas = document.createElement('canvas');
        div.appendChild(canvas);
        if (typeof G_vmlCanvasManager != 'undefined') {
            canvas = G_vmlCanvasManager.initElement(canvas);
        }   
        var ctx = canvas.getContext("2d");
        return ctx;
    }

    var ctx = createCanvas("graphDiv1");
    var upld = <?php echo json_encode($dirupld[0]); ?>;
    var upldred = <?php echo json_encode($dirupldred[0]); ?>;

    var graph = new BarGraph(ctx);
    graph.maxValue = 30;
    graph.margin = 10;
    graph.width = 450;
    graph.height = 200;
    graph.colors = ["#49a0d8", "#d353a0"];
    graph.xAxisLabelArr = ["Traditional", "Duplicate Reduced"];
    graph.update([upld, upldred]);
    //graph.update([Math.random() * 30, Math.random() * 30]);
}());
</script>

不确定我在哪里失去了价值观?Firebug 报告以下内容

var upld = 14060116;
var upldred = 0;
graph.update([upld, upldred]); 
4

3 回答 3

0

尝试更改 graph.maxValue 的值以显示 upld

我假设您不希望为 upldred 显示任何内容,因为它已经是 0

于 2013-10-03T04:12:38.960 回答
0
First you will passing hidden values in php form
something like this
<input type="hidden" name="upld" id="upld" value="<?php echo json_encode($dirupld[0]); ?>">
<input type="hidden" name="upldred" id="upldred" value=" <?php echo json_encode($dirupldred[0]); ?>">

then you will get this value in javascript

var upld=document.getElementById("upld").value;
var upldred=document.getElementById("upldred").value;

dont use below the code
 var upld = <?php echo json_encode($dirupld[0]); ?>; //Is not Correct
    var upldred = <?php echo json_encode($dirupldred[0]); ?>;//Is not correct

Use document.getElementById().value syntax
于 2013-10-03T04:17:48.127 回答
0

你的 PHP 看起来不错;您从 PHP 中获取整数,然后将这些整数作为参数传递以呈现条形图。正如其他人所提到的,它出现的事实意味着你的 PHP 很好,这是一个 JS 问题。

但是,由于您设置graph.maxValue = 30;了,当您尝试渲染值为 的图表时会出现错误14060116

Uncaught IndexSizeError: Index or size was negative, or greater than the allowed value.

(我假设您使用的是http://www.williammalone.com/articles/html5-canvas-javascript-bar-graph/

于 2013-10-03T04:23:30.193 回答