在Javascript图表中调用php变量

时间:2015-01-07 04:46:26

标签: javascript php bar-chart

您好我想在javascript图表中显示数据库中的值。 我尝试了整个脚本的生态,但它不起作用。

这是我的代码。关于如何做到这一点的任何线索?

<body>
    <div style="width: 50%">
        <canvas id="canvas" height="450" width="600"></canvas>
    </div>


<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};

var barChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,0.8)",
            highlightFill : "rgba(151,187,205,0.75)",
            highlightStroke : "rgba(151,187,205,1)",
            data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
        }
    ]

}
window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myBar = new Chart(ctx).Bar(barChartData, {
        responsive : true
    });
}

</script>
</body>

我想控制这些值:

labels : ["January","February","March","April","May","June","July"],

data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]

1 个答案:

答案 0 :(得分:0)

为此,您需要将 JSON 字符串作为参数传递给 JavaScript 函数,该函数将具有像月这样的关联数组:缩放因子作为键值对。然后在函数中你可以解析 JSON 字符串并获取所需的数据并传递给图表函数。