从mysql向高图表的Spline Graph添加值

时间:2016-04-21 13:17:30

标签: php mysqli highcharts

我从查询中选择的行是,

 ## Selected rows from query ##
        id             rank          Date
        2               5           2016-03-15    
        4               3           2016-04-15
        5               2           2016-04-15

我的查询是,

$ touserid = '26'; $ teamcode =“RXqYLM”;

Query = select *,SUM(rank_emotions)为来自assign_emotions的总数,其中to_user_id ='$ touserid'和teamcode ='$ teamcode';

所以,我完全得到了完全但问题是我想在图表中显示数月,所以我已经完成了几个月。

              $date = $row['date'];
              $time = strtotime($date);
              $month = date("F",$time);  

所以从这段代码我得到了一个月的问题是我必须将两者结合起来,我想要用户的月度排名,并在图表中显示。

我试过下面的代码, 但是我想要按月排名这意味着如果一个月内如果用户有多个等级,那么这些值必须是总和,所以在数据字段的例子中,4月份的等级是8。

 <?php 
      include("header.php");
      include("config.php"); 
      $touserid = '26';
      $teamcode = "RXqYLM";
      $name = $_POST['teammembername'];
      if(isset($_POST['teammembername']))
      {
          $touserid = '26';
          $teamcode = "RXqYLM";
          $name = $_POST['teammembername'];

          $res = $conn -> query("select *,SUM(rank_emotions) as total, MONTH(date) as month from assign_emotions where to_user_id='$touserid' and teamcode='$teamcode' GROUP BY MONTH(date)");
          $counter = 0;
           while($row = mysqli_fetch_assoc($res))
          {
            echo $row['month'];
          }
        }
?>    

<!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script src="https://code.highcharts.com/highcharts.js"></script>
        <script src="https://code.highcharts.com/highcharts-more.js"></script>
        <script src="https://code.highcharts.com/modules/exporting.js"></script>
    </head> 
    <body>
    <script>
    $(function () {
        $('#container').highcharts({
            chart: {
                type: 'spline'
            },
            title: {
                text: 'KPI Chart Monthwise'
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'March', 'April', 'May','June', 'July', 'August', 'September', 'October', 'Nov', 'December']
            },
            credits: {
                enabled: false
            },
            series: [{
                name: '<?php echo "bhavin"; ?>',
                data: [<?php if($row['month'] == '1')
                    {
                       echo $row['total'];
                    }
                    else
                    {
                        echo '0';
                    } ?>, 3, 4, 7, 2, 0, 0, 0, 0, -2, 0 ,0]
            }]
        });
    });
    </script>
    <div id="container" style="height: 400px; margin: auto; min-width: 310px; max-width: 600px"></div>
    </body>
    </html>

如果对问题有任何疑问,请告诉我。

1 个答案:

答案 0 :(得分:2)

如果您更改查询以按月分组结果

$res = $conn -> query("
    select *,SUM(rank_emotions) as total, MONTH(date) as month
    from assign_emotions 
    where to_user_id='$touserid' 
      and teamcode='$teamcode'
    GROUP BY MONTH(date)");

这将为您提供每月的总数