admin管理员组文章数量:1414628
I want to build a statistic graph that shows how many users have registered per day and maybe some other data. I have a MySql table in which I store the date they registered and usernames and etc.
How would I build such a graph ? What do I need for it ?
I want to build a statistic graph that shows how many users have registered per day and maybe some other data. I have a MySql table in which I store the date they registered and usernames and etc.
How would I build such a graph ? What do I need for it ?
Share Improve this question asked Jan 29, 2012 at 13:09 RolandRoland 9,74119 gold badges82 silver badges136 bronze badges 4- You need a php script that can generate images. Either build one yourself, or choose from the many that exist already. A short google for 'php graph' will reveal enough. Check graphpite.sourceforge or jpgraph/download – Konerak Commented Jan 29, 2012 at 13:14
- 1 You know about R right? You can pull your relevant DB data and then graph it in R... – learner Commented Jan 29, 2012 at 13:17
- Oh, I've tried to Google it, but without PHP, I wasn't sure that I should use PHP or JS, but logically I need PHP to read from the db :) – Roland Commented Jan 29, 2012 at 13:18
- Sorry, brainfart on my part. R is a stats language with good graph making functions, but actually I would go with your scripting language. These libraries look like good places to start. – learner Commented Jan 29, 2012 at 13:23
3 Answers
Reset to default 3You don't always need to do things using real graphics.
<?php
// mysql connection setup
// ...
// Get the dates in a single SELECT. 2592000 seconds = 30 days
$result = mysql_query("SELECT regdate FROM users WHERE regdate > NOW()-2592000");
foreach ($row = mysql_fetch_row($result)) {
$output[date("Y-m-d", strtotime($row['regdate']))]++;
}
$fmt = ' <tr><td>%s</td><td width="%s" background="#FF0000"> </td></tr>' . "\n";
?>
<table border="0" cellspacing="0" cellpadding="0"><tr height="100">
<?php
for ($date = time()-2592000; $date < time(); date += 86400) {
$thisdate = date("Y-m-d", $date);
printf($fmt, $thisdate, $output[$thisdate]);
}
?>
</tr></table>
?>
Untested, obviously. Possibly inplete. YMMV. Salt to taste.
There are many ways to build a graph. I can think of a few methods, use one you think best depending on your knowledge.
In every case you need to query your database. So basics of MySQL.
Then you can either create graph on server side by looping trough result set and creating graph by simple HTML divs or using GD library.
Or you can send result set as JSON object and create graph on client side using simple HTML divs or canvas tag.
Server side graphs are much simpler but cant be animated or updated without page refresh. Client side graphs require additional knowledge (JSON, security etc.)
If you only need bar graphs, you might be much better of using div
s of a calculated height, all anchored to a bottom line. This is quite trivial to write and uses a whole lot less of CPU, RAM and bandwidht.
本文标签: phpHow to build a statistics graphStack Overflow
版权声明:本文标题:php - How to build a statistics graph? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745151083a2644923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论