PHP Classes

How to Use a PHP Chart Generator to Output a Chart from a Few Parameters in the SVG Format using the Class NanoChart: Outputs a pie chart from a values array

Recommend this page to a friend!
  Info   View files Example   Demos   Screenshots Screenshots   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-25 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 1 This week: 1All time: 11,370 This week: 73Up
Version License PHP version Categories
nanochart 1.0MIT/X Consortium ...7.4HTML, Graphics, PHP 7
Description 

Author

This package outputs a pie chart from a values array.

It can take an associative array with text labels and associated values as a parameter.

The package can generate a pie (or doughnut) chart with a legend returning a string with the SVG tags for the generated chart.

It also allows to configure several parameters of the chart:

- Size

- Style

- Direction

- Hue

- Color palette

Picture of Sheldon Kennedy
Name: Sheldon Kennedy <contact>
Classes: 4 packages by
Country: South Africa South Africa

Instructions

https://youtu.be/hplH_LyNPwI

https://github.com/LuniaStudio/NanoChart

Example

<?php

/**
 * Include the NanoChart class in your script.
 **/

require 'src/NanoChart.php';

/**
 * Create an associative array of items and amounts.
 **/

$items = [
   
'Monday' => 10,
   
'Tuesday' => 20,
   
'Wednesday' => 30
];

/**
 * 1. Basic usage.
 **/

$nanoChart = new NanoChart($items);
$html = $nanoChart->build();

/**
 * 2. Set your own colour hue value and NanoChart will create a palette.
 **/

$nanoChart = new NanoChart($items);
$nanoChart->setHue(150);
$html = $nanoChart->build();

/**
 * 3. Pass in your own colour palette.
 **/

$palette = [
   
'hsl(355, 90%, 65%)',
   
'hsl(37, 100%, 70%)',
   
'hsl(140, 70%, 65%)'
];

$nanoChart = new NanoChart($items);
$nanoChart->setPalette($palette);
$html = $nanoChart->build();

/**
 * 4. Set the size of the chart in pixels, excluding the legend.
 **/

$nanoChart = new NanoChart($items);
$nanoChart->setSize(250);
$html = $nanoChart->build();

/**
 * 5. Place the legend to the right or below the chart.
 **/

$nanoChart = new NanoChart($items);
$nanoChart->setDirection('row');
$html = $nanoChart->build();

/**
 * 6. Change the chart from the default 'pie' to 'doughnut'.
 **/

$nanoChart = new NanoChart($items);
$nanoChart->setStyle('doughnut');
$html = $nanoChart->build();

/**
 * 7. Set the slice origin point to the top, right, bottom or left side of the chart.
 **/

$nanoChart = new NanoChart($items);
$nanoChart->setStartPosition('top');
$html = $nanoChart->build();


Details

NanoChart

This PHP class builds a simple pie or doughnut chart as an SVG with a text legend.

Features

  1. Pass in an associative array of items and their amounts, and NanoChart creates the chart.
  2. Pass in your own colour palette to overide the default colours, or set your own hue value and NanoChart will create a palette.
  3. The legend can be to the right or below the chart (using CSS flexbox row or column).
  4. The output is HTML with inline styles.

Screenshot

screenshot

Usage

Prepare an associative array of names and amounts:

    $items = [
        'Monday' => 10,
        'Tuesday' => 20,
        'Wednesday' => 30
    ];

Instantiate NanoChart and pass in the array:

    $nanoChart = new NanoChart($items);

Customise with setters:

    $nanoChart->setSize(250); // width and height in pixels
    $nanoChart->setDirection('column'); // CSS flexbox direction options
    $nanoChart->setStyle('doughnut'); // change from a basic pie chart to a doughnut chart
    $nanoChart->setHue(150); // set the hue value for the colour palette
    $nanoChart->setPalette($palette); //pass in a simple array of colours to use for the items

Examples

See examples.php for different ways the class can be used.

Dependencies

Requires PHP 7.4 or later.

License

This project is licensed under the MIT License.

Acknowledgments

Feel free to contribute, report issues, or suggest improvements!


  YouTube demoExternal page  

Open in a separate window

Screenshots  
  • screenshot.png
  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file demo.php Example Example script
Accessible without login Plain text file examples.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file NanoChart.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1
This week:1
All time:11,370
This week:73Up