Logo Beartropy Charts
Example Queries
Learn how to query your database using Eloquent to prepare data for your charts.

Bar Chart Examples

Users by Month

This example groups users by registration month for the current year.

1// Controller
2public function getUsersByMonth()
3{
4 $data = User::selectRaw('MONTH(created_at) as month, COUNT(*) as count')
5 ->whereYear('created_at', date('Y'))
6 ->groupBy('month')
7 ->orderBy('month')
8 ->get();
9 
10 return [
11 'labels' => $data->pluck('month')->map(fn($m) => date('F', mktime(0, 0, 0, $m, 1)))->toArray(),
12 'values' => $data->pluck('count')->toArray(),
13 ];
14}

Sales by Category

This example sums the total sales amount for each product category.

1// Controller
2public function getSalesByCategory()
3{
4 $data = Order::selectRaw('category_id, SUM(total_amount) as total')
5 ->with('category')
6 ->groupBy('category_id')
7 ->get();
8 
9 return [
10 'labels' => $data->pluck('category.name')->toArray(),
11 'values' => $data->pluck('total')->toArray(),
12 ];
13}

Line Chart Examples

Revenue Over Time

This example calculates daily revenue for the last 30 days.

1// Controller
2public function getRevenueOverTime()
3{
4 $data = Order::selectRaw('DATE(created_at) as date, SUM(total_amount) as revenue')
5 ->where('created_at', '>=', now()->subDays(30))
6 ->groupBy('date')
7 ->orderBy('date')
8 ->get();
9 
10 return [
11 'labels' => $data->pluck('date')->toArray(),
12 'values' => $data->pluck('revenue')->toArray(),
13 ];
14}

Active Sessions by Hour

This example counts distinct active sessions per hour for the current day.

1// Controller
2public function getActiveSessionsByHour()
3{
4 // Assuming you have an ActivityLog model
5 $data = ActivityLog::selectRaw('HOUR(created_at) as hour, COUNT(DISTINCT session_id) as active_users')
6 ->whereDate('created_at', now())
7 ->groupBy('hour')
8 ->orderBy('hour')
9 ->get();
10 
11 return [
12 'labels' => $data->pluck('hour')->map(fn($h) => sprintf('%02d:00', $h))->toArray(),
13 'values' => $data->pluck('active_users')->toArray(),
14 ];
15}

Pie Chart Examples

User Roles Distribution

This example counts the number of users for each role.

1// Controller
2public function getUserRolesDistribution()
3{
4 $data = User::selectRaw('role, COUNT(*) as count')
5 ->groupBy('role')
6 ->get();
7 
8 return [
9 'labels' => $data->pluck('role')->toArray(),
10 'values' => $data->pluck('count')->toArray(),
11 ];
12}

Order Status Breakdown

This example shows the distribution of orders by their status.

1// Controller
2public function getOrderStatusBreakdown()
3{
4 $data = Order::selectRaw('status, COUNT(*) as count')
5 ->groupBy('status')
6 ->get();
7 
8 return [
9 'labels' => $data->pluck('status')->toArray(),
10 'values' => $data->pluck('count')->toArray(),
11 ];
12}

Donut Chart Examples

Traffic by Device

This example counts page views by device type for the last 30 days, perfect for center text display.

1// Controller
2public function getTrafficByDevice()
3{
4 $data = PageView::selectRaw('device_type, COUNT(*) as visits')
5 ->whereDate('created_at', '>=', now()->subDays(30))
6 ->groupBy('device_type')
7 ->get();
8 
9 return $data->mapWithKeys(fn($item) => [
10 $item->device_type => $item->visits
11 ])->toArray();
12}

Project Status with Colors

This example groups projects by status and assigns colors based on status type, ideal for donut visualization with center metrics.

1// Controller
2public function getProjectStatusWithTotal()
3{
4 $data = Project::selectRaw('status, COUNT(*) as count')
5 ->groupBy('status')
6 ->get();
7 
8 $formatted = $data->map(fn($item) => [
9 'label' => ucfirst($item->status),
10 'value' => $item->count,
11 'color' => match($item->status) {
12 'completed' => 'green',
13 'in_progress' => 'blue',
14 'pending' => 'yellow',
15 'cancelled' => 'red',
16 default => 'gray'
17 }
18 ])->toArray();
19 
20 return $formatted;
21}

Polar Chart Examples

Department Performance

This example calculates average performance scores per department for radial visualization.

1// Controller
2public function getDepartmentPerformance()
3{
4 $data = Department::selectRaw('name, AVG(performance_score) as avg_score')
5 ->join('performance_reviews', 'departments.id', '=', 'performance_reviews.department_id')
6 ->whereYear('performance_reviews.created_at', date('Y'))
7 ->groupBy('departments.id', 'departments.name')
8 ->get();
9 
10 return $data->mapWithKeys(fn($dept) => [
11 $dept->name => round($dept->avg_score)
12 ])->toArray();
13}

Regional Sales Distribution

This example sums sales by region and assigns custom colors for each region in polar format.

1// Controller
2public function getRegionalSalesDistribution()
3{
4 $data = Sale::selectRaw('region, SUM(amount) as total')
5 ->whereYear('created_at', date('Y'))
6 ->groupBy('region')
7 ->get();
8 
9 return $data->map(fn($item) => [
10 'label' => $item->region,
11 'value' => $item->total,
12 'color' => $this->getRegionColor($item->region)
13 ])->toArray();
14}
15 
16private function getRegionColor($region)
17{
18 return match($region) {
19 'North' => 'blue',
20 'South' => 'green',
21 'East' => 'orange',
22 'West' => 'purple',
23 default => 'gray'
24 };
25}

Radar Chart Examples

Product Evaluation

This example aggregates product review ratings across multiple metrics for spider/web visualization.

1// Controller
2public function getProductEvaluation()
3{
4 $product = Product::with('reviews')->findOrFail($productId);
5 
6 $metrics = [
7 'Speed' => $product->reviews->avg('speed_rating'),
8 'Quality' => $product->reviews->avg('quality_rating'),
9 'Price' => $product->reviews->avg('price_rating'),
10 'Design' => $product->reviews->avg('design_rating'),
11 'Support' => $product->reviews->avg('support_rating'),
12 ];
13 
14 return [
15 [
16 'label' => $product->name,
17 'Speed' => round($metrics['Speed']),
18 'Quality' => round($metrics['Quality']),
19 'Price' => round($metrics['Price']),
20 'Design' => round($metrics['Design']),
21 'Support' => round($metrics['Support']),
22 ]
23 ];
24}

Product Comparison

This example compares multiple products across the same metrics, ideal for multi-dataset radar charts.

1// Controller
2public function compareProducts(array $productIds)
3{
4 $products = Product::with('reviews')
5 ->whereIn('id', $productIds)
6 ->get();
7 
8 $colors = ['blue', 'red', 'green', 'purple', 'orange'];
9 
10 return $products->map(function($product, $index) use ($colors) {
11 return [
12 'label' => $product->name,
13 'color' => $colors[$index] ?? 'gray',
14 'Speed' => round($product->reviews->avg('speed_rating')),
15 'Quality' => round($product->reviews->avg('quality_rating')),
16 'Price' => round($product->reviews->avg('price_rating')),
17 'Design' => round($product->reviews->avg('design_rating')),
18 'Support' => round($product->reviews->avg('support_rating')),
19 ];
20 })->toArray();
21}
Beartropy Logo

© 2026 Beartropy. All rights reserved.

Provided as-is, without warranty. Use at your own risk.