\n"; $out.= "\n"; $iwidth = $width-100; $iheight = $height-100; #This is what little javascript/ecmascript it takes to make this work. #Note: No CDATA's. You might want to add them. Sometimes my naming conventions are all the keps me sane. $out.= << function livemouse(evt) { var foo = evt.target; foo.setAttribute("fill","#FF00FF"); } ; function deadmouse(evt) { var foo = evt.target; foo.setAttribute("fill","#8888FF"); document.getElementById("nuts").firstChild.nodeValue = '' ; document.getElementById("cheese").firstChild.nodeValue = '' ; document.getElementById("seeds").firstChild.nodeValue = '' ; } ; function showmouse(evt,cheese,seeds,nuts) { document.getElementById("nuts").setAttribute("x",evt.clientX + 30 ); document.getElementById("cheese").setAttribute("x",evt.clientX + 30 ); document.getElementById("seeds").setAttribute("x",evt.clientX + 30 ); document.getElementById("nuts").setAttribute("y",evt.clientY + 35); document.getElementById("cheese").setAttribute("y",evt.clientY + 10); document.getElementById("seeds").setAttribute("y",evt.clientY + 20 ); document.getElementById("nuts").firstChild.nodeValue = nuts ; document.getElementById("cheese").firstChild.nodeValue = cheese ; document.getElementById("seeds").firstChild.nodeValue = seeds ; } ; EOF; #Creates a 'G' for the main chart background $out.= ""; $out.= ""; $y = 0; while ($y < $iheight) { $y = $y+$grid; $out.= "\n"; }; $x = 0; while ($x < $iwidth) { $x = $x+$grid; # $out.= "\n"; }; $out.= "\n\n"; $out.= ""; $ty = $grid-$grid/4; $tx = $grid/2; $tf = $grid/2; $out.= "$title"; $out.= "\n"; #================END BASIC GRID BACKGROUND #------------------------------------------------ #Q1,2,3 $result = mysql_query($q1, $db) or die("Query failed $query: " . mysql_error()); $vmin = 9999999999999999; $vmax = 0; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $k = 0; while (list($key, $val) = each($row)) { #creates arrays of the data needed. if ($key == 'label') { #calcs max and min values from all sets. $labels[$c] = $val; #and grabs set labels } elseif ($key == 'sublabel') { $sublabels[$c] = $val; #and grabs set labels } else { $keys[$k] = $key; $values[$key][$c] = $val; if ($val < $vmin) { $vmin = intval($val) ; # -1 just keeps data off the bottom line. }; if ($val > $vmax) { $vmax = intval($val) +5; # You could add a +1 here for the same reason. }; $k++; # Increments +k ; }; }; $c++; }; if ($c > 0) { $h = intval($iwidth / $c) ; if ($vmin == 9999999999999999) { $vmin = 0; }; $vmin = 0; $vmin = floor($vmin) ; $l = strlen($vmax) ; $vmax = (substr($vmax,0,1) + 1) ; while(strlen($vmax) < $l) { $vmax .= '0' ; } ; $h = floor($iwidth/$c); $v = floor($iheight/($vmax-$vmin)); if($v < 2) { $v = (($iheight/($vmax-$vmin))) ; } ; if ($v < 0.0001) { $v = 0.0001; }; } else { #What to use if there is no data, makes it display a nice blank chart $vmin = 0; $vmax = 200; $h = 25; $v = 25; $labels = array(); $keys = array('No Data'); $values = array(); }; #====START DEBUG #DEBUGGING LINES USEFUL FOR SEEING HOW THIS WORKS #COMMENT THEM BOTH OUT FOR ACTUAL USAGE # print "iw $iwidth $iheight vmin $vmin vmax $vmax records $c hres $h vres $v" ; # $debug .= "iw $iwidth $ih $iheight vmin $vmin vmax $vmax records $c hres $h vres $v\n"; #=====END DEBUG $i = 0; foreach($labels as $set) { #LABELS ON BOTTOM $tx = $i*$h; $ih = $iheight+10; if($labelmode == '0') { if ($tx-$ltx > ($grid-1)) { $out.= "$set\n"; $out.= "\n"; $ltx = $tx; }; } ; if($labelmode == '1') { if ($lastset <> $set) { $out.= "$set\n"; $out.= "\n"; $ltx = $tx; }; } ; $lastset = $set ; $i++; }; if ($c > 0) { $i = 0; foreach($keys as $set) { #KEYS USING FIELD NAMES $tx = $iwidth+30; $ih = $iheight-($i*$grid) -$grid/2; $out.= "\n"; $out.= "$set\n"; $i++; }; $i = $iheight ; $iw = $iwidth+10; while ($i > 0 ) { #LABELS ON RIGHT OF CHART FOR VALUES $ih = $i ; $val = $vmax - ($vmax / ($iheight/$i)) ; $out.= "$val\n"; # $out.= "$i\n"; $i = $i - $grid ; }; $si = 0; foreach($values as $set) { $fill = $colors[$si]; $i = 0; $dots = ''; $points = ''; foreach($set as $s) { #$s = chartsizeme($s); $iy = ($iheight-($s * $v)); if ($chart == 'column') { $out.= ""; $out.= "\n"; $out.= ""; }; #x -------- #y| if ($chart == 'line') { $dots.= ""; $cx = $i*$h; $dots.= ""; if ($points == '') { $points = "M$cx,$iy "; } else { $points.= "L$cx,$iy "; }; #Have fun with T, $dots.= ""; }; $lasts = $s; $i++; }; $si++; if ($chart == 'line') { $out.= ""; $out.= $dots; }; }; #------------------------------------------------ #drawn last to stay on top: this is the items that the mouseover adds values, X, Y and text. $cw = ($iwidth/2) -$grid; $cc = $iheight-30; $cn = $iheight-60; $out.= " "; $out.= " "; $out.= " "; #------------------------------------------------------------------------------------------- } else { $out.= "No Data"; }; $out.= "$debug\n"; #THE END! print $out; }; function chartsizeme($val) { #return $val sized to display easily on charts. if ($val > 1000 and $val < 1000001) { $val = number_format($val/1000, 0, '.', ',') . 'k'; } elseif ($val > 1000000 and $val < 1000000001) { $val = number_format($val/1000000, 0, '.', ',') . 'm'; } elseif ($val > 1000000000 and $val < 1000000000001) { $val = number_format($val/1000000000, 0, '.', ',') . 'g'; } elseif ($val > 1000000000000) { $val = number_format($val/1000000000000, 0, '.', ',') . 't'; } else { $val = number_format($val, 2, '.', ','); }; return $val; }; function glconnect() { include ("demodata.inc"); $db = mysql_connect($dbhost, $dblogin, $dbpasswd); mysql_select_db($database, $db); return $db; }; // end function glconnect ; global $db ; $db = glconnect(); #==========================THE COMMAND SYNTAX # juicechart( SQLQUERY TO RUN, notusedyet,notusedyet, TITLE, column | line, width, height, grid size, 0 | 1 label type ) ; # # label type 0 = On the grid marker # label type 1 = On the label change #============================================ if($_REQUEST['demo'] == '1') { $q1 = "select date_format(billingtimestamp,'%b %d') as label,date_format(billingtimestamp,'%h:%m') as sublabel, value * 10 as kWh from meterintervaldata where serialnumber = 'SR000003' limit 100"; juicechart("$q1", "$q2", "$q3", 'kWh', 'column', 900, 300, 50,0); } ; if($_REQUEST['demo'] == '2') { $q1 = "select date_format(billingtimestamp,'%b %d') as label,date_format(billingtimestamp,'%h:%m') as sublabel, value * 10 as kWh from meterintervaldata where serialnumber = 'SR000003' limit 1000"; juicechart("$q1", "$q2", "$q3", 'kWh', 'line', 900, 300, 50,1); } ; if($_REQUEST['demo'] == '3') { $q1 = "select date_format(billingtimestamp,'%b %d') as label, usagediff/1000 as kWh,(usagediff/600) as Foo from meterreads where serialnumber = 'SR000003' and billingtimestamp > '20100101' limit 31"; juicechart("$q1", "$q2", "$q3", 'kWh', 'line', 900, 200, 25,1); } ; if($_REQUEST['demo'] == '') { $q1 = "select date_format(billingtimestamp,'%b %d') as label, usagediff/1000 as kWh,(usagediff/600) as Foo from meterreads where serialnumber = 'SR000003' and billingtimestamp > '20100101' limit 31"; juicechart("$q1", "$q2", "$q3", 'kWh', 'line', 900, 200, 25,1); } ; ?>