fp_pie_chart.php

This script uses the pchart library (pchart.net) to display a pie chart in FlightPath.

Use this script as if it were an image file, ex: <img src='fp_pie_chart.php?args=vals' />

NOTE!! You are REQUIRED to first set an arbitrary token into $_SESSION["fp_pie_chart_token"], and pass it an argument of token= in the query. This is to prevent outside sites from accessing this script.

Expected in the args list:

token = string (ex: token=44539884)

  • This is REQUIRED. This will be matched against $_SESSION["fp_pie_chart_token"] to see if they match. This is to ensure that outside sites cannot access this script. *

progress = num (ex: progress=20)

  • How much progress, out of 100, should be displayed.

unfinished = num (ex: unfinished=80)

  • How much is remaining?

It is assumed you've already done the calculations to make sure that progress and unfinished add up to 100.

unfinished_col = html hex color (ex: unfinished_col=660000)

  • Notice we need to leave off the #.

progress_col = html hex color (ex: progress_col=CCCCCC)

Functions & methods

NameDescription
hex2rgb

File

inc/pchart/fp_pie_chart.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * This script uses the pchart library (pchart.net) to display a pie chart in FlightPath.
  5. *
  6. * Use this script as if it were an image file, ex:
  7. * <img src='fp_pie_chart.php?args=vals' />
  8. *
  9. * NOTE!! You are REQUIRED to first set an arbitrary token into $_SESSION["fp_pie_chart_token"],
  10. * and pass it an argument of token= in the query. This is to prevent outside sites from accessing
  11. * this script.
  12. *
  13. * Expected in the args list:
  14. *
  15. * token = string (ex: token=44539884)
  16. * - This is REQUIRED. This will be matched against $_SESSION["fp_pie_chart_token"] to see if
  17. * they match. This is to ensure that outside sites cannot access this script. *
  18. * progress = num (ex: progress=20)
  19. * - How much progress, out of 100, should be displayed.
  20. * unfinished = num (ex: unfinished=80)
  21. * - How much is remaining?
  22. *
  23. * It is assumed you've already done the calculations to make sure that
  24. * progress and unfinished add up to 100.
  25. *
  26. * unfinished_col = html hex color (ex: unfinished_col=660000)
  27. * - Notice we need to leave off the #.
  28. * progress_col = html hex color (ex: progress_col=CCCCCC)
  29. *
  30. */
  31. // check session variable for valid token
  32. session_start();
  33. $token = $_GET["token"];
  34. $sess_token = $_SESSION["fp_pie_chart_token"];
  35. if ($token == "" || $sess_token == "" || ($token != $sess_token && $sess_token != "")) {
  36. die("Token mismatch");
  37. }
  38. // Include the pChart classes...
  39. include_once("pchart/class/pData.class.php");
  40. include_once("pchart/class/pDraw.class.php");
  41. include_once("pchart/class/pPie.class.php");
  42. include_once("pchart/class/pImage.class.php");
  43. $progress = addslashes($_GET["progress"])*1;
  44. $unfinished = addslashes($_GET["unfinished"])*1;
  45. $unfinished_col = hex2rgb($_GET["unfinished_col"]);
  46. $progress_col = hex2rgb($_GET["progress_col"]);
  47. // Begin constructing the chart.
  48. $data = new pData();
  49. $data->addPoints(array($progress, $unfinished), "Value");
  50. // Required before chart will show up...
  51. $data->addPoints(array("point1", "point2"), "Legend");
  52. $data->setAbscissa("Legend");
  53. $picture = new pImage(75, 75, $data);
  54. $chart = new pPie($picture, $data);
  55. // Set colors
  56. $chart->setSliceColor(0, array("R" => $progress_col["r"], "G" => $progress_col["g"], "B" => $progress_col["b"])); // first val (progress)
  57. $chart->setSliceColor(1, array("R" => $unfinished_col["r"], "G" => $unfinished_col["g"], "B" => $unfinished_col["b"])); // remainder, unfinished col
  58. // Render it out, with a certain size, and a little gap between the value and the remainder
  59. $chart->draw2DPie(38, 38, array("Radius" => 25, "Border" => TRUE));
  60. // Render the graphic to the browser
  61. $picture->Stroke();
  62. exit();
  63. // Needed a function to convert HTML hex colors to an rgb array.
  64. function hex2rgb($hex) {
  65. $hex = str_replace("#", "", $hex);
  66. if(strlen($hex) == 3) {
  67. $r = hexdec(substr($hex,0,1).substr($hex,0,1));
  68. $g = hexdec(substr($hex,1,1).substr($hex,1,1));
  69. $b = hexdec(substr($hex,2,1).substr($hex,2,1));
  70. } else {
  71. $r = hexdec(substr($hex,0,2));
  72. $g = hexdec(substr($hex,2,2));
  73. $b = hexdec(substr($hex,4,2));
  74. }
  75. $rgb = array("r" => $r, "g" => $g, "b" => $b);
  76. return $rgb; // returns an array with the rgb values
  77. }