// Visualisation des preferences floues : trois panneaux SVG.
// Le SVG est ecrit a la main (matplotlib n'est pas dans la bibliotheque
// standard) et toutes les valeurs sont calculees, aucune n'est recopiee.
// Fabrique SVG : une boite, un texte, une barre, un segment, une courbe.
fn svg_open(w: f64, h: f64) -> String {
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{}\" height=\"{}\" viewBox=\"0 0 {} {}\">",
w, h, w, h
)
}
fn svg_text(x: f64, y: f64, s: &str, size: f64, anchor: &str) -> String {
format!(
"<text x=\"{}\" y=\"{}\" font-family=\"sans-serif\" font-size=\"{}\" text-anchor=\"{}\">{}</text>",
x, y, size, anchor, s
)
}
fn svg_rect(x: f64, y: f64, w: f64, h: f64, fill: &str) -> String {
format!(
"<rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" fill=\"{}\" stroke=\"black\" stroke-width=\"1\"/>",
x, y, w, h, fill
)
}
fn svg_line(x1: f64, y1: f64, x2: f64, y2: f64, stroke: &str, tirets: bool) -> String {
let d = if tirets {
" stroke-dasharray=\"4,3\""
} else {
""
};
format!(
"<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" stroke=\"{}\" stroke-width=\"1.5\"{}/>",
x1, y1, x2, y2, stroke, d
)
}
fn svg_polyline(points: &[(f64, f64)], stroke: &str) -> String {
let mut s = format!("<polyline fill=\"none\" stroke=\"{}\" stroke-width=\"2\" points=\"", stroke);
for (i, (x, y)) in points.iter().enumerate() {
if i > 0 {
s.push(' ');
}
s.push_str(&format!("{},{}", x, y));
}
s.push_str("\"/>");
s
}
fn svg_polygon(points: &[(f64, f64)], fill: &str) -> String {
let mut s = format!("<polygon fill=\"{}\" fill-opacity=\"0.3\" points=\"", fill);
for (i, (x, y)) in points.iter().enumerate() {
if i > 0 {
s.push(' ');
}
s.push_str(&format!("{},{}", x, y));
}
s.push_str("\"/>");
s
}
// Panneau de barres groupees : une serie par couleur, une barre par categorie.
// Une barre peut porter une couleur propre (couleur de remplacement).
fn svg_bar_panel(
x0: f64,
y0: f64,
w: f64,
h: f64,
titre: &str,
categories: &[&str],
series: &[(&str, &str, Vec<(f64, Option<String>)>)],
ymax: f64,
xlab: &str,
ylab: &str,
hlines: &[(f64, &str)],
vlines: &[(f64, &str)],
) -> String {
let pad_l = 62.0;
let pad_b = 42.0;
let pad_t = 28.0;
let pw = w - pad_l;
let ph = h - pad_b - pad_t;
let n = categories.len().max(1) as f64;
let ns = series.len().max(1) as f64;
let gw = pw / n;
let bw = (gw / ns) * 0.85;
let mut s = String::new();
s.push_str(&format!(
"<rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" fill=\"none\" stroke=\"black\"/>",
x0 + pad_l,
y0 + pad_t,
pw,
ph
));
s.push_str(&svg_text(
x0 + pad_l + pw / 2.0,
y0 + 19.0,
titre,
13.0,
"middle",
));
s.push_str(&svg_text(x0 + pad_l + pw / 2.0, y0 + h - 8.0, xlab, 11.0, "middle"));
s.push_str(&svg_text(
x0 + 16.0,
y0 + pad_t + ph / 2.0,
ylab,
11.0,
"middle",
));
for (ci, cat) in categories.iter().enumerate() {
let cx = x0 + pad_l + gw * (ci as f64 + 0.5);
s.push_str(&svg_text(cx, y0 + pad_t + ph + 16.0, cat, 10.0, "middle"));
}
for (si, (libelle, couleur, valeurs)) in series.iter().enumerate() {
for (ci, (v, remplacement)) in valeurs.iter().enumerate() {
let bh = ph * (v / ymax).clamp(0.0, 1.0);
let bx = x0 + pad_l + gw * ci as f64 + (gw / ns) * si as f64 + ((gw / ns) - bw) / 2.0;
let by = y0 + pad_t + ph - bh;
let couleur_barre = remplacement.as_deref().unwrap_or(couleur);
s.push_str(&svg_rect(bx, by, bw, bh, couleur_barre));
}
let ly = y0 + pad_t + 12.0 + (si as f64) * 15.0;
s.push_str(&svg_rect(x0 + pad_l + pw - 96.0, ly - 9.0, 11.0, 11.0, couleur));
s.push_str(&svg_text(
x0 + pad_l + pw - 81.0,
ly,
libelle,
10.0,
"start",
));
}
for (yv, couleur) in hlines {
let y = y0 + pad_t + ph - ph * (yv / ymax);
s.push_str(&svg_line(x0 + pad_l, y, x0 + pad_l + pw, y, couleur, true));
}
for (xv, couleur) in vlines {
let x = x0 + pad_l + xv;
s.push_str(&svg_line(x, y0 + pad_t, x, y0 + pad_t + ph, couleur, true));
}
s
}
// Preference par mois.
let months = ["Juin", "Juillet", "Aout"];
let month_prefs = [0.4f64, 1.0, 0.7];
// Preference par destination.
let dests = ["Londres", "Paris", "Rome"];
let dest_prefs = [0.3f64, 0.8, 1.0];
// Fonction d'appartenance du budget : 100 points de 500 a 2000 EUR.
let mut costs: Vec<f64> = Vec::with_capacity(100);
for i in 0..100 {
costs.push(500.0 + (i as f64) * 1500.0 / 99.0);
}
let mut membership: Vec<f64> = Vec::with_capacity(100);
for c in costs.iter() {
membership.push(if *c <= 1000.0 {
1.0
} else if *c <= 1500.0 {
(1.0 - (*c - 1000.0) / 500.0).max(0.0)
} else {
0.0
});
}
let mut svg = svg_open(1400.0, 420.0);
svg.push_str(&svg_bar_panel(
10.0,
20.0,
440.0,
380.0,
"Preference par Mois",
&months,
&[(
"Preference",
"steelblue",
month_prefs.iter().map(|v| (*v, None)).collect(),
)],
1.1,
"Mois",
"Degre de preference",
&[],
&[],
));
svg.push_str(&svg_bar_panel(
470.0,
20.0,
440.0,
380.0,
"Preference par Destination",
&dests,
&[(
"Preference",
"coral",
dest_prefs.iter().map(|v| (*v, None)).collect(),
)],
1.1,
"Destination",
"Degre de preference",
&[],
&[],
));
{
let px = 930.0;
let py = 20.0;
let pw = 460.0;
let ph = 380.0;
let pad_l = 62.0;
let pad_b = 42.0;
let pad_t = 28.0;
let w = pw - pad_l;
let h = ph - pad_b - pad_t;
svg.push_str(&format!(
"<rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" fill=\"none\" stroke=\"black\"/>",
px + pad_l,
py + pad_t,
w,
h
));
svg.push_str(&svg_text(
px + pad_l + w / 2.0,
py + 19.0,
"Fonction d'Appartenance Budget",
13.0,
"middle",
));
svg.push_str(&svg_text(
px + pad_l + w / 2.0,
py + ph - 8.0,
"Cout (EUR)",
11.0,
"middle",
));
svg.push_str(&svg_text(
px + 16.0,
py + pad_t + h / 2.0,
"Degre d'acceptabilite",
11.0,
"middle",
));
let x_of = |c: f64| px + pad_l + w * (c - 500.0) / 1500.0;
let y_of = |m: f64| py + pad_t + h - h * m;
let mut courbe: Vec<(f64, f64)> = Vec::with_capacity(100);
for (c, m) in costs.iter().zip(membership.iter()) {
courbe.push((x_of(*c), y_of(*m)));
}
let mut aire = courbe.clone();
aire.push((x_of(2000.0), y_of(0.0)));
aire.push((x_of(500.0), y_of(0.0)));
svg.push_str(&svg_polygon(&aire, "green"));
svg.push_str(&svg_polyline(&courbe, "green"));
svg.push_str(&svg_line(
x_of(1000.0),
py + pad_t,
x_of(1000.0),
py + pad_t + h,
"green",
true,
));
svg.push_str(&svg_line(
x_of(1500.0),
py + pad_t,
x_of(1500.0),
py + pad_t + h,
"red",
true,
));
svg.push_str(&svg_text(
x_of(1000.0) + 4.0,
py + pad_t + 13.0,
"Seuil ideal",
10.0,
"start",
));
svg.push_str(&svg_text(
x_of(1500.0) + 4.0,
py + pad_t + 27.0,
"Seuil max",
10.0,
"start",
));
svg.push_str(&svg_text(
px + pad_l,
py + pad_t + h + 16.0,
"500 EUR",
10.0,
"start",
));
svg.push_str(&svg_text(
px + pad_l + w,
py + pad_t + h + 16.0,
"2000 EUR",
10.0,
"end",
));
}
svg.push_str("</svg>");
println!(
"EVCXR_BEGIN_CONTENT image/svg+xml\n{}\nEVCXR_END_CONTENT",
svg
);