【问题标题】:calling php variable in Javascript chart在 Javascript 图表中调用 php 变量
【发布时间】:2015-01-07 04:46:25
【问题描述】:

您好,我正在尝试在 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()]

【问题讨论】:

  • 服务器与客户端的问题。尝试检查 AJAX。也就是说,如果您从数据库中获取数据。我并没有真正在代码中看到它。
  • 你可以使用 在你的脚本中使用你的 PHP 变量,试试这个。它可能对你有用
  • 问题已修复。我在js中直接调用了php变量。

标签: javascript php bar-chart


【解决方案1】:

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

【讨论】:

  • 举个例子会很有帮助。
  • 这里是示例代码:$json = "[{'X':0,'Y':1.52},{'X':1,'Y':1.56},{'X' :2,'Y':1.42},{'X':3,'Y':1.85},{'X':4,'Y':1.62}]"; $callscript = "get_chart($json);";回声 $callscript; function get_chart(data) { for(keyd in data) { alert(keyd); } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多