doulangchao8934 2014-07-12 14:50
浏览 80

如何使用动态值更新饼图?

this is my code:

<?php
$pfstatetext = get_mypfstate();
$cpuusage= cpu_usage();
?>
<script>
var myVar=setInterval(function(){myTimer()},10000);

function myTimer() {
    var ctx2 = document.getElementById("chart-area2").getContext("2d"); 
    var myPie2 = new Chart(ctx2).Pie(pieData2);
}
</script>
<div id="show">
<canvas id="chart-area2" width="300" height="300"/>
</div>

<script>

    var pieData2 = [
            {
                value: <?= $pfstatetext;?>,
                color:"#F7464A",
                highlight: "#FF5A5E",
                label: "Red :"
            },
            {
                value: <?= $cpuusage; ?>,
                color: "#46BFBD",
                highlight: "#5AD3D1",
                label: "Green"
            },
            {
                value: 100,
                color: "#FDB45C",
                highlight: "#FFC870",
                label: "Yellow"
            },
            {
                value: 40,
                color: "#949FB1",
                highlight: "#A8B3C5",
                label: "Grey"
            },
            {
                value: 120,
                color: "#4D5360",
                highlight: "#616774",
                label: "Dark Grey"
            }

        ];
window.onload = function(){
            var ctx2 = document.getElementById("chart-area2").getContext("2d");
            var myPie2 = new Chart(ctx2).Pie(pieData2);

        };


 </script>

'$pfstatetext' and '$cpuusage' values will be changing automatically. the above code is refreshing the pie chart every 10 seconds. But the values of the pieData2 is displaying the pie chart with the values, generated when the page was loaded, every time the pie chart refreshes after 10 seconds.

When I reload the page the pie chart will be drawn with different and updated values of '$pfstatetext' and '$cpuusage' and these same values will be displayed every time the pie chart refreshes after 10 seconds, but not with changing values of '$pfstatetext' and '$cpuusage'.

So what changes will i have to make so that changing values of '$pfstatetext' and '$cpuusage' will be taken by pie chart every time it refreshes after 10 seconds?

  • 写回答

1条回答 默认 最新

  • douzai2562 2014-07-12 15:02
    关注

    This is because you seem to be misunderstanding the difference between server-side code and client-side code. PHP will execute on the server first followed by your JS code. If you want the values to change you will need to generate an Ajax call to get the new values.

    Step 1.

    Set up PHP script that returns your '$pfstatetext' and '$cpuusage' values in say, an array or JSON String.

    Step 2.

    Make Ajax call that hits the file you set up in Step 1 and JavaScript (Client) will then have access to the data returned from your server (PHP).

    I can post some code later if you need that, but it is pretty straight forward. Good luck.

    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)