简体   繁体   English

数据数组中带有 php 循环的谷歌图表未显示

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

I'm trying to display some PHP code within a google chart.我正在尝试在谷歌图表中显示一些 PHP 代码。 I have another chart on the same page using the same process and that one works fine but for some reason, the second chart is not displaying.我在同一页面上有另一个图表使用相同的过程,并且工作正常,但由于某种原因,第二个图表没有显示。

I have a key-value array in PHP which I know contains the information I require because I have tested it with this loop.我在 PHP 中有一个键值数组,我知道它包含我需要的信息,因为我已经用这个循环对其进行了测试。

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

and it displays correctly so the data is there.并且它显示正确,因此数据就在那里。

This is the output from the loop ['Entertainment £167.99', 167.99],['Other £0.00', 0.00],['Bill £1,155.81', 1,155.81],['Motoring £225.00', 225.00],['Borrowing £126.58', 126.58],['Saving £187.00', 187.00],['Occasions £87.50', 87.50],['Insurance £37.00', 37.00],这是循环中的 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],

When I call the javascript to display the chart there is an error and the chart is not displayed.当我调用 javascript 显示图表时出现错误,图表不显示。 Here is my chart code.这是我的图表代码。

<!-- 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>

Image of the working graph and result of the loop for the second graph underneath where the second graph should appear.工作图的图像和第二个图应该出现在下面的第二个图的循环结果。

Okay so after hours of error checking and testing I found the problem.好的,经过数小时的错误检查和测试,我发现了问题。 It was simple in the end but very hard to find so I am posting my findings here and hopefully this will help someone further down the line.最后它很简单,但很难找到,所以我在这里发布我的发现,希望这能帮助更多的人。

It turns out the error was in the php number_format.事实证明,错误出现在 php number_format 中。 I was using number_format($number,2) when my database result returned a number greater than 1000.00 the result was held in the array as 1,000.00 creating an extra array index which in turn created an extra column in my google chart.我正在使用 number_format($number,2) 当我的数据库结果返回一个大于 1000.00 的数字时,结果被保存在数组中作为 1,000.00 创建一个额外的数组索引,这反过来又在我的谷歌图表中创建了一个额外的列。 This caused an exception.这导致了异常。

I solved the problem by using number_format($cvalue,2,'.','').我通过使用 number_format($cvalue,2,'.','') 解决了这个问题。 my chart now displays and looks like this.我的图表现在显示并且看起来像这样。

图表显示正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM