Krysti.Engineer Official Chat
You are viewing an inferior version of the site because your browser does not support WebP. Do upgrade to something like Chrome or Firefox. Loading websites like this causes them to require a fallback set of images and they are almost always lower quality and larger in size.

Using PHP Library JpGraph To Create Graphs

Got some numbers that you need to have a good visual representation? Let's get started. Let's say we have a csv file and we need to transfer into a line graph.
2022-07-20T:00:00:00+00:00 July 21 2022 00:00:00

export WEBROOT="/your/web/root/directory"
mkdir -p $WEBROOT/lib/jpgraph
wget "https://jpgraph.net/download/download.php?p=55" -O jpgraph-4.4.1.tar.gz
cd jpgraph-4.4.1/src
cp -R src/* $WEBROOT/lib/jpgraph/

Download JpGraph, extract and copy the contents of 'src/' directory to your webroot's 'lib/jpgraph/' directory.

<?php
require_once ('lib/jpgraph/jpgraph.php');
require_once ('lib/jpgraph/jpgraph_line.php');
// Here you would put a procedure to read your numbers
// but we'll put a sample number set in here.
$numbers = array(1,2,1,4,3,1,5,6,2,5,7,1,7,3,7,8,9,1,3,7,6,8,2,6,7);
// End sample
$graph = new Graph(800,600,'auto'); // Your width and height is set here
$graph->SetScale('textlin');
$title = "Cool graph with JpGraph library";
$lineplot=new LinePlot($numbers);
$lineplot->SetColor('red');
$graph->title->Set($title);
$graph->Add($lineplot);
$graph->Stroke();
?>

This will build a basic line graph based on the numbers in the example but this library does SO much more that would be redundant to the short little article here shouting out this handy little library. Read the documentation to learn how to expand JpGraph's powers with your graphing needs. The link provided is the full docs but I linked into the lineplot section so you can test and expand upon the sample shown above.
JpGraph in action

Contact Krysti

@itskrystibitch Twitter Photo
Twitter:
@KrystiOnline
Facebook:
@KrystiOnline
IRC:
Official Chat
Direct.Me:
@Krysti
Ko-fi:
@Krysti
Photos:
Virtual Krysti
E-Mail / Notify of errors:
coder [@] krysti.engineer
Please be patient contacting me, I don't really check much of social media or anything. If you use the IRC be sure to stick around because that's how IRC works, silly. :P