davefx/phplot
PHP plotting library (PHPlot) for generating charts and graphs as images. Create line, bar, pie, and area plots with titles, legends, and custom styling. Useful for reports, dashboards, and exporting static graphics server-side.
Installation Since this is a deprecated package, verify if it’s still usable or if a modern alternative (e.g., ChartJS, Laravel Charts, or Livewire Charts) is needed. If proceeding:
composer require davefx/phplot
Note: Ensure PHP extensions like gd or imagick are enabled for image generation.
First Use Case Generate a basic line chart:
use Davefx\Phplot\Phplot;
$plot = new Phplot();
$plot->addData([1, 2, 3, 4, 5]); // Y-axis data
$plot->setTitle("Sample Line Chart");
$plot->render(); // Outputs image to browser or saves to file
Where to Look First
vendor/davefx/phplot/src/ for class methods (e.g., Phplot::addData(), Phplot::setTitle()).tests/ folder (if any).Dynamic Data Binding Fetch data from Laravel models/queries and pass to PHPlot:
$users = User::pluck('created_at')->countBy(function ($date) {
return Carbon\Carbon::parse($date)->format('Y-m');
});
$plot->addData(array_values($users));
Integration with Laravel Views Pass the plot object to a Blade template and render it inline:
return view('charts.dashboard', ['plot' => $plot]);
{!! $plot->render() !!}
Storing Charts as Files
Save generated images to storage/app/public/charts/:
$plot->save('monthly-revenue.png');
Storage::disk('public')->put('charts/monthly-revenue.png', file_get_contents(storage_path('app/monthly-revenue.png')));
Reusable Chart Components Create a service class to encapsulate PHPlot logic:
class ChartService {
public function lineChart(array $data, string $title) {
$plot = new Phplot();
$plot->addData($data);
$plot->setTitle($title);
return $plot;
}
}
{
"chart_url": "/storage/charts/sales.png"
}
$path = cache()->remember("chart_{$cacheKey}", now()->addHours(1), function() use ($plot) {
return $plot->renderToFile();
});
Deprecation Risks
Image Output Issues
sudo apt-get install php-gd # Ubuntu/Debian
sudo pecl install imagick # For Imagick
storage/ and public/ directories are writable:
chmod -R 755 storage public
Memory Limits
memory_limit. Increase it temporarily:
ini_set('memory_limit', '512M');
Legacy Code Quirks
create_function()). Wrap calls in @ to suppress warnings or patch the package.APP_DEBUG=true) and check storage/logs/laravel.log for PHPlot errors.php artisan serve to isolate issues before deploying.try {
return $plot->render();
} catch (\Exception $e) {
return response()->file(public_path('images/chart-fallback.png'));
}
Custom Styling Override PHPlot’s default styles by extending the class:
class CustomPhplot extends Phplot {
public function __construct() {
parent::__construct();
$this->setPlotAreaWorld(0, 0, 100, 100); // Adjust plot boundaries
$this->setPlotAreaFillColor('lightblue');
}
}
Event Hooks PHPlot supports callbacks for pre/post-rendering. Example:
$plot->setPlotCallback(function($plot) {
$plot->setPlotAreaWorld(0, 0, 100, 100);
});
Multi-Chart Layouts Combine multiple PHPlot instances into a single image using GD:
$image = imagecreatetruecolor(800, 400);
$plot1->renderToGD($image, 0, 0, 400, 400);
$plot2->renderToGD($image, 400, 0, 400, 400);
imagepng($image, 'combined_charts.png');
$plot->setFont('title', 'Arial');
$plot->setFontSize('title', 14);
Carbon or PHP’s date.timezone is set to avoid date-label misalignment:
date_default_timezone_set('UTC');
How can I help you explore Laravel packages today?