JpGraph Community
September 08, 2010, 12:56:07 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Sparklines Example  (Read 5057 times)
0 Members and 1 Guest are viewing this topic.
JonTheNiceGuy
Newbie
*
Posts: 2


View Profile
« on: April 30, 2008, 07:57:38 am »

A sparkline is an inline graphic for quickly representing a small amount of data. I was inspired by http://sparkline.org but re-implemented this in JpGraph because I was already using the JpGraph libraries in my project.

Sadly, I can't post an image (as the forum won't let me!), but here's the code I've used:

Code:
<?php

// This code developed by Jon "The Nice Guy" Spriggs, and is released under the GNU General Public License version 3.
// This is release 1.0

require_once("../libs/jpgraph/jpgraph.php");
require_once(
"../libs/jpgraph/jpgraph_line.php");

$v=0// Value (about to be set by Data)
$t=NULL// Top (Max)
$b=NULL// Bottom (Min)
$l=0// Last record and counter
foreach(explode(','$_GET['graph']) as $v) { //Value
  
$l++;
  
$line[$l]=$v+0.0;
  if(
$t==NULL OR $t<=$v) {$t=$v;}
  if(
$b==NULL OR $b>=$v) {$b=$v;}
}

function 
MaxMinLastCallback($XVal$YVal) {
  global 
$t,$b,$l;
  if(
$XVal==$t && $_GET['mark']!=FALSE) {return(array(5,'red','red'));}
  elseif(
$XVal==$b && $_GET['mark']!=FALSE) {return(array(5,'blue','blue'));}
  elseif(
$YVal==$l-&& $_GET['mark']!=FALSE) {return(array(5,'black','black'));}
  else {return(array(
0,'',''));}
}

if(isset(
$_GET['debug'])) {
  foreach(
$line as $key=>$value) {
    echo 
"$key => $value (";
    
$data=MaxMinLastCallback($value$key);
    
$set=0;
    foreach(
$data as $data_key=>$data_value) {
      echo 
"$data_value";
      if(
$set=1) {echo ",";}
      
$set=1;
    }
    echo 
")<br>";
  }
} else {
  
$graph = new Graph(10015);
  
$graph->SetFrame(false);
  
$graph->SetScale("lin");
  
$graph->SetMargin(0,0,0,1); // Need to have a margin of 1 at the bottom, otherwise it doesn't render properly.
  
$graph->xaxis->Hide();
  
$graph->yaxis->Hide();

  
$lineplot=new LinePlot($line); // This is the line of data
  
$lineplot->mark->SetType(MARK_X);
  
$lineplot->mark->SetWidth(0);
  
$lineplot->mark->SetCallbackYX("MaxMinLastCallback");
  
$lineplot->mark->Show();

  
$graph->Add($lineplot);
  
$graph->Stroke();
}
?>

Call this using: jpspark.php?graph=6,6,14,13,4,10,9,2,2,10,10,16,13,18,1,16,5,6,7,19,8,4,10
Logged
patrick
Newbie
*
Posts: 1


View Profile
« Reply #1 on: May 20, 2008, 12:03:55 pm »

Hi JonTheNiceGuy,

it's great ... but it seems to the, that the last value in $_GET ist missed. When I add a , at the end the last value is taken into the sparkline. Is it a bug or a feature of jpgraph or by design of your script?

Greets patrick
Logged
JonTheNiceGuy
Newbie
*
Posts: 2


View Profile
« Reply #2 on: May 21, 2008, 06:17:24 am »

Is it a bug or a feature of jpgraph or by design of your script?

I have to be honest, it's a bug, but I never actually meant to release this code, so I worked around it in my project. It may be where the borders of the image are a little close to the edges, so you don't get to see the last block.

You may be able to finish with a , to give it a last point to work with.

Jon
Logged
pmarc
jpGraph Guru
*****
Posts: 106


View Profile
« Reply #3 on: February 24, 2010, 10:22:39 am »

Just wanted to unburry that topic to say that I've been using http://sparkline.org for a while with no problem at all.
Becareful: it is free but unsupported by the author.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.036 seconds with 17 queries.