Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Livecharts Laravel Package

matheusmarnt/livecharts

LiveCharts is a reactive chart abstraction for Laravel using a pure PHP fluent API. Build 18 chart types and render via a single Livewire component. Supports ApexCharts and Chart.js with pluggable engines, enabling easy updates without JS boilerplate.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require matheusmarnt/livecharts
    php artisan livecharts:install
    

    This publishes config, assets, and optionally chart stubs.

  2. First Chart:

    use Matheusmarnt\LiveCharts\Facades\LiveCharts;
    
    $chart = LiveCharts::line()
        ->title('Monthly Revenue')
        ->labels(['Jan', 'Feb', 'Mar'])
        ->dataset('2026', [100, 200, 150])
        ->colors(['#3B82F6']);
    
  3. Render in Blade:

    @liveChartsScripts
    <livewire:livecharts :chart="$chart" />
    

Key First Use Case

Create a reactive dashboard where chart data updates dynamically without JavaScript. Example:

// In a Livewire component
public $revenueData = [100, 200, 150];

public function render()
{
    $chart = LiveCharts::line()
        ->title('Revenue')
        ->labels(['Jan', 'Feb', 'Mar'])
        ->dataset('2026', $this->revenueData);

    return view('livewire.dashboard', compact('chart'));
}

Implementation Patterns

1. Fluent Builder Workflow

  • Chain methods for declarative chart configuration:

    LiveCharts::bar()
        ->title('Sales')
        ->labels(['Q1', 'Q2'])
        ->dataset('North', [400, 300])
        ->dataset('South', [200, 150])
        ->stacked()
        ->height(400);
    
  • Reuse charts via class-based builders:

    // app/Charts/RevenueChart.php
    class RevenueChart extends Chart {
        public function __construct() {
            $this->title('Revenue')
                 ->labels(['Jan', 'Feb'])
                 ->dataset('2026', [100, 200]);
        }
    }
    

2. Livewire Integration

  • Bind to Livewire properties:

    // Livewire component
    public $chartData = [100, 200];
    
    public function updatedChartData() {
        $this->chart = LiveCharts::line()
            ->dataset('Revenue', $this->chartData);
    }
    
  • Polling for real-time updates:

    $chart->poll(5000); // Refresh every 5s
    
    <livewire:livecharts :chart="$chart" wire:poll="refresh" />
    

3. Event Handling

  • Map chart events to Livewire:
    $chart->onDataPointClick('chart-clicked');
    
    // Livewire component
    #[On('chart-clicked')]
    public function handleClick(array $data) {
        // $data = ['seriesIndex' => 0, 'value' => 150, ...]
    }
    

4. Multi-Engine Support

  • Switch engines globally (config) or per chart:
    LiveCharts::line()->engine('chartjs')->labels(['Jan']);
    
  • Register custom engines:
    LiveCharts::registerEngine('highcharts', HighchartsAdapter::class);
    

5. Theming & Dark Mode

  • Theme-aware colors:
    ->titleColor(dark: TwColor::Amber300, light: TwColor::Amber600)
    
  • Auto-detect dark mode (no Livewire roundtrip):
    ->theme('auto');
    

6. Broadcasting

  • Push updates via Laravel Echo:
    $chart->broadcastOn('private-charts.user1')->broadcastAs('chart.updated');
    

Gotchas and Tips

Pitfalls

  1. Asset Loading:

    • Issue: @liveChartsScripts must appear before @livewireScripts in your layout.
    • Fix: Place it right after <body> or in <head> if using @extends.
    • Debug: Check browser console for livecharts.js 404 errors.
  2. Polling Conflicts:

    • Issue: Multiple wire:poll on the same component can cause race conditions.
    • Fix: Use unique poll keys or disable polling via wire:poll.ignore="refresh".
  3. Color Roundtrip:

    • Issue: Custom colors may not render as expected in dark mode.
    • Fix: Use TwColor enum for theme-aware colors:
      ->colors([TwColor::Blue500, TwColor::Red400])
      
  4. Engine-Specific Quirks:

    • ApexCharts: Supports all 18 chart types but may need ->options(['...']) for advanced configs.
    • Chart.js: Requires explicit plugin registration for treemap, sankey, etc.:
      LiveCharts::treemap()->engine('chartjs')->plugins(['treemap']);
      
  5. Stub Customization:

    • Issue: Default make:chart stubs may not match your project’s PSR-12 style.
    • Fix: Override stubs by publishing them first:
      php artisan vendor:publish --tag=livecharts-stubs --force
      
      Then edit stubs/livecharts/chart.stub.

Debugging Tips

  1. Preview All Charts:

    php artisan livecharts:preview
    

    Opens a gallery of every chart type for visual debugging.

  2. Log Chart Payload: Add this to your Livewire component to inspect the serialized chart:

    public function mount() {
        \Log::info('Chart payload:', $this->chart->toArray());
    }
    
  3. Check Engine Compatibility:

    • Run php artisan livecharts:install --force to ensure assets are up to date.
    • Verify engine support with:
      LiveCharts::availableEnginesForType('line'); // ['apexcharts', 'chartjs']
      
  4. Dark Mode Issues:

    • Ensure your layout has <html class="dark"> or prefers-color-scheme: dark in CSS.
    • Test with:
      ->theme('dark')->titleColor(TwColor::Slate500);
      

Extension Points

  1. Custom Engines: Implement Matheusmarnt\LiveCharts\Contracts\EngineAdapter and register it:

    LiveCharts::registerEngine('my-engine', MyEngineAdapter::class);
    
  2. Chart Modifiers: Extend the Chart class to add domain-specific methods:

    class FinancialChart extends Chart {
        public function addCandlestickSeries(string $label, array $data) {
            $this->dataset($label, $data)->type('candlestick');
            return $this;
        }
    }
    
  3. Asset Customization: Override asset paths in config/livecharts.php:

    'assets' => [
        'mode' => 'local', // 'cdn', 'both'
        'path' => 'custom/path/to/livecharts',
    ],
    
  4. Translation Overrides: Publish translations and extend them:

    php artisan vendor:publish --tag=livecharts-translations
    

    Edit resources/lang/en/livecharts.php.


Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony