jQuery Mobile中的饼图没有显示出来

时间:2013-12-12 09:01:49

标签: php jquery-mobile pie-chart

我开发了一个jQuery移动网站,想要显示饼图,数据获取表单数据库以饼图显示。由于jQuery移动链接到使用div的另一个页面,我在index.php中创建一个div来显示数据库中的数据,然后当数据显示时,我可以通过单击视图链接来查看数据。在这一部分,我点击视图链接时加载jQuery加载页面piechart.php。由jQuery加载调用的piechart.php页面成功,但不显示饼图,但如果我右键单击页面并单击查看检查元素,它会开始正常显示饼图。

为什么我需要右键单击显示它?

这是index.php

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>SSPIC</title>
    <meta name="viewport" content="width=device-width, maximum-scale=1.0, minimum-scale=1.0, initial-scale=1" />
    <meta name="description" content="This site was created from a template originally designed and developed by Codify Design Studio. Find more free templates at http://www.adobe.com/devnet/author_bios/chris_converse.html" />
    <link href="jquery-mobile/jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css" />
         <script type="text/javascript" src="js/formcalculations.js"></script>

      <link href="styles/cakeform.css" rel="stylesheet" type="text/css" />
     <script src="jquery-mobile/jquery-1.6.4.min.js" type="text/javascript"></script>
     <script src="jquery-mobile/jquery.mobile-1.0.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="themes/my_company.min.css" />
    <link rel="stylesheet" href="includes/overrides.css" />
    <script type="text/javascript" src="includes/custom.js"></script>
      <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>

          <link href="stylesheets/app.css" media="screen, projector, print" rel="stylesheet" type="text/css" />
     <link href="stylesheets/pizza.css" media="screen, projector, print" rel="stylesheet" type="text/css" />

       <script src="javascripts/vendor/snap.svg.js"></script> 
       <script src="javascripts/pizza.js"></script> 

         <script type="text/javascript">
    function MM_popupMsg(msg) { //v1.0
      alert(msg);
    }
         </script>
   </head>
    <body onload='hideTotal()'>

        <div data-role="page" id="home" data-theme="a">
        <div data-role="header">
            <h1>Homepage</h1>
        </div>
        <div data-role="content">
            <ul data-role="listview">
                <li class="btn_a"><a href="#about_us">About Us</a></li>
                <li class="btn_s"><a href="#services">Services</a></li>
                <li class="btn_l"><a href="#locations">Search</a></li>
                <li class="btn_c"><a href="#contact_us">Upload</a></li>
                <li class="btn_c"><a href="#chart">Chart</a></li>
            </ul>
        </div>
        <div data-role="">
        </div>
    </div>





    <div data-role="page" id="locations" data-title="Find a Locations">
        <div data-role="header">
            <h1>Search Data</h1>
            <a href="#" data-rel="back" data-icon="arrow-l" data-iconpos="notext">back</a>
        </div>
        <div data-role="content">
            <p></p>
            <h2>Search Data</h2>
        </div>
        <div data-role="footer">
            <div data-role="controlgroup" data-type="horizontal">
                <a href="#home" data-role="button" data-icon="home">Home</a>
            </div>
            <h4>©2013 SSPIC</h4>
        </div>
    </div>

    <div data-role="page" id="contact_us" data-title="Contact Us">
        <div data-role="header">
            <h1>Upload</h1>
            <a href="#" data-rel="back" data-icon="arrow-l" data-iconpos="notext">back</a>
        </div>
        <div data-role="content" class="content_container">
            <p><form action="insertimage.php" method="post" enctype="multipart/form-data" name="form1">
                  <h2 align="center">&nbsp;</h2>
                  <h2 align="center">Information</h2>
                  <table width="63%" border="1" align="center">
                    <tr>
                      <td width="40%">Upload File</td>
                      <td width="60%"><input name="pic" type="file" id="pic" /></td>
                    </tr>
                    <tr>
                      <td colspan="2"><div align="center">
                        <input type="submit" name="Submit" value="Submit" />
                      </div></td>
                    </tr>
                  </table>
                  <p align="center">&nbsp;</p>
                  <p align="center">&nbsp;</p>
                  <p align="center">                      
                </form></p>
        </div>
        <div data-role="footer">
            <div data-role="controlgroup" data-type="horizontal">
                <a href="#home" data-role="button" data-icon="home">Home</a>
            </div>
            <h4>©2013 SSPIC</h4>
        </div>
    </div>


    <div data-role="page" id="chart" data-title="chart Us" class="chart">
        <div data-role="header">
            <h1>Pie Chart</h1>
            <a href="#" data-rel="back" data-icon="arrow-l" data-iconpos="notext">back</a>
        </div>
        <div id="output" data-role="content" class="content_container"></div>
        <div data-role="content" class="content_container" id="k_chart">
            <p>


            <form action="insertimage.php" method="post" enctype="multipart/form-data" name="form1">
                  <h2 align="center">&nbsp;</h2>
                  <h2 align="center">Display File</h2>
                  <table width="63%" border="1" align="center" class="table_chart">
                    <tr>
                      <td width="40%">Bil</td>
                      <td width="60%">Pusat rawatan</td>
                      <td width="60%">Pengasas</td>
                      <td width="60%">Bil pengamal</td>
                      <td width="60%">Total</td>
                      <td width="60%">Phone number</td>
                      <td width="60%">Peratus</td>
                      <td width="60%">view chart</td>
                    </tr>
                  <?php 
                  include("connection.php");

                  $sel = mysql_query("select * from info");
                  $a = 1;
                  while($get_sel = mysql_fetch_array($sel))
                  {
                  ?>
                  <tr>
                      <td width="40%"><?php echo $a++;?></td>
                      <td width="60%"><?php echo $get_sel['nama_PusatRawatan'];?></td>
                      <td width="40%"><?php echo $get_sel['nama_Pengasas'];?></td>
                      <td width="60%"><?php echo $get_sel['bilangan_Pengamal'];?></td>
                      <td width="40%"><?php echo $get_sel['totalPrice'];?></td>
                      <td width="60%"><?php echo $get_sel['phonenumber'];?></td>
                      <td width="60%"><?php echo $get_sel['peratus'];?></td>
                      <td width="60%"><div class="id"><?php echo $get_sel['id'];?></div><span class="viewBtn">View</span></td>
                    </tr>
                   <?php 
                  }
                  ?>
                  </table>
                  <p align="center">&nbsp;</p>
                  <p align="center">&nbsp;</p>
                  <p align="center">                      
                </form></p>
        </div>

        <div data-role="footer">
            <div data-role="controlgroup" data-type="horizontal">
                <a href="#home" data-role="button" data-icon="home">Home</a>
            </div>
            <h4>©2013 SSPIC</h4>
        </div>
    </div>


     <script>
        $(document).ready(function(e) {
            // Pizza.init();
            $('.viewBtn').on('click',function()
            {
                 var id = $(this).closest('td').find('.id').text();

                $('#k_chart').hide();
                $('#output').load("piechart.php?id="+id);//load page to display pie chart

            });


            $(window).load(function() {

                Pizza.init({
                donut: false, // enable donut chart
                donut_inner_ratio: 0.4,   // between 0 and 1
                percent_offset: 35, // relative to radius
                stroke_color: '#333',
                stroke_width: 0,
                show_percent: true, // show or hide the percentage on the chart.
                animation_speed: 500,
                animation_type: 'elastic' // options: backin, backout, bounce, easein, easeinout, easeout, linear
                    });
                });
            });
        </script>
    </body>
      </html>

