我真的需要完成此操作,我需要将它们按四类进行分组,Im当前在表中使用饼图来显示每个项目的百分比,以及使用此查询的计数:


  这是我的完整代码:https://www.pastiebin.com/5be43c1f6ec73


$query = "SELECT DISTINCT trendx, COUNT(*) as counter FROM jeremy_table_trend GROUP BY trendx";


那是我查询的结果

Escalate                        99
Not Supported                12809
PE.Win32.TRX.XXPE50FFF026       23
PE.Win32.TRX.XXPE50FFF027      102
PUA.Win32.TRX.XXPE50FFF026       1
PUA.Win32.TRX.XXPE50FFF027      16
PUA.Win32.TRX.XXPE50FFF027P0005  1
Ransom.Win32.TRX.XXPE50FFF026   96
Ransom.Win32.TRX.XXPE50FFF027  619
Rapid Proliferation            596
TROJ.Win32.TRX.XX.PE50FFF026     5
Undet                         1425
Undet - component               17
Undet - Feedback               712
Undet - FileSize                 9
Undet - M/P                     73
Undet - Maturity               146


这是我的饼图的输出,但我希望它们按4-Undetected, Supported,Escalate and Not Supported分组。我怎样才能做到这一点?请帮我

php - 选择计数字符串,然后将它们分组为饼图-LMLPHP

这是我的图表代码:

<!-- Chart code -->
<script>
var chart = AmCharts.makeChart("chartdiv", {
  "fontSize":15,
  "type": "pie",
  "startDuration": 0,
   "theme": "black",
  "addClassNames": true,
  "legend":{
    "fontSize":15,
    "position":"right",
    "marginRight":250,
    "autoMargins":false
  },
  "innerRadius": "0",
  "defs": {
    "filter": [{
      "id": "shadow",
      "width": "200%",
      "height": "200%",
      "feOffset": {
        "result": "offOut",
        "in": "SourceAlpha",
        "dx": 0,
        "dy": 0
      },
      "feGaussianBlur": {
        "result": "blurOut",
        "in": "offOut",
        "stdDeviation": 5
      },
      "feBlend": {
        "in": "SourceGraphic",
        "in2": "blurOut",
        "mode": "normal"
      }
    }]
  },
  "dataProvider":
  [
  <?php while($row = mysqli_fetch_array($result)): ?>

    {
      "TRENDX": "<?php echo $row['trendx'] ?>",
      "Values": <?php echo $row['counter']; ?>
    },

  <?php endwhile; ?>
  ]
  ,
  "valueField": "Values",
  "titleField": "TRENDX",
  "outlineAlpha": 0.4,
  "depth3D": 30,
  "balloonText": "[[title]]<br><span style='font-size:20px'><b>[[value]]</b> ([[percents]]%)</span>",
  "angle": 20,
  "export": {
    "enabled": true
  }
});

chart.addListener("init", handleInit);

chart.addListener("rollOverSlice", function(e) {
  handleRollOver(e);
});

function handleInit(){
  chart.legend.addListener("rollOverItem", handleRollOver);
}

function handleRollOver(e){
  var wedge = e.dataItem.wedge.node;
  wedge.parentNode.appendChild(wedge);
}
</script>


我正在考虑这个想法,但我不知道如何将它们按四个分组

对于未还原:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Undet%';


对于升级:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Escalate%';


对于不支持:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Not Supported%';


对于支持:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx NOT LIKE '%Not Supported%' OR '%Undet%' OR '%Escalate%';

最佳答案

有两种方法可以解决此问题,可以使用联合查询

select 'Undet' as trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Undet%'
union all
select 'Escalate', COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Escalate%'
union all
select 'Not Supported', COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Not Supported%'
union all
select 'Supported', COUNT(*) as counter from jeremy_table_trend WHERE trendx NOT LIKE '%Not Supported%' OR '%Undet%' OR '%Escalate%';


或者,您也可以使用case语句和子查询,例如

select status, count(*) from (
  select
   case
    when trendx like '%Undet%' then 'Undet'
    when trendx like '%Escalate%' then 'Escalate'
    when trendx LIKE '%Not Supported%' then 'Not Supported'
    else 'Supported'
   end as status
) x group by status

关于php - 选择计数字符串,然后将它们分组为饼图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53208816/

10-16 13:17