【问题标题】:json_encode and highchartsjson_encode 和 highcharts
【发布时间】:2013-04-09 23:10:07
【问题描述】:

我正在尝试使用 jQuery 在 PHP 和 MySQL 中制作图表,特别是使用 Highcharts 库,我已经设法使用 MySQL 数据库中的数据制作图表,但是如果这些数据在数据库中而不是其他数据,则这些数据显示了一些数据,例如,产生 10 个结果的查询 5 个很好,但其他五个没有,我在网上做了一些研究,我知道这是由于我在 MySQL 中通过 json_encode 传递查询结果时出现的问题,我希望你们中的一些人能告诉我,因为我有这个错误,或者我误用了 JSON,非常感谢。 代码: index.php

    <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 个注册器的限制,而不是我不知道我错在哪里。 :(

感谢您的帮助:)

【问题讨论】:

  • 您的数据/JSON 是什么样的?您是否按 x 升序对数据进行了排序?

标签: php jquery mysql json highcharts


【解决方案1】:

首先我会尝试给数组元素一个值,而不是使用索引。给你的数组键一个名字。以及从您的编码中删除所有大写字母。我使用的是 ipad,无法编写代码,但如果您有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多