mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-11 13:57:41 -08:00
Fixed dashboard chart
This commit is contained in:
parent
06d66f6e94
commit
bc5fcf8736
|
@ -6,6 +6,7 @@ use Illuminate\Http\Request;
|
|||
use App\Http\Controllers\Controller;
|
||||
use App\Helpers\Helper;
|
||||
use App\Models\Statuslabel;
|
||||
use App\Models\Asset;
|
||||
|
||||
class StatuslabelsController extends Controller
|
||||
{
|
||||
|
@ -100,4 +101,47 @@ class StatuslabelsController extends Controller
|
|||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/statuslabels/message.delete.success')));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Show a count of assets by status label for pie chart
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v3.0]
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
|
||||
public function getAssetCountByStatuslabel()
|
||||
{
|
||||
|
||||
$statusLabels = Statuslabel::with('assets')->get();
|
||||
$labels=[];
|
||||
$points=[];
|
||||
$colors=[];
|
||||
foreach ($statusLabels as $statusLabel) {
|
||||
if ($statusLabel->assets()->count() > 0) {
|
||||
$labels[]=$statusLabel->name;
|
||||
$points[]=$statusLabel->assets()->whereNull('assigned_to')->count();
|
||||
if ($statusLabel->color!='') {
|
||||
$colors[]=$statusLabel->color;
|
||||
}
|
||||
}
|
||||
}
|
||||
$labels[]='Deployed';
|
||||
$points[]=Asset::whereNotNull('assigned_to')->count();
|
||||
|
||||
$colors_array = array_merge($colors, Helper::chartColors());
|
||||
|
||||
$result= [
|
||||
"labels" => $labels,
|
||||
"datasets" => [ [
|
||||
"data" => $points,
|
||||
"backgroundColor" => $colors_array,
|
||||
"hoverBackgroundColor" => $colors_array
|
||||
]]
|
||||
];
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,43 +37,6 @@ class StatuslabelsController extends Controller
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show a count of assets by status label
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getAssetCountByStatuslabel()
|
||||
{
|
||||
|
||||
$statusLabels = Statuslabel::with('assets')->get();
|
||||
$labels=[];
|
||||
$points=[];
|
||||
$colors=[];
|
||||
foreach ($statusLabels as $statusLabel) {
|
||||
if ($statusLabel->assets()->count() > 0) {
|
||||
$labels[]=$statusLabel->name;
|
||||
$points[]=$statusLabel->assets()->whereNull('assigned_to')->count();
|
||||
if ($statusLabel->color!='') {
|
||||
$colors[]=$statusLabel->color;
|
||||
}
|
||||
}
|
||||
}
|
||||
$labels[]='Deployed';
|
||||
$points[]=Asset::whereNotNull('assigned_to')->count();
|
||||
|
||||
$colors_array = array_merge($colors, Helper::chartColors());
|
||||
|
||||
$result= [
|
||||
"labels" => $labels,
|
||||
"datasets" => [ [
|
||||
"data" => $points,
|
||||
"backgroundColor" => $colors_array,
|
||||
"hoverBackgroundColor" => $colors_array
|
||||
]]
|
||||
];
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -147,15 +147,34 @@
|
|||
var pieChart = new Chart(pieChartCanvas);
|
||||
var ctx = document.getElementById("statusPieChart");
|
||||
|
||||
$.get('{{ route('api.statuslabels.assets') }}', function (data) {
|
||||
var myPieChart = new Chart(ctx,{
|
||||
|
||||
type: 'doughnut',
|
||||
data: data,
|
||||
options: pieOptions
|
||||
});
|
||||
// document.getElementById('my-doughnut-legend').innerHTML = myPieChart.generateLegend();
|
||||
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: '{{ route('api.statuslabels.assets.bytype') }}',
|
||||
headers: {
|
||||
"X-Requested-With": 'XMLHttpRequest',
|
||||
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
|
||||
},
|
||||
|
||||
dataType: 'json',
|
||||
success: function (data) {
|
||||
var myPieChart = new Chart(ctx,{
|
||||
|
||||
type: 'doughnut',
|
||||
data: data,
|
||||
options: pieOptions
|
||||
});
|
||||
},
|
||||
error: function (data) {
|
||||
// AssetRequest Validator will flash all errors to session, this just refreshes to see them.
|
||||
window.location.reload(true);
|
||||
// console.log(JSON.stringify(data));
|
||||
// console.log('error submitting');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<script src="{{ asset('assets/js/bootstrap-table.js') }}"></script>
|
||||
|
|
|
@ -112,6 +112,31 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
]
|
||||
);
|
||||
|
||||
|
||||
/*---Status Label API---*/
|
||||
Route::group([ 'prefix' => 'statuslabels'], function () {
|
||||
|
||||
|
||||
Route::get('{id}/deployable', function ($statuslabelId) {
|
||||
|
||||
$statuslabel = Statuslabel::find($statuslabelId);
|
||||
if (( $statuslabel->deployable == '1' ) && ( $statuslabel->pending != '1' )
|
||||
&& ( $statuslabel->archived != '1' )
|
||||
) {
|
||||
return '1';
|
||||
} else {
|
||||
return '0';
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// Pie chart for dashboard
|
||||
|
||||
Route::get('assets', [ 'as' => 'api.statuslabels.assets.bytype', 'uses' => 'StatuslabelsController@getAssetCountByStatuslabel' ]);
|
||||
|
||||
});
|
||||
|
||||
|
||||
Route::resource('statuslabels', 'StatuslabelsController',
|
||||
['names' =>
|
||||
[
|
||||
|
@ -124,6 +149,10 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
]
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Route::resource('consumables', 'ConsumablesController',
|
||||
['names' =>
|
||||
[
|
||||
|
@ -188,30 +217,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
|
||||
|
||||
|
||||
/*---Status Label API---*/
|
||||
Route::group([ 'prefix' => 'statuslabels' ,'middleware' => ['authorize:admin']], function () {
|
||||
|
||||
Route::resource('statuslabels', 'StatuslabelsController', [
|
||||
'parameters' => ['statuslabel' => 'statuslabel_id']
|
||||
]);
|
||||
|
||||
Route::get('{statuslabelId}/deployable', function ($statuslabelId) {
|
||||
|
||||
$statuslabel = Statuslabel::find($statuslabelId);
|
||||
if (( $statuslabel->deployable == '1' ) && ( $statuslabel->pending != '1' )
|
||||
&& ( $statuslabel->archived != '1' )
|
||||
) {
|
||||
return '1';
|
||||
} else {
|
||||
return '0';
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Route::get('list', [ 'as' => 'api.statuslabels.list', 'uses' => 'StatuslabelsController@getDatatable' ]);
|
||||
Route::get('assets', [ 'as' => 'api.statuslabels.assets', 'uses' => 'StatuslabelsController@getAssetCountByStatuslabel' ]);
|
||||
|
||||
});
|
||||
|
||||
/*---Accessories API---*/
|
||||
Route::group([ 'prefix' => 'accessories' ], function () {
|
||||
|
|
Loading…
Reference in a new issue