PHP code example of image-charts / image-charts

1. Go to this page and download the library: Download image-charts/image-charts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

image-charts / image-charts example snippets



$chart = new \ImageCharts();



$pie = $chart->cht('p')->chd('a:2.5,5,8.3')->chs('100x100');

$pie->toURL(); // https://image-charts.com/chart?chd=a%3A2.5%2C5%2C8.3&chs=600x300&cht=p
$pie->toFile('/path/to/chart.png'); //
$pie->toDataURI(); // data:image/png;base64,iVBORw0KGgo...
$pie->toBinary(); // {image content}


new ImageCharts( [ Object $opt ] )


$opt = array(
/*
* Request timeout (in millisecond) when calling toBinary() or toDataURI()
*/
'timeout' => 5000,

/*
* (Enterprise and Enterprise+ subscription only) SECRET_KEY
*/
'secret' => null,

/*
* (Enterprise, Enterprise+ and On-Premise subscription only) custom domain
*/
'host' => 'image-charts.com',

/*
* (On-Premise subscription only) custom protocol
*/
'protocol' => 'https',

/*
* (On-Premise subscription only) custom port
*/
'port' => 443,

/*
* (On-Premise subscription only) custom pathname
*/
'pathname' => '/chart'
)



$chart_url = new \ImageCharts()
                ->cht('bvg') // vertical bar chart
                ->chs('300x300') // 300px x 300px
                ->chd('a:60,40') // 2 data points: 60 and 40
                ->toURL(); // get the generated URL

echo $chart_url; // https://image-charts.com/chart?cht=bvg&chs=300x300&chd=a%3A60%2C40



(new \ImageCharts())
->cht('bvg') // vertical bar chart
->chs('300x300') // 300px x 300px
->chd('a:60,40') // 2 data points: 60 and 40
->toFile('/tmp/gorgeous_chart.png'); // write the chart



$chart_url = (new ImageCharts())
                ->cht('bvg') // vertical bar chart
                ->chs('300x300') // 300px x 300px
                ->chd('a:60,40') // 2 data points: 60 and 40
                ->toBinary(); // download chart image

echo $chart_url; // Image content



$chart_url = (new ImageCharts())
            ->cht('bvg') // vertical bar chart
            ->chs('300x300') // 300px x 300px
            ->chd('a:60,40') // 2 data points: 60 and 40
            ->toDataURI(); // download chart image and generate a data URI string

echo $chart_url; // "data:image/png;base64,iVBORw0KGgo...




$chart_url = (new ImageCharts(array("secret" => "SECRET_KEY")))
                ->icac('ACCOUNT_ID')
                ->cht('p3') // pie chart
                ->chs('700x190') // 700px x 190px
                ->chd('t:60,40') // 2 data points: 60 and 40
                ->chl('Hello|World') // 1 label per pie slice : "Hello" and "World"
                ->chf('ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1') // 1 gradient per pie slice
                ->icretina('1') // enable paid-only features like high-resolution charts
                ->toURL(); // get the whole (HMAC signed) URL

echo $chart_url;
// https://image-charts.com/chart?chd=t%3A60%2C40&chf=ps0-0%2Clg%2C45%2Cffeb3b%2C0.2%2Cf44336%2C1%7Cps0-1%2Clg%2C45%2C8bc34a%2C0.2%2C009688%2C1&chl=Hello%7CWorld&chs=700x190&cht=p3&icac=documentation&icretina=1&ichm=122242bb700d825d69b3fafe4ec67193b45dcfe0ed93fcca307e0d0a84b73ea2



$chart_url = (new ImageCharts(array(
    'secret' => 'SECRET_KEY',

    // override the host with your own endpoint
    'host' => 'custom-domain.tld'
)))
->icac('ACCOUNT_ID')
->cht('p3') // pie chart
->chs('700x190') // 700px x 190px
->chd('t:60,40') // 2 data points: 60 and 40
->chl('Hello|World') // 1 label per pie slice : "Hello" and "World"
->chf('ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1') // 1 gradient per pie slice
->icretina('1') // enable paid-only features like high-resolution charts
->toURL(); // get the whole (HMAC signed) URL

echo $chart_url;
// https://custom-domain.tld/chart?chd=t%3A60%2C40&chf=ps0-0%2Clg%2C45%2Cffeb3b%2C0.2%2Cf44336%2C1%7Cps0-1%2Clg%2C45%2C8bc34a%2C0.2%2C009688%2C1&chl=Hello%7CWorld&chs=700x190&cht=p3&icac=documentation&icretina=1&ichm=122242bb700d825d69b3fafe4ec67193b45dcfe0ed93fcca307e0d0a84b73ea2