这是piechart.php

<link href="jquery-mobile/jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css" />
<script src="jquery-mobile/jquery.mobile-1.0.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="themes/my_company.min.css" />
<link rel="stylesheet" href="includes/overrides.css" />
<link href="stylesheets/app.css" media="screen, projector, print" rel="stylesheet"  type="text/css" />
<link href="stylesheets/pizza.css" media="screen, projector, print" rel="stylesheet" type="text/css" />
 <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script><script src="javascripts/vendor/snap.svg.js"></script>
  <script src="javascripts/pizza.js"></script> 

  <div data-role="content" class="content_container">
<p>
  <div id="jquery-script-menu">
<div class="jquery-script-center">
<div class="jquery-script-clear"></div>
</div>
</div>
<h1 style="margin-top:150px;" align="center">Papar peratus lulus pie chart </h1>
<div class="row">
<div class="large-12 columns">
<h3>Graphs</h3>
</div>
</div>
<?php 
include("connection.php");
$id_info = $_GET['id'];
$papar = mysql_query("select *from info where id = '$id_info'");
$get_papar = mysql_fetch_array($papar);
if($get_papar['peratus']>0 and $get_papar['peratus']<=50)
{
$xlulus = $get_papar['peratus'];
$lulus = 100 - $xlulus;
}
else
 {
$lulus = $get_papar['peratus'];
$xlulus = 100 - $lulus;
 }
?>
<div class="row">
<div class="large-4 small-4 columns">
<ul data-pie-id="svg">
<li data-value="<?php echo $xlulus;?>">Tidak memenuhi syarat</li><li data-value="<?php  echo $lulus;?>">Memenuhi syarat</li>
</ul>
</div>
<div class="large-8 small-8 columns">
<div id="svg"></div>
</div>
</div>
</p>
</div>   

jQuery代码位于页面底部index.php ..

1 个答案:

答案 0 :(得分:2)

您需要正确计算插件初始化时间。

首先忘记文档就绪和窗口加载,它们无法正常使用jQuery Mobile应用程序(通常它们会在jQuery Mobile内容成功增强之前触发)。

相反,您应该学习如何使用jQuery Mobile页面事件,例如存在pageinit事件来替换文档就绪。

详细了解 here

现在关于您的主要问题,您的插件必须在pageshow事件期间初始化。许多视觉插件(旋转木马,图表......)需要正确的页面高度。使用jQuery Mobile时,页面高度仅在pageshow事件期间正确计算,在任何其他情况下都将为0。

可以找到页面事件初始化顺序 here