Charts & Data Visualization
Dashboard Laravel uses Chart.js for creating beautiful, responsive charts. The dashboard includes four main chart types: line, bar, pie, and doughnut charts for various data visualization needs.Setup
Chart.js is loaded via CDN in the main layout file:resources/views/layouts/app.blade.php
Chart.js is automatically available on all dashboard pages. No additional installation is required.
Chart Types
Dashboard Laravel implements four chart types used across different pages:Line Chart
Used for displaying trends over time, such as monthly sales data.- Implementation
- JavaScript
- Full Example
resources/views/estadisticas.blade.php
Bar Chart
Ideal for comparing quantities across categories, such as product sales.resources/views/estadisticas.blade.php
Setting
indexAxis: 'y' creates horizontal bar charts, which work well for displaying ranked data like product sales.Pie Chart
Perfect for showing percentage distribution across categories.resources/views/estadisticas.blade.php
Doughnut Chart
Similar to pie charts but with a center cutout, often used for demographic data.resources/views/estadisticas.blade.php
Complete Page Example
Here’s a full example from the statistics page with multiple charts:resources/views/estadisticas.blade.php
Chart Configuration Options
Common Options
Line Chart Options
Bar Chart Options
Color Schemes
Use colors from the design system for consistent branding:Using the design system colors ensures charts match the overall dashboard aesthetic.
Dynamic Data with Laravel
Pass data from your controller to charts:Controller
app/Http/Controllers/DashboardController.php
View
resources/views/estadisticas.blade.php
Use
@json() directive to safely pass PHP arrays to JavaScript. This escapes data properly and prevents XSS vulnerabilities.Multiple Datasets
Compare multiple data series on a single chart:Best Practices
- Place scripts in @section(‘scripts’): Keep chart initialization code in the scripts section
- Use unique canvas IDs: Each chart needs its own unique
<canvas>element - Set explicit height: Use
height="200"on canvas for consistent sizing - Enable responsive: Always set
responsive: truein options - Match design system: Use colors from
dashboard.cssvariables - Sanitize data: Use
@json()for passing Laravel data to JavaScript - Hide unnecessary legends: For single-dataset bar charts, hide the legend
- Format tooltips: Customize tooltips to display data appropriately (currency, percentages)
Advanced Features
Mixed Chart Types
Custom Tooltips
Troubleshooting
Chart not rendering
Chart not rendering
- Ensure Chart.js is loaded before your script
- Check that the canvas element ID matches the JavaScript selector
- Verify the canvas is inside a visible element
- Check browser console for JavaScript errors
Chart too small/large
Chart too small/large
- Set explicit
heightattribute on canvas element - Use
maintainAspectRatio: falsein options - Ensure parent container has defined dimensions
Data not updating
Data not updating
- Use
chart.update()after changing data - Or destroy and recreate:
chart.destroy(); new Chart(...)
Colors not showing
Colors not showing
- Ensure backgroundColor/borderColor are properly formatted RGBA
- Check for CSS conflicts overriding canvas styles
Resources
Next Steps
Blade Templates
Master Blade templating for building views
Design System
Explore the Soft UI design system
