簡體   English   中英

數據數組中帶有 php 循環的谷歌圖表未顯示

[英]google chart with php loop in data array not displaying

我正在嘗試在谷歌圖表中顯示一些 PHP 代碼。 我在同一頁面上有另一個圖表使用相同的過程,並且工作正常,但由於某種原因,第二個圖表沒有顯示。

我在 PHP 中有一個鍵值數組,我知道它包含我需要的信息,因為我已經用這個循環對其進行了測試。

foreach($categoryKeyValues as $key => $value){
    echo "['".$key.' '.$_SESSION['currency'].number_format($value,2)."', 
    ".number_format($value,2)."],";
}

並且它顯示正確,因此數據就在那里。

這是循環中的 output ['Entertainment £167.99', 167.99],['Other £0.00', 0.00],['Bill £1,155.81', 1,155.81],['Motoring £225.00', 225.00],[ £126.58', 126.58],['節省 £187.00', 187.00],['場合 £87.50', 87.50],['保險 £37.00', 37.00],

當我調用 javascript 顯示圖表時出現錯誤,圖表不顯示。 這是我的圖表代碼。

<!-- Graph for Categorised expenditure -->
<script type="text/javascript">
    google.charts.load('current', {'packages':['corechart']});  
    google.charts.setOnLoadCallback(drawCategory);
    function drawCategory()
    {
        var data = google.visualization.arrayToDataTable([
                ['Category', 'Total'],
                <?php
                    foreach($categoryKeyValues as $key => $value)
                    {
                        if($value > 0){
                            echo "['".$key.' '.$_SESSION['currency'].number_format($value,2)."', ".number_format($value,2)."],";
                            $atotal = $atotal + $value;
                        }
                    }
                    //$title = "Account Type, Total - ".$total;
                 ?>
             ]);
        var options = {
              is3D:true,
              chartArea: {left: '1%', top: '5%', right:'1%', width: '95%', height: '100%'},
              width: $(window).width()*0.50,
              height: $(window).width()*0.20,
              pieSliceText: 'none',
              sliceVisibilityThreshold: 0.00,
              pieHole:0.4,
              tooltip: {
                isHtml:true,
                showColorCode: true,
                trigger: 'hover'
              },
              colors: ['#204969', '#719192', '#5f6769', '#5c94bd','#1a3e59', '#315b96', '#719192', '#3c4245', '#dadada']
             };
        var chart = new google.visualization.PieChart(document.getElementById('categoryChart'));  
        chart.draw(data, options);
    }
    // And then:
    $(window).resize(function () {
        drawCategory();
    });
</script>

工作圖的圖像和第二個圖應該出現在下面的第二個圖的循環結果。

好的,經過數小時的錯誤檢查和測試,我發現了問題。 最后它很簡單,但很難找到,所以我在這里發布我的發現,希望這能幫助更多的人。

事實證明,錯誤出現在 php number_format 中。 我正在使用 number_format($number,2) 當我的數據庫結果返回一個大於 1000.00 的數字時,結果被保存在數組中作為 1,000.00 創建一個額外的數組索引,這反過來又在我的谷歌圖表中創建了一個額外的列。 這導致了異常。

我通過使用 number_format($cvalue,2,'.','') 解決了這個問題。 我的圖表現在顯示並且看起來像這樣。

圖表顯示正確

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM