Reputation:
I have a login/logout application which I created using Laravel 4. I need to make a web statistics so whenever the user login it will show me a line graph with the amount of users within a day.
Below is my login method from which I save data in the database whenever a user login.
public function postLogin() {
$input = Input::all();
$rules = array('email' => 'required', 'password' => 'required');
$v = Validator::make($input, $rules);
if ($v->fails()) {
return Redirect::to('login')->withErrors($v);
} else {
$credentials = array('email' => $input['email'], 'password' => $input['password']);
if (Auth::attempt($credentials)) {
$id = Auth::user()->id;
$auditor = new Auditor();
$auditor->user_id = $id;
$auditor->operation = 'login';
$auditor->save();
return Redirect::to('admin');
} else {
return View::make('home.login', array('message' => 'Wrong username/password!'));
}
}
}
I have a controller where I group my data from database. I know it is not correct but I don't know how to continue to create the graph.
public function graph() {
$auditor = new Auditor();
$auditor = array();
//get data from database
$sql = "SELECT DATE('auditor'.'created') AS date" . "COUNT('auditor'.'auditor_id') AS 'count'". "FROM 'auditor'".
"WHERE auditor .created BETWEEN 2015-09-01 00:00:00 AND 2015-09-31 23:59:59". "GROUP BY date"."ORDER BY date";
$result = mysqli_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
}
}
Please can you help me how to draw a graph and is it possible to display data in the graph after grouped?
Upvotes: 0
Views: 2077
Reputation: 4167
I second phpChart. Used it in the past for an online report task. Very easy to create charts quickly.
Here's the solution to your scenario using phpChart based on their online example -- Axis Labels Rotated Text 2:
<?php
$line = array(array('user', 12), array('admin', 3), array('user2', 1), array('user3', 1));
$pc = new C_PhpChartX(array($line),'user_chart');
$pc->add_plugins(array('canvasTextRenderer'));
//set series
$pc->add_series(array('renderer'=>'plugin::BarRenderer'));
//set axes
$pc->set_axes(array(
'xaxis' => array(
'renderer'=>'plugin::CategoryAxisRenderer',
'tickRenderer'=>'plugin::CanvasAxisTickRenderer'),
'yaxis' => array(
'autoscale'=>true,
'tickRenderer'=>'plugin::CanvasAxisTickRenderer')
));
$pc->draw(800,500);
?>
Here's a great intro on Codeproject I found: http://www.codeproject.com/Articles/604542/Creating-Interactive-HTML5-Graphs-in-PHP
Laravel doesn't provides any charting library out of the box. You need to find a third party library written in PHP to generate charts from your Laravel app.
Some free library for charting are:
I strongly encourage you to search for php charting library here in StackOverflow to see the opinion from other experienced users.
When choosing a particular library add it to you composer.json file as any other dependency.
Upvotes: 1
Reputation: 1971
Once you get the data from PHP you can use some javascript charting tool like Chart.js.
Upvotes: 1