【问题标题】:Highchart from Mysql not workingMysql的Highchart无法正常工作
【发布时间】:2017-03-21 13:12:36
【问题描述】:

我想在highchart pie 3D中显示最常用的类别,但它不起作用,json编码数组只接收一行数据。这是我的php代码:

$script1 = "SELECT categoria, COUNT(categoria) from entidade GROUP BY categoria ORDER BY COUNT(categoria) DESC";
$result9 = mysql_query($script1);
$data = array();

while ($row = mysql_fetch_array($result9)) 
{
    $data[] = json_encode(array(array($row['categoria'], $row['COUNT(categoria)'])));
}  

这是我的 javaScript:

$(function () { 
    Highcharts.chart('container2', {
    chart: {
        type: 'pie',
        options3d: {
            enabled: true,
            alpha: 45
        }
    },
    title: {
        text: 'Categoria de Crimes Mais Cometidos'
    },
    subtitle: {
        text: ''
    },
    plotOptions: {
        pie: {
            innerSize: 100,
            depth: 45
        }
    },
    series: [{
        name: 'Quantidade:',
        data:JSON.parse(<?php echo(json_encode($data));?>)
    }]

请帮帮我!

【问题讨论】:

  • 能否也显示?的输出?
  • $script1 = "SELECT categoria, COUNT(categoria) from entidade GROUP BY categoria ORDER BY COUNT(categoria) DESC"; $result9 = mysql_query($script1); $数据 = 数组(); while ($row = mysql_fetch_array($result9)) { $data[] = json_encode(array(array($row['categoria'], $row['COUNT(categoria)']))); }
  • 我认为@RuhulAmin 的意思是当你输出数据时你得到了什么结果(例如console.log(JSON.parse() )
  • 没有什么是空白的
  • 但我认为问题出在 json_encode 数组中,因为格式应该是这样的:[["Butter noob",26375],["Crab Vermecilli",0],["Salted Egg Yolk Crab",0]] 但它向我展示的是这样的:Array ( [0] => 'Falsifica��o',2 [1] => 'Homic�dio',2 [2] => 'Amea �a',1 [3] => 'Extorsão',1 [4] => 'Chantagem',1 [5] => 'Rapto',1 [6] => 'Homicídio',1 [7] => 'Roubo',1 [8] => 'Ameaça',1) 你知道我怎样才能得到那种格式吗?提前谢谢...

标签: javascript php mysql highcharts


【解决方案1】:

如果您想要像之前注释掉的那样具有所需的数组格式,我认为您需要像这样分配数组:

$data = array();
$iIndexCounter = 0;
while ($row = mysql_fetch_array($result9)) {
    $data[$iIndexCounter][] = $row['categoria'];
    $data[$iIndexCounter][] = $row['COUNT(categoria)']
    iIndexCounter++;
    // e.g : $data[0][0] = Crab Vermecilli; || $data[0][1] = 0;
}

echo json_encode($aData); //[["Crab Vermecilli",0], ["Salted Egg Yolk Crab",0]]

$iIndexCounter 将成为包含这两个值的数组索引(例如 categoria 和 COUNT(categoria))

希望这对您的情况有所帮助。

【讨论】:

  • 好的,谢谢你的提示,但是在 data:JSON.parse() 中它会得到该格式的数据吗?
  • 我可以看看您在 [data] 属性上得到的输出吗?我预计它将返回一个 [object] 数据类型。我以为您已经事先检索了该值?
猜你喜欢
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 2012-05-29
相关资源
最近更新 更多