Bar.class.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once(__DIR__.SLASH.'..'.SLASH.'View.class.php');
  3. class Bar{
  4. public static $LABEL = "Histogramme";
  5. public static $DESCRIPTION = "Graphique en barres verticales";
  6. public static $ICON = "far fa-chart-bar";
  7. function __construct(){}
  8. public static function option(){
  9. $options = array();
  10. for($i=0;$i<12;$i++){
  11. $options['color-'.$i] = array('label'=>'Couleur '.$i,'type'=>'color');
  12. }
  13. return $options;
  14. }
  15. public static function toHtml($label,$data,$options){
  16. require_once(__DIR__.SLASH.'..'.SLASH.'View.class.php');
  17. if(!isset($options['properties'])) $options['properties'] = array();
  18. $html = '';
  19. try{
  20. $data = $data['data'];
  21. $colors = View::colorSet();
  22. shuffle($colors);
  23. $colorNumber = 0;
  24. foreach($options['properties'] as $key=>$value){
  25. if(strpos( $key,'color-')===false || empty($value)) continue;
  26. $colors[$colorNumber] = $value;
  27. $colorNumber++;
  28. }
  29. $background = implode('","',$colors);
  30. $keys = array_keys($data);
  31. $values = array_values($data);
  32. if(count($data)==0) throw new Exception("<h4 class='text-center'>Aucune donnée</h4>");
  33. if(!isset($data) || View::array_depth($data)>1) throw new Exception("Données d'entrées incorrectes.<br> Les données de ce graphique doivent être présentées sous la forme d'un simple tableau clé valeur<br>
  34. <pre>
  35. array(
  36. 'Légende 1' => 12,
  37. 'Légende 2' => 14,
  38. 'Légende 3' => 15
  39. );
  40. </pre>");
  41. if(isset($label)) $html.='<h5 class="text-center">'.$label.'</h5>';
  42. /*
  43. $html .= '<canvas data-background="[\''.$background.'\']" ';
  44. $html .= 'data-values="['.implode(',',$values).']" ';
  45. $html .= 'data-keys=\'["'.implode('","',$keys).'"]\' data-object="chart" data-type="bar"></canvas>';
  46. */
  47. $hideLegend = (!isset($options['properties']['show-legend']) || $options['properties']['show-legend']!=='1');
  48. $html .= '<canvas data-colors=\'["'.$background.'"]\' ';
  49. $html .= 'data-values="['.implode(',',$values).']" ';
  50. $html .= 'data-labels=\'["'.implode('","',$keys).'"]\' data-type="bar"></canvas>';
  51. }catch(Exception $e){
  52. $html .= '<h5 class="text-center">'.(isset($label)?$label:'Affichage impossible :').'</h5><br>'.$e->getMessage();
  53. }
  54. return $html;
  55. }
  56. }
  57. ?>