2013-04-09 72 views
2

我想在PHP和MySQL中使用jQuery制作图表,特别是使用Highcharts库,我设法使用来自MySQL数据库的数据制作图表,但这些数据显示一些数据(如果它们在数据库中)但不是其他人,例如,产生10个结果的查询5是好的,但其他5个没有,我在网络上做了一些研究,我知道这是由于当我将查询的结果传递给MySQL by json_encode,我希望你们中的一些人可以告诉我,因为我有这个错误,或者如果我滥用JSON,非常感谢。 代码: 的index.phpjson_encode和highcharts

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>Incidencias</title> 
    <script type="text/javascript" src="../js/jquery-1.7.1.min.js" ></script> 
    <script type="text/javascript" src="../js/highcharts.js" ></script> 
    <script type="text/javascript" src="../js/themes/grid.js"></script> 
    <script type="text/javascript" src="../js/export.js"></script> 
    <script type="text/javascript"> 
     var chart; 
      $(document).ready(function() { 
       var options = { 
        chart: { 
         renderTo: 'pieClientes', 
         defaultSeriesType: 'pie', 
         marginRight: 10, 
         marginBottom: 25 
        }, 
        title: { 
         text: 'top 10. Clientes con m\u00e1s incidencias', 
         x: -10 
        }, 
        subtitle: { 
         text: '', 
         x: -20 
        }, 
        tooltip: { 
         formatter: function() { 
          return '<b>'+ this.point.name +'</b>: <b>'+ this.y +' incidencias</b>'; 
         } 
        }, 
        legend: { 
         layout: 'vertical', 
         align: 'right', 
         verticalAlign: 'top', 
         x: -10, 
         y: 100, 
         borderWidth: 0 
        }, 

       series: [{ 
        type: 'pie', 
        name: 'Browser share', 
        data: [] 
       }] 
      } 

      $.getJSON("datosCliente.php", function(json) { 
       options.series[0].data = json; 
       chart = new Highcharts.Chart(options); 
      }); 
      }); 
    </script> 
</head> 
<body> 
    <div style="font-size: 12p; font-family: Calibri, Candara, Segoe, Segoe UI, Optima, Arial, sans-serif; align="left" > 
     <h1>Dashboard incidencias SCI</h1> 
    <div> 
    <div id="pieClientes" style="width: 770px; height: 450x; margin: 0"></div>    
</body> 
</html> 

datosCliente.php

<?php 
    include"../clases/conexion.php"; 
    $result = mysql_query("SELECT 
        clientes.cliente, 
        COUNT(incidencias.idIncidencia) AS nIncidencias 
        FROM incidencias 
        INNER JOIN clientes ON incidencias.idCliente = clientes.idCliente 
        GROUP BY incidencias.idCliente 
        ORDER BY nIncidencias DESC 
        LIMIT 0,10"); 
    $rows = array(); 
     while($r = mysql_fetch_array($result)) 
      { 
       $row[0] = $r[0]; 
       $row[1] = $r[1]; 
       array_push($rows,$row); 
      } 

    //print json_encode($rows, JSON_PRETTY_PRINT); 
    echo json_encode($rows, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); 
    mysql_close($con); 
?> 

当我想显示10行数据库在图中的误差是例如,它告诉我这个结果形式:

 1st. row-> show me "slice" 
     2nd. row-> the registerok, 
     3th. row-> show me "slice" 
     4. row-> the registerok, 
     5. row-> the registerok, 
     6.row-> show me "slice" 
    7.row->the registerok, 
    8.row-> show me "slice" 
    9.row->the registerok, 
    10.row-> show me "slice" 

的问题是,在曲线图(饼图)显示器正确的数据而不是其他人,这是因为如果一些记录或tubiese跳过5的限制registrros,而不是我不知道我错在哪里。 :(

感谢帮助:)

+0

什么是错误? – Phil 2013-04-09 23:11:18

+0

您的数据/ JSON的外观如何?你是否按x升序排列数据? – 2013-04-11 09:48:45

回答

0

首先我会尽量给数组元素的值,而不是使用索引。给你的数组键一个名字。同时删除编码中的所有大写字母。我在iPad上,无法编码,但如果您有任何问题,请告诉我。