RENDIMENTO DEI BTP
“`html
Grafico Rendimento BTP
body {
font-family: Arial, sans-serif;
margin: 30px;
}
#chart {
width: 900px;
height: 600px;
}
Rendimento dei principali BTP italiani (ordinati decrescente)
// Dati titoli e rendimenti
const titoli = [
“BTP 1nv26 7,25%”, “BTP 1nv27 6,5%”, “BTP 1nv29 5,25%”,
“BTP 30Y 4,38%”, “BTP 25Y 4,22%”, “BTP 20Y 4,10%”,
“BTP 15Y 3,93%”, “BTP 10Y 3,52%”, “BTP 9Y 3,30%”, “BTP 8Y 3,19%”
];
const rendimenti = [7.25, 6.5, 5.25, 4.38, 4.22, 4.10, 3.93, 3.52, 3.30, 3.19];
// Ordina i dati in ordine decrescente per rendimento
const combined = titoli.map((t, i) => ({ titolo: t, rendimento: rendimenti[i] }));
combined.sort((a, b) => b.rendimento – a.rendimento);
// Estrai dati ordinati
const titoliOrdinati = combined.map(item => item.titolo);
const rendimentiOrdinati = combined.map(item => item.rendimento);
// Calcola posizioni verticali per scaletta etichette (offset alternato)
const yPositions = titoliOrdinati.map((_, i) => i);
const labelOffsets = titoliOrdinati.map((_, i) => ((i % 2 === 0) ? 15 : -15)); // spostamento verticale in px
// Trace per barre orizzontali
const trace = {
type: ‘bar’,
x: rendimentiOrdinati,
y: yPositions,
orientation: ‘h’,
text: titoliOrdinati,
textposition: ‘none’, // non mostrare testo standard
marker: {
color: ‘steelblue’
},
hovertemplate: ‘%{text}Rendimento: %{x:.2f}%’
};
// Layout con personalizzazione asse y e testo etichette sopra barre
const layout = {
height: 600,
width: 900,
margin: { l: 150, r: 50, t: 80, b: 50 },
yaxis: {
tickvals: yPositions,
ticktext: Array(yPositions.length).fill(”), // nascondi etichette asse y
autorange: ‘reversed’
},
xaxis: {
title: ‘Rendimento (%)’,
gridcolor: ‘#e5e5e5’,
zeroline: false
},
title: {
text: ‘Rendimento dei principali BTP italiani (ordinati decrescente)’,
font: { size: 20 }
},
// Aggiungiamo annotazioni per le etichette sopra le barre, con offset a scaletta
annotations: titoliOrdinati.map((titolo, i) => ({
x: rendimentiOrdinati[i] / 2, // metà barra
y: yPositions[i],
text: titolo,
showarrow: false,
font: {
size: 12,
color: ‘black’,
family: ‘Arial, sans-serif’,
weight: ‘bold’
},
xanchor: ‘left’,
yanchor: ‘middle’,
// Spostamento verticale alternato per scaletta
yshift: labelOffsets[i]
}))
};
Plotly.newPlot(‘chart’, [trace], layout, {displayModeBar: false});
“`
—