Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ordered list to demo dashboard + fix pie chart #539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions demo/app/Sharp/Dashboard/DemoDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Sharp\Dashboard;

use App\Models\Category;
use App\Models\User;
use App\Sharp\Dashboard\Commands\ExportStatsAsCsvCommand;
use App\Sharp\Utils\Filters\PeriodRequiredFilter;
Expand All @@ -17,9 +16,11 @@
use Code16\Sharp\Dashboard\Widgets\SharpFigureWidget;
use Code16\Sharp\Dashboard\Widgets\SharpGraphWidgetDataSet;
use Code16\Sharp\Dashboard\Widgets\SharpLineGraphWidget;
use Code16\Sharp\Dashboard\Widgets\SharpOrderedListWidget;
use Code16\Sharp\Dashboard\Widgets\SharpPieGraphWidget;
use Code16\Sharp\Dashboard\Widgets\WidgetsContainer;
use Code16\Sharp\Utils\Links\LinkToEntityList;
use Code16\Sharp\Utils\Links\LinkToShowPage;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -60,6 +61,13 @@ protected function buildWidgets(WidgetsContainer $widgetsContainer): void
->setMinimal()
->setCurvedLines(),
)
->addWidget(
SharpOrderedListWidget::make('top_categories_list')
->setTitle('Most used categories')
->buildItemLink(function ($item) {
return LinkToShowPage::make('categories', $item['id']);
})
)
->addWidget(
SharpFigureWidget::make('draft_panel')
->setTitle('Draft posts')
Expand Down Expand Up @@ -89,6 +97,9 @@ protected function buildDashboardLayout(DashboardLayout $dashboardLayout): void
$row->addWidget(6, 'authors_bar')
->addWidget(6, 'categories_pie');
})
->addRow(function (DashboardLayoutRow $row) {
$row->addWidget(12, 'top_categories_list');
})
->addFullWidthWidget('visits_line');
});
}
Expand Down Expand Up @@ -124,6 +135,7 @@ protected function buildWidgetsData(): void
$this->setPieGraphDataSet();
$this->setBarsGraphDataSet();
$this->setLineGraphDataSet();
$this->setOrderedListDataset();

$posts = DB::table('posts')
->select(DB::raw('state, count(*) as count'))
Expand Down Expand Up @@ -193,19 +205,23 @@ protected function setBarsGraphDataSet()

protected function setPieGraphDataSet(): void
{
Category::withCount([
'posts' => function (Builder $query) {
$query->whereBetween('published_at', [$this->getStartDate(), $this->getEndDate()]);
}, ])
->limit(8)
->orderBy('posts_count')
DB::table('posts')
->selectRaw('categories.name as category_name, count(*) as post_count')
->join('category_post', 'post_id', '=', 'posts.id')
->join('categories', 'category_id', '=', 'categories.id')
->whereBetween('published_at', [
$this->getStartDate(),
$this->getEndDate(),
])
->groupBy('category_id')
->orderBy('post_count', 'desc')
->limit(5)
->get()
->each(function (Category $category) {
->each(function ($result) {
$this->addGraphDataSet(
'categories_pie',
SharpGraphWidgetDataSet::make([$category->posts_count])
->setLabel($category->name)
SharpGraphWidgetDataSet::make([$result->post_count])
->setLabel($result->category_name)
->setColor(static::nextColor()),
);
});
Expand All @@ -232,4 +248,32 @@ protected function getEndDate(): Carbon
today()->subDay(),
);
}

private function setOrderedListDataset()
{
$this->setOrderedListData(
'top_categories_list',
DB::table('posts')
->selectRaw('category_id, categories.name as category_name, count(*) as post_count')
->join('category_post', 'post_id', '=', 'posts.id')
->join('categories', 'category_id', '=', 'categories.id')
->whereBetween('published_at', [
$this->getStartDate(),
$this->getEndDate(),
])
->groupBy('category_id')
->orderBy('post_count', 'desc')
->limit(5)
->get()
->map(function ($data) {
return [
'label' => $data->category_name,
'count' => (int) $data->post_count,
'id' => $data->category_id,
];
})
->values()
->toArray(),
);
}
}
Loading