#3: Added a quick and simple pie chart of cameras used in the gallery. Added an image to the "Albums" menu item.

This commit is contained in:
Andy Heathershaw 2017-09-06 16:08:38 +01:00
parent 34b2ff6ea4
commit f5a269d634
6 changed files with 143 additions and 3 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace App\Http\Controllers\Gallery;
use App\Facade\Theme;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class StatisticsController extends Controller
{
public function camerasChart(Request $request)
{
$stats = DB::table('photos')
->where([
['camera_make', '!=', ''],
['camera_model', '!=', '']
])
->groupBy('camera_make', 'camera_model')
->select('camera_make', 'camera_model', DB::raw('count(*) as photo_count'))
->orderBy('photo_count', 'desc')
->get();
$labels = [];
$data = [];
foreach ($stats as $stat)
{
// Remove the model from the make if it starts with it
// E.g. CANON - CANON EOS 1200D becomes just CANON EOS 1200D
if (substr($stat->camera_model, 0, strlen($stat->camera_make)) == $stat->camera_make)
{
$stat->camera_make = substr($stat->camera_make, strlen($stat->camera_make));
}
$labels[] = sprintf('%s %s', $stat->camera_make, $stat->camera_model);
$data[] = $stat->photo_count;
}
return response()->json([
'labels' => $labels,
'backgrounds' => $this->rotateColoursForData($data),
'data' => $data
]);
}
public function index(Request $request)
{
return Theme::render('gallery.statistics');
}
private function rotateColoursForData(array $data = [])
{
$colours = ['#0F2240', '#174E79', '#287598', '#46BBB5', '#35DCAD'];
$result = [];
$lastIndex = 0;
for ($i = 0; $i < count($data); $i++)
{
$result[] = $colours[$lastIndex];
$lastIndex++;
if ($lastIndex >= count($colours))
{
$lastIndex = 0;
}
}
return $result;
}
}

View File

@ -14,5 +14,10 @@ return [
'manage_album_link_2' => 'Manage Album',
'open_album_link' => 'Open Album',
'other_albums_description' => 'You may also be interested in the following albums.',
'other_albums_heading' => 'More Albums in :album_name'
'other_albums_heading' => 'More Albums in :album_name',
'statistics' => [
'cameras' => 'Cameras Used',
'intro' => 'This page displays some interesting graphs and statistics about :gallery_name.',
'title' => 'Statistics'
]
];

View File

@ -27,6 +27,7 @@ return [
'change_password' => 'Change password',
'login' => 'Login',
'logout' => 'Logout',
'register' => 'Register'
'register' => 'Register',
'statistics' => 'Statistics'
]
];

View File

@ -0,0 +1,59 @@
@extends('themes.base.layout')
@section('title', trans('gallery.statistics.title'))
@section('breadcrumb')
<li class="breadcrumb-item"><a href="{{ route('home') }}"><i class="fa fa-fw fa-home"></i></a></li>
<li class="breadcrumb-item active">@lang('gallery.statistics.title')</li>
@endsection
@section('content')
<div class="container">
<div class="row">
<div class="col">
<h1>@lang('gallery.statistics.title')</h1>
<div class="alert alert-info" style="margin-bottom: 30px;">
<i class="fa fa-fw fa-info"></i> @lang('gallery.statistics.intro', ['gallery_name' => UserConfig::get('app_name')])
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">@lang('gallery.statistics.cameras')</div>
<div class="card-body text-center" id="cameras-graph">
<canvas id="cameras-chart" style="display: none;"></canvas>
<img class="loading" src="{{ asset('ripple.svg') }}"/>
</div>
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
{{-- TODO: include ChartJS locally --}}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.get('{{ route('statistics.cameras') }}', function(result)
{
$('.loading', '#cameras-graph').hide();
$('canvas', '#cameras-graph').show();
var myPieChart = new Chart($('#cameras-chart'),
{
type: 'pie',
data: {
datasets: [
{
backgroundColor: result.backgrounds,
data: result.data
}
],
labels: result.labels
}
});
});
});
</script>
@endpush

View File

@ -9,7 +9,7 @@
@if (count($albums) > 0)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="{{ url('/') }}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
@lang('navigation.navbar.albums')
<i class="fa fa-book"></i> @lang('navigation.navbar.albums')
</a>
<div class="dropdown-menu">
@foreach ($albums as $album)
@ -17,6 +17,9 @@
@endforeach
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('statistics.index') }}"><i class="fa fa-bar-chart"></i> @lang('navigation.navbar.statistics')</a>
</li>
@endif
@if (!Auth::guest() && (Auth::user()->can('admin:access')))

View File

@ -69,6 +69,8 @@ Route::get('/activate/{token}', 'Auth\ActivateController@activate')->name('auth.
Route::get('/password/change', 'Auth\ChangePasswordController@showChangePasswordForm')->name('auth.changePassword');
Route::post('/password/change', 'Auth\ChangePasswordController@processChangePassword')->name('auth.processChangePassword');
Route::get('/sitemap.xml', 'Gallery\DefaultController@sitemapXml');
Route::get('/statistics', 'Gallery\StatisticsController@index')->name('statistics.index');
Route::get('/statistics/cameras', 'Gallery\StatisticsController@camerasChart')->name('statistics.cameras');
Route::get('a/{albumUrlAlias}', 'Gallery\AlbumController@index')
->name('viewAlbum')
->where('albumUrlAlias', '.*');