danielpetrica/svg-charts

A PHP library to create svg charts for your data. No external js, no external dependancies just plain svg images

dev-main 2025-05-12 21:58 UTC

This package is auto-updated.

Last update: 2025-05-12 21:59:03 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A PHP library to create SVG charts for your data. No external JavaScript, no external dependencies - just plain SVG images.

Features

  • Generates bar charts in SVG format based on array data
  • Each bar represents a value (count) associated with a subject, grouped by time intervals
  • Minimal graphical representations without external dependencies or JavaScript libraries
  • Output can be passed to Blade for on-screen display
  • Built-in color scheme with automatic color generation

Security Note: For saving SVG as an image, security checks are delegated to the user. Make sure to perform all necessary security checks!

Installation

Install the package via composer:

composer require danielpetrica/svg-charts

Publish and run migrations:

php artisan vendor:publish --tag="svg-charts-migrations"
php artisan migrate

Publish the config file:

php artisan vendor:publish --tag="svg-charts-config"

Config file contents (config/svg-charts.php):

return [
    // Configuration options will go here
];

Optionally publish views:

php artisan vendor:publish --tag="svg-charts-views"

Usage

Data Format Requirements

Data must be provided as an array of associative arrays with these keys:

[
    'subject'   => (string) Subject/category name,
    'timeSlice' => (string) Time interval (e.g., "January", "2024-04"),
    'count'     => (int) The numeric value to display
]

Example dataset:

$data = [
    ['subject' => 'Product A', 'timeSlice' => 'January', 'count' => 25],
    ['subject' => 'Product B', 'timeSlice' => 'January', 'count' => 15],
    ['subject' => 'Product A', 'timeSlice' => 'February', 'count' => 30],
    ['subject' => 'Product B', 'timeSlice' => 'February', 'count' => 22],
];

Basic Usage

use DanielPetrica\SvgCharts\SvgCharts;

// Create chart instance
$chart = new SvgCharts($data, 'Monthly Sales');

// Set dimensions (width, height in pixels)
$chart->setDimensions(800, 400);

// Output to screen
echo $chart->render();

// Save to file
$chart->renderToFile('chart.svg');

Laravel Blade Integration

<div class="chart-container">
    {!! $chart->render() !!}
</div>

Customizing Colors

// Set custom colors for subjects
$chart->setColors([
    'Product A' => '#FF5733',
    'Product B' => '#33FF57'
]);

Testing

Run the test suite:

composer test

Changelog

See CHANGELOG for version history.

Contributing

Contributions welcome! See CONTRIBUTING for guidelines.

Security

Please report vulnerabilities via our security policy.

Credits

License

MIT License. See LICENSE for details.


This markdown file includes:

1. Header with badges
2. Clear feature list
3. Security notice
4. Installation instructions
5. Usage examples with code blocks
6. Data format requirements
7. Testing information
8. Standard open-source sections (Changelog, Contributing, Security, Credits, License)

The formatting uses proper markdown syntax for:
- Headers (`#`, `##`, `###`)
- Code blocks (```)
- Lists (`-`)
- Emphasis (`**`)
- Links (`[text](url)`)
- Escape characters where needed

The content is organized logically from high-level overview to specific implementation details.