sid/svg.php

85 lines
2.4 KiB
PHP

<?php
function displayParcours($parcours){
/* paramètres de texte */
$lettrePixel = 7; // taille d'une lettre pour centrer le texte
$maxLettres = 0;
foreach($parcours as $p) if( strlen($p['nom']) > $maxLettres ) $maxLettres = strlen($p['nom']); // on récupère le libellé le plus long
/* calcul du positionnement */
// W = longueur du svg
// M = marge (utilisée pour séparer les éléments)
// m = marge de manoeuvre
// n = nombre d'éléments (points du parcours)
// t = longueur du libellé le plus long
$m = 30; // marge de manoeuvre
$t = $maxLettres * $lettrePixel; // longueur du libellé le plus long (px)
$n = count($parcours); // nombre d'élements du parcours
$M = $m + $t/2; // taille de la marge
$W = 2*$M*$n + 2;
/* paramètres graphiques */
$line = array( 'x' => $M, 'y' => '57', 'width' => $W-2*$M, 'height' => 6 );
$dot = array( 'y' => 60, 'r' => ($M <= 20 ) ? $M/3 : 10 );
$text = array( 'y' => 30 );
$themes = array( // couleur des thèmes
'pamplemousse' => '#f34e4e',
'paprika' => '#c42019',
'banana' => '#f3c04e',
'tomato' => '#d50000',
'twitter' => '#30b6ea',
'facebook' => '#3372c5',
'blou' => '#3c73e6'
);
/* CHOIX DU THEME */
$themeColor = $themes['twitter'];
$textColor = $themes['facebook'];
/****************/
/* DEBUT DU SVG */
/****************/
echo "<svg height='100' width='".($W + 2*$m)."'>";
// [1] ligne de liaison
echo "<rect x='".$line['x']."' y='".$line['y']."' width='".$line['width']."' height='".$line['height']."' fill='".$themeColor."' />";
// [3] points d'ancrages
for($i = 0 ; $i < $n ; $i++){
$ti = $lettrePixel * strlen( $parcours[$i]['nom'] ); // longueur du texte actuel
echo "<text x='".($M+2*$M*$i - $ti/2)."' y='".$text['y']."' fill='".$textColor."' style='font-family:Ubuntu;font-size:16px;'>".$parcours[$i]['nom']."</text>";
echo "<circle class='semestre_circle' data-stre='".$parcours[$i]['id']."' cx='".($M+2*$M*$i)."' cy='".$dot['y']."' r='".$dot['r']."' stroke='".$themeColor."' stroke-width='".(.8*$dot['r'])."' fill='#ecf0f1'/>";
}
echo "</svg>";
/**************/
/* FIN DU SVG */
/**************/
}
// exemple
// displayParcours(array(
// array('id'=>1, 'nom'=>'S1'),
// array('id'=>2, 'nom'=>'S2'),
// array('id'=>3, 'nom'=>'S3'),
// array('id'=>4, 'nom'=>'S4')
// ));
?>