chart.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. require("libchart/classes/libchart.php");
  3. require("connection.php");
  4. // error_reporting(E_ALL);
  5. // ini_set("display_errors", 1);
  6. // Opens a connection to a MySQL server
  7. $pdo = new PDO("mysql:host=$dbhost;dbname=$db;charset=utf8mb4", $dbuser, $dbpass);
  8. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  10. if(!isset($_REQUEST['channelId'])) {
  11. echo "no channelId set";
  12. exit(1);
  13. }
  14. $channelId = intval($_REQUEST['channelId']);
  15. $stmtEvents = $pdo->prepare("SELECT MAX(channelEvent.clientCount) AS clientCount, FROM_UNIXTIME( TRUNCATE( UNIX_TIMESTAMP( channelEvent.date)/3600, 0 ) * 3600 ) AS time_slice
  16. FROM channelEvent
  17. LEFT JOIN channel on channel.id = channelEvent.channelId
  18. LEFT JOIN server ON server.id = channel.serverId
  19. WHERE server.uid = ? AND channel.channelId = ? AND date BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW()
  20. GROUP BY time_slice
  21. ORDER BY date ASC LIMIT 1000");
  22. $stmtEvents->execute(Array($serverUid, $channelId));
  23. $stmtChannel = $pdo->prepare("SELECT channel.id, channel.name, channel.channelId, channel.parentId, channel.position, channel.description, server.name as serverName, server.id as serverId FROM channel
  24. LEFT JOIN server ON server.id = channel.serverId
  25. WHERE server.uid = ? AND channel.channelId = ?");
  26. $stmtChannel->execute(Array($serverUid, $channelId));
  27. $events = $stmtEvents->fetchAll(PDO::FETCH_ASSOC);
  28. $channels = $stmtChannel->fetchAll(PDO::FETCH_ASSOC);
  29. if(count($channels) == 0) {
  30. echo "no channel found";
  31. exit(2);
  32. }
  33. $serie1 = new XYDataSet();
  34. foreach($events as $event) {
  35. $date = new DateTime($event['time_slice']);
  36. $serie1->addPoint(new Point($date->format('H:i'), $event['clientCount']));
  37. }
  38. $chart = new LineChart(500, 250);
  39. $chart->setDataSet($serie1);
  40. $chart->setTitle($channels[0]['name']);
  41. header("Content-Type: image/png");
  42. $chart->render();
  43. ?>