简体   繁体   English

来自 PHP 的流程图

[英]Flot Chart from PHP

I'm trying to have a flot chart with values from a database.我正在尝试使用数据库中的值创建一个流程图。 How I have it now, the chart only plots 1 datapoint.我现在如何拥有它,图表只绘制了 1 个数据点。 How can I get it to plot the rest of the data from the array?我怎样才能让它绘制数组中的其余数据? I'm trying to plot the total count of agents that were entered on each date.我试图绘制在每个日期输入的代理总数。 var data2 will be for agents lost var data2 将用于代理丢失

<?php

      foreach($mysqli->query('SELECT COUNT(*), agent_date_entered, agent_id 
      FROM agents GROUP BY agent_date_entered 
      ORDER BY agent_date_entered') as $row) {

        $count[] = $row['COUNT(*)'];
        $agent[] = $row['agent_id'];
        $date[] = $row['agent_date_entered'];

      }
?>

<script>
    var data, data1, options, chart;
    var data1 = [ <?php echo json_encode($count); ? > , < ? php echo json_encode($date); ?> ];
    var data2 = [];

    data = [{
        data: data1,
        label: "Agents Gained",
        lines: {
            show: true
        },
        points: {
            show: true
        }
    }, {
        data: data2,
        label: "Agents Lost",
        lines: {
            show: true
        },
        points: {
            show: true
        }
    }];
    options = {
        legend: {
            position: "nw"
        }
    };
    $(document).ready(function () {
        chart = $.plot($("#placeholder"), data, options);
    });
</script>

Your data format is not what Flot requires.您的数据格式不是 Flot 所要求的。 You need something like你需要类似的东西

[[1, 1533081600000], [1, 1535932800000]]

(or [[1533081600000, 1], [1535932800000, 1]] if you want the dates on the x axis). (或[[1533081600000, 1], [1535932800000, 1]]如果您想要 x 轴上的日期)。
The numbers likes 1535932800000 are JavaScript timestamps.1535932800000这样的数字是 JavaScript 时间戳。 You also need the option mode: 'time' on the axis with the date/time values.您还需要选项mode: 'time'带有日期/时间值的轴上的mode: 'time'

For more info see the documentation under Data format and Time series data .有关更多信息,请参阅数据格式时间序列数据下的文档。

With the help of the links in one of the comments to Data format and Time series data , I found the answer I was looking for.借助Data formatTime series data评论之一中的链接,我找到了我正在寻找的答案。

Just in case anyone else is having this issue, here is how I got it to work.以防万一其他人遇到这个问题,这是我如何让它工作的。 First, I changed the date in the sql statement to read: (UNIX_TIMESTAMP(agent_date_depart)*1000) AS date .首先,我将 sql 语句中的日期更改为: (UNIX_TIMESTAMP(agent_date_depart)*1000) AS date Then I encoded it when I set the variable $depart = json_encode(floatval($row['date']));然后我在设置变量$depart = json_encode(floatval($row['date']));时对其进行编码$depart = json_encode(floatval($row['date'])); . . To make it so it's not a huge number on the x-axis and displays as a date, I set the x-axis like this: xaxis: {mode: "time", timeformat: "%Y/%m/%d"}, .为了使它在 x 轴上不是一个巨大的数字并显示为日期,我将 x 轴设置如下: xaxis: {mode: "time", timeformat: "%Y/%m/%d"}, . You can even set the date format in the tooltip by using tooltipOpts like this:您甚至可以使用 tooltipOpts 在工具提示中设置日期格式,如下所示:

    tooltipOpts: {
        content: function (label, x, y) {
            var date = new Date(+x);
            var tooltip = '<h4>' + label + '</h4><ul>';
            tooltip += '<li>Date ' + date.toLocaleDateString() + '</li>';
            tooltip += '<li>Total Count: ' + y + '</li></ul>';
            return tooltip;
        },

The easiest way I found to get the data to display inside the chart, I put the sql statement inside the data variable like this:我发现让数据显示在图表中的最简单方法是,我将 sql 语句放在 data 变量中,如下所示:

        var d2 = [

<?php

         $sql = "SELECT COUNT(agent_id), (UNIX_TIMESTAMP(agent_date_depart)*1000) AS date FROM agents
          WHERE agent_date_depart BETWEEN '$bdatecy' AND '$edatecy'
          GROUP BY agent_date_depart ";
          $result = mysqli_query($mysqli, $sql);
           while ($row = mysqli_fetch_assoc($result)) {

    $agent = $row['COUNT(agent_id)'];
    $depart = json_encode(floatval($row['date']));

    echo "[".$depart.", ".$agent."],";
  }

?>

  ];

Hope this helps and I wanted to say thank you again for those links.希望这会有所帮助,我想再次感谢您提供这些链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM