import streamlit as st import altair as alt import numpy as np from collections import Counter import pandas as pd def afficher_graphique_altair(df): ordre_personnalise = ['Assemblage', 'Fabrication', 'Traitement', 'Extraction'] categories = [cat for cat in ordre_personnalise if cat in df['categorie'].unique()] for cat in categories: st.markdown(f"### {cat}") df_cat = df[df['categorie'] == cat].copy() coord_pairs = list(zip(df_cat['ihh_pays'].round(1), df_cat['ihh_acteurs'].round(1))) counts = Counter(coord_pairs) offset_x = [] offset_y = {} seen = Counter() for pair in coord_pairs: rank = seen[pair] seen[pair] += 1 if counts[pair] > 1: angle = rank * 1.5 radius = 0.8 + 0.4 * rank offset_x.append(radius * np.cos(angle)) offset_y[pair] = radius * np.sin(angle) else: offset_x.append(0) offset_y[pair] = 0 df_cat['ihh_pays'] += offset_x df_cat['ihh_acteurs'] += [offset_y[p] for p in coord_pairs] df_cat['ihh_pays_text'] = df_cat['ihh_pays'] + 0.5 df_cat['ihh_acteurs_text'] = df_cat['ihh_acteurs'] + 0.5 base = alt.Chart(df_cat).encode( x=alt.X('ihh_pays:Q', title='IHH Pays (%)'), y=alt.Y('ihh_acteurs:Q', title='IHH Acteurs (%)'), size=alt.Size('criticite_cat:Q', scale=alt.Scale(domain=[1, 2, 3], range=[50, 500, 1000]), legend=None), color=alt.Color('criticite_cat:N', scale=alt.Scale(domain=[1, 2, 3], range=['darkgreen', 'orange', 'darkred'])) ) points = base.mark_circle(opacity=0.6) lines = alt.Chart(df_cat).mark_rule(strokeWidth=0.5, color='gray').encode( x='ihh_pays:Q', x2='ihh_pays_text:Q', y='ihh_acteurs:Q', y2='ihh_acteurs_text:Q' ) labels = alt.Chart(df_cat).mark_text( align='left', dx=3, dy=-3, fontSize=8, font='Arial', angle=335 ).encode( x='ihh_pays_text:Q', y='ihh_acteurs_text:Q', text='nom:N' ) hline_15 = alt.Chart(df_cat).mark_rule(strokeDash=[2,2], color='green').encode(y=alt.datum(15)) hline_25 = alt.Chart(df_cat).mark_rule(strokeDash=[2,2], color='red').encode(y=alt.datum(25)) vline_15 = alt.Chart(df_cat).mark_rule(strokeDash=[2,2], color='green').encode(x=alt.datum(15)) vline_25 = alt.Chart(df_cat).mark_rule(strokeDash=[2,2], color='red').encode(x=alt.datum(25)) chart = (points + lines + labels + hline_15 + hline_25 + vline_15 + vline_25).properties( width=500, height=400, title=f"Concentration et criticité – {cat}" ).interactive() st.altair_chart(chart, use_container_width=True) def creer_graphes(donnees): if not donnees: st.warning("Aucune donnée à afficher.") return try: df = pd.DataFrame(donnees) df['ivc_cat'] = df['ivc'].apply(lambda x: 1 if x <= 15 else (2 if x <= 30 else 3)) from collections import Counter coord_pairs = list(zip(df['ihh_extraction'].round(1), df['ihh_reserves'].round(1))) counts = Counter(coord_pairs) offset_x, offset_y = [], {} seen = Counter() for pair in coord_pairs: rank = seen[pair] seen[pair] += 1 if counts[pair] > 1: angle = rank * 1.5 radius = 0.8 + 0.4 * rank offset_x.append(radius * np.cos(angle)) offset_y[pair] = radius * np.sin(angle) else: offset_x.append(0) offset_y[pair] = 0 df['ihh_extraction'] += offset_x df['ihh_reserves'] += [offset_y[p] for p in coord_pairs] df['ihh_extraction_text'] = df['ihh_extraction'] + 0.5 df['ihh_reserves_text'] = df['ihh_reserves'] + 0.5 base = alt.Chart(df).encode( x=alt.X('ihh_extraction:Q', title='IHH Extraction (%)'), y=alt.Y('ihh_reserves:Q', title='IHH Réserves (%)'), size=alt.Size('ivc_cat:Q', scale=alt.Scale(domain=[1, 2, 3], range=[50, 500, 1000]), legend=None), color=alt.Color('ivc_cat:N', scale=alt.Scale(domain=[1, 2, 3], range=['darkgreen', 'orange', 'darkred'])), tooltip=['nom:N', 'ivc:Q', 'ihh_extraction:Q', 'ihh_reserves:Q'] ) points = base.mark_circle(opacity=0.6) lines = alt.Chart(df).mark_rule(strokeWidth=0.5, color='gray').encode( x='ihh_extraction:Q', x2='ihh_extraction_text:Q', y='ihh_reserves:Q', y2='ihh_reserves_text:Q' ) labels = alt.Chart(df).mark_text( align='left', dx=10, dy=-10, fontSize=10, font='Arial', angle=335 ).encode( x='ihh_extraction_text:Q', y='ihh_reserves_text:Q', text='nom:N' ) hline_15 = alt.Chart(df).mark_rule(strokeDash=[2,2], color='green').encode(y=alt.datum(15)) hline_25 = alt.Chart(df).mark_rule(strokeDash=[2,2], color='red').encode(y=alt.datum(25)) vline_15 = alt.Chart(df).mark_rule(strokeDash=[2,2], color='green').encode(x=alt.datum(15)) vline_25 = alt.Chart(df).mark_rule(strokeDash=[2,2], color='red').encode(x=alt.datum(25)) chart = (points + lines + labels + hline_15 + hline_25 + vline_15 + vline_25).properties( width=600, height=500, title="Concentration des ressources critiques vs vulnérabilité IVC" ).interactive() st.altair_chart(chart, use_container_width=True) except Exception as e: st.error(f"Erreur lors de la création du graphique : {e}") def lancer_visualisation_ihh_criticite(graph): try: import networkx as nx from utils.graph_utils import recuperer_donnees niveaux = nx.get_node_attributes(graph, "niveau") noeuds = [n for n, v in niveaux.items() if v == "10" and "Reserves" not in n] noeuds.sort() df = recuperer_donnees(graph, noeuds) if df.empty: st.warning("Aucune donnée à visualiser.") else: afficher_graphique_altair(df) except Exception as e: st.error(f"Erreur dans la visualisation IHH vs Criticité : {e}") def lancer_visualisation_ihh_ivc(graph): try: from utils.graph_utils import recuperer_donnees_2 noeuds_niveau_2 = [ n for n, data in graph.nodes(data=True) if data.get("niveau") == "2" and "ivc" in data ] if not noeuds_niveau_2: return data = recuperer_donnees_2(graph, noeuds_niveau_2) creer_graphes(data) except Exception as e: st.error(f"Erreur dans la visualisation IHH vs IVC : {e}") def interface_visualisations(G_temp, G_temp_ivc): st.markdown("# Visualisations") st.markdown("""## Indice de Herfindahl-Hirschmann - IHH vs Criticité Entre 0 et 15%, concentration faible, entre 15 et 25%, modérée, au-delà, forte. Taille des points = criticité substituabilité du minerai """) if st.button("Lancer", key="btn_ihh_criticite"): try: lancer_visualisation_ihh_criticite(G_temp) except Exception as e: st.error(f"Erreur dans la visualisation IHH vs Criticité : {e}") st.markdown("""## Indice de Herfindahl-Hirschmann - IHH vs IVC Entre 0 et 15%, concentration faible, entre 15 et 25%, modérée, au-delà, forte. Taille des points = criticité concurrentielle du minerai """) if st.button("Lancer", key="btn_ihh_ivc"): try: lancer_visualisation_ihh_ivc(G_temp_ivc) except Exception as e: st.error(f"Erreur dans la visualisation IHH vs IVC : {e}")