Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix js issue with displaying multiple dashboard charts
- Loading branch information
Showing
6 changed files
with
129 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Admin\Traits; | ||
|
||
use Carbon\Carbon; | ||
use DateInterval; | ||
use DatePeriod; | ||
use DB; | ||
use Illuminate\Support\Collection; | ||
|
||
trait HasChartDatasets | ||
{ | ||
public function loadAssets() | ||
{ | ||
$this->addJs('~/app/system/assets/ui/js/vendor/moment.min.js', 'moment-js'); | ||
$this->addJs('~/app/admin/dashboardwidgets/charts/assets/vendor/daterange/daterangepicker.js', 'daterangepicker-js'); | ||
$this->addCss('~/app/admin/dashboardwidgets/charts/assets/vendor/daterange/daterangepicker.css', 'daterangepicker-css'); | ||
|
||
$this->addJs('~/app/admin/dashboardwidgets/charts/assets/vendor/chartjs/Chart.min.js', 'chartsjs-js'); | ||
$this->addCss('~/app/admin/dashboardwidgets/charts/assets/css/charts.css', 'charts-css'); | ||
$this->addJs('~/app/admin/dashboardwidgets/charts/assets/js/charts.js', 'charts-js'); | ||
} | ||
|
||
public function onFetchDatasets() | ||
{ | ||
$start = post('start'); | ||
$end = post('end'); | ||
|
||
$start = Carbon::parse($start); | ||
$end = Carbon::parse($end); | ||
|
||
return $this->getDatasets($start, $end); | ||
} | ||
|
||
protected function getDatasets($start, $end) | ||
{ | ||
$config = []; | ||
$results[] = $this->makeDataset($config, $start, $end); | ||
|
||
return $results; | ||
} | ||
|
||
protected function makeDataset($config, $start, $end) | ||
{ | ||
list($r, $g, $b) = sscanf($config['color'], '#%02x%02x%02x'); | ||
$backgroundColor = sprintf('rgba(%s, %s, %s, 0.5)', $r, $g, $b); | ||
$borderColor = sprintf('rgb(%s, %s, %s)', $r, $g, $b); | ||
|
||
return array_merge($this->datasetOptions, [ | ||
'label' => lang($config['label']), | ||
'data' => $this->queryDatasets($config, $start, $end), | ||
'backgroundColor' => $backgroundColor, | ||
'borderColor' => $borderColor, | ||
]); | ||
} | ||
|
||
protected function queryDatasets($config, $start, $end) | ||
{ | ||
$modelClass = $config['model']; | ||
$dateColumnName = $config['column']; | ||
|
||
$dateColumn = DB::raw('DATE_FORMAT('.$dateColumnName.', "%Y-%m-%d") as x'); | ||
$query = $modelClass::select($dateColumn, DB::raw('count(*) as y')); | ||
$query->whereBetween($dateColumnName, [$start, $end])->groupBy('x'); | ||
|
||
$dateRanges = $this->getDatePeriod($start, $end); | ||
|
||
return $this->getPointsArray($dateRanges, $query->get()); | ||
} | ||
|
||
protected function getDatePeriod($start, $end) | ||
{ | ||
return new DatePeriod( | ||
Carbon::parse($start), | ||
new DateInterval('P1D'), | ||
Carbon::parse($end) | ||
); | ||
} | ||
|
||
protected function getPointsArray($dateRanges, Collection $result) | ||
{ | ||
$points = []; | ||
$keyedResult = $result->keyBy('x'); | ||
foreach ($dateRanges as $date) { | ||
$x = $date->format('Y-m-d'); | ||
$points[] = [ | ||
'x' => $x, | ||
'y' => $keyedResult->get($x) ?? 0, | ||
]; | ||
} | ||
|
||
return $points; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters