feat: Add NPV comparison and distribution charts to reporting
- Implemented NPV comparison chart generation using Plotly in ReportingService. - Added distribution histogram for Monte Carlo results. - Updated reporting templates to include new charts and improved layout. - Created new settings and currencies management pages. - Enhanced sidebar navigation with dynamic URL handling. - Improved CSS styles for chart containers and overall layout. - Added new simulation and theme settings pages with placeholders for future features.
This commit is contained in:
@@ -8,6 +8,9 @@ import math
|
||||
from typing import Mapping, Sequence
|
||||
from urllib.parse import urlencode
|
||||
|
||||
import plotly.graph_objects as go
|
||||
import plotly.io as pio
|
||||
|
||||
from fastapi import Request
|
||||
|
||||
from models import FinancialCategory, Project, Scenario
|
||||
@@ -515,6 +518,7 @@ class ReportingService:
|
||||
"label": "Download JSON",
|
||||
}
|
||||
],
|
||||
"chart_data": self._generate_npv_comparison_chart(reports),
|
||||
}
|
||||
|
||||
def build_scenario_comparison_context(
|
||||
@@ -611,8 +615,64 @@ class ReportingService:
|
||||
"label": "Download JSON",
|
||||
}
|
||||
],
|
||||
"chart_data": self._generate_distribution_histogram(report.monte_carlo) if report.monte_carlo else "{}",
|
||||
}
|
||||
|
||||
def _generate_npv_comparison_chart(self, reports: Sequence[ScenarioReport]) -> str:
|
||||
"""Generate Plotly chart JSON for NPV comparison across scenarios."""
|
||||
scenario_names = []
|
||||
npv_values = []
|
||||
|
||||
for report in reports:
|
||||
scenario_names.append(report.scenario.name)
|
||||
npv_values.append(report.deterministic.npv or 0)
|
||||
|
||||
fig = go.Figure(data=[
|
||||
go.Bar(
|
||||
x=scenario_names,
|
||||
y=npv_values,
|
||||
name='NPV',
|
||||
marker_color='lightblue'
|
||||
)
|
||||
])
|
||||
|
||||
fig.update_layout(
|
||||
title="NPV Comparison Across Scenarios",
|
||||
xaxis_title="Scenario",
|
||||
yaxis_title="NPV",
|
||||
showlegend=False
|
||||
)
|
||||
|
||||
return pio.to_json(fig) or "{}"
|
||||
|
||||
def _generate_distribution_histogram(self, monte_carlo: ScenarioMonteCarloResult) -> str:
|
||||
"""Generate Plotly histogram for Monte Carlo distribution."""
|
||||
if not monte_carlo.available or not monte_carlo.result or not monte_carlo.result.samples:
|
||||
return "{}"
|
||||
|
||||
# Get NPV samples
|
||||
npv_samples = monte_carlo.result.samples.get(SimulationMetric.NPV, [])
|
||||
if len(npv_samples) == 0:
|
||||
return "{}"
|
||||
|
||||
fig = go.Figure(data=[
|
||||
go.Histogram(
|
||||
x=npv_samples,
|
||||
nbinsx=50,
|
||||
name='NPV Distribution',
|
||||
marker_color='lightgreen'
|
||||
)
|
||||
])
|
||||
|
||||
fig.update_layout(
|
||||
title="Monte Carlo NPV Distribution",
|
||||
xaxis_title="NPV",
|
||||
yaxis_title="Frequency",
|
||||
showlegend=False
|
||||
)
|
||||
|
||||
return pio.to_json(fig) or "{}"
|
||||
|
||||
|
||||
def _build_cash_flows(scenario: Scenario) -> tuple[list[CashFlow], ScenarioFinancialTotals]:
|
||||
cash_flows: list[CashFlow] = []
|
||||
|
||||
Reference in New Issue
Block a user