-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinancial_analysis.js
117 lines (103 loc) · 4.94 KB
/
financial_analysis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// financial_analysis.js
document.addEventListener("DOMContentLoaded", function() {
// Get references to input elements
const systemCostInput = document.getElementById("system-cost");
const incentiveInput = document.getElementById("incentive");
const energyRateInput = document.getElementById("energy-rate");
const annualProductionInput = document.getElementById("annual-production");
const calculateButton = document.getElementById("calculate-financial");
// Set default values for the inputs
systemCostInput.value = 15000;
incentiveInput.value = 3000;
energyRateInput.value = 0.25;
annualProductionInput.value = 8000;
// Get references to output elements
const totalInvestmentOutput = document.getElementById("total-investment");
const annualSavingsOutput = document.getElementById("annual-savings");
const paybackPeriodOutput = document.getElementById("payback-period");
const roiOutput = document.getElementById("roi");
// Event listener for calculate button
calculateButton.addEventListener("click", function() {
// Parse input values
const systemCost = parseFloat(systemCostInput.value);
const incentive = parseFloat(incentiveInput.value);
const energyRate = parseFloat(energyRateInput.value);
const annualProduction = parseFloat(annualProductionInput.value);
// Validate inputs
if (isNaN(systemCost) || isNaN(incentive) || isNaN(energyRate) || isNaN(annualProduction)) {
alert("Please enter valid numbers for all inputs.");
return;
}
// Calculate financial metrics
const totalInvestment = systemCost - incentive;
const annualSavings = annualProduction * energyRate;
const paybackPeriod = totalInvestment / annualSavings;
const roi = ((annualSavings / totalInvestment) * 100).toFixed(2);
// Display results
totalInvestmentOutput.textContent = `Total Investment Cost: $${totalInvestment.toFixed(2)}`;
annualSavingsOutput.textContent = `Annual Savings: $${annualSavings.toFixed(2)}`;
paybackPeriodOutput.textContent = `Payback Period: ${paybackPeriod.toFixed(2)} years`;
roiOutput.textContent = `Return on Investment (ROI): ${roi}%`;
});
// Chart.js integration for financial visualizations
const ctxPaybackChart = document.getElementById("paybackChart").getContext("2d");
let paybackChart;
function updatePaybackChart(totalInvestment, annualSavings, paybackPeriod) {
const cumulativeSavings = [];
for (let year = 0; year <= Math.ceil(paybackPeriod); year++) {
cumulativeSavings.push(year * annualSavings);
}
// If chart already exists, destroy it to prevent duplication issues
if (paybackChart) {
paybackChart.destroy();
}
// Create a new chart
paybackChart = new Chart(ctxPaybackChart, {
type: 'line',
data: {
labels: Array.from({ length: cumulativeSavings.length }, (_, i) => `Year ${i}`),
datasets: [{
label: 'Cumulative Savings ($)',
data: cumulativeSavings,
borderColor: '#3498db',
backgroundColor: 'rgba(52, 152, 219, 0.2)',
fill: true,
borderWidth: 2
}, {
label: 'Total Investment ($)',
data: Array(cumulativeSavings.length).fill(totalInvestment),
borderColor: '#e74c3c',
borderDash: [5, 5],
fill: false,
borderWidth: 2
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Dollars ($)'
}
}
}
}
});
}
// Event listener to update chart when calculate button is clicked
calculateButton.addEventListener("click", function() {
const systemCost = parseFloat(systemCostInput.value);
const incentive = parseFloat(incentiveInput.value);
const energyRate = parseFloat(energyRateInput.value);
const annualProduction = parseFloat(annualProductionInput.value);
if (!isNaN(systemCost) && !isNaN(incentive) && !isNaN(energyRate) && !isNaN(annualProduction)) {
const totalInvestment = systemCost - incentive;
const annualSavings = annualProduction * energyRate;
const paybackPeriod = totalInvestment / annualSavings;
// Update the payback chart
updatePaybackChart(totalInvestment, annualSavings, paybackPeriod);
}
});
});