The user wants a "Lease vs Buy" financial analysis tool with:
- WACC (Weighted Average Cost of Capital)
- Tax considerations
- Sensitivity analysis
This is a corporate finance decision tool that compares:
- Leasing an asset (with tax-deductible lease payments)
- Buying an asset (with depreciation tax shields and interest tax shields)
Key components needed:
-
Input Parameters:
- Asset cost
- Lease terms (payment, term)
- Debt capacity/interest rate
- Tax rate
- WACC
- Depreciation method/schedule
- Residual value
-
Calculations:
-
Sensitivity Analysis:
- How the decision changes with different:
- Discount rates
- Tax rates
- Lease payment amounts
- Residual values
-
Visualization:
- Comparison charts
- Sensitivity tornado chart
- NPV profile
Let me create an interactive, visually appealing single-page HTML tool with:
- Clean financial dashboard aesthetic
- Real-time calculations
- Interactive sensitivity sliders
- Charts showing the comparison
- Clear decision recommendation
I'll use a professional, clean design with a dark theme that's appropriate for financial tools.
Lease vs Buy Analysis Tool
This is a comprehensive financial analysis tool that compares leasing versus buying an asset, incorporating:
- WACC (Weighted Average Cost of Capital) as the discount rate
- Tax effects including depreciation tax shields and after-tax lease payments
- Sensitivity analysis with interactive sliders and tornado chart
Key Features:
- Real-time NPV calculations for both options
- Incremental analysis (Lease vs Buy differential)
- Break-even analysis visualization
- Sensitivity tornado chart showing impact of key variables
- Detailed cash flow breakdown
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lease vs Buy Analyzer</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Space+Mono:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--bg: #0a0f1a;
--bg-secondary: #111827;
--card: #1a2332;
--border: #2d3a4f;
--fg: #e8edf5;
--muted: #8896ab;
--accent: #10b981;
--accent-glow: rgba(16, 185, 129, 0.3);
--danger: #ef4444;
--warning: #f59e0b;
}
* {
box-sizing: border-box;
}
body {
font-family: 'DM Sans', sans-serif;
background: var(--bg);
color: var(--fg);
min-height: 100vh;
}
.mono {
font-family: 'Space Mono', monospace;
}
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 12px;
}
.input-field {
background: var(--bg-secondary);
border: 1px solid var(--border);
border-radius: 8px;
padding: 10px 14px;
color: var(--fg);
width: 100%;
transition: border-color 0.2s, box-shadow 0.2s;
}
.input-field:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.slider-track {
-webkit-appearance: none;
appearance: none;
width: 100%;
height: 6px;
background: var(--border);
border-radius: 3px;
outline: none;
}
.slider-track::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
background: var(--accent);
border-radius: 50%;
cursor: pointer;
transition: transform 0.15s;
}
.slider-track::-webkit-slider-thumb:hover {
transform: scale(1.2);
}
.result-badge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 16px;
border-radius: 20px;
font-weight: 600;
}
.result-lease {
background: rgba(16, 185, 129, 0.15);
color: var(--accent);
border: 1px solid var(--accent);
}
.result-buy {
background: rgba(59, 130, 246, 0.15);
color: #3b82f6;
border: 1px solid #3b82f6;
}
.glow-accent {
box-shadow: 0 0 30px var(--accent-glow);
}
@keyframes fadeSlideIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes countUp {
from { opacity: 0; }
to { opacity: 1; }
}
.animate-in {
animation: fadeSlideIn 0.5s ease-out forwards;
}
.stagger-1 { animation-delay: 0.1s; opacity: 0; }
.stagger-2 { animation-delay: 0.2s; opacity: 0; }
.stagger-3 { animation-delay: 0.3s; opacity: 0; }
.stagger-4 { animation-delay: 0.4s; opacity: 0; }
@media (prefers-reduced-motion: reduce) {
.animate-in {
animation: none;
opacity: 1;
}
}
.grid-bg {
background-image:
linear-gradient(rgba(45, 58, 79, 0.3) 1px, transparent 1px),
linear-gradient(90deg, rgba(45, 58, 79, 0.3) 1px, transparent 1px);
background-size: 40px 40px;
}
.gradient-overlay {
background: radial-gradient(ellipse at top right, rgba(16, 185, 129, 0.08) 0%, transparent 50%),
radial-gradient(ellipse at bottom left, rgba(59, 130, 246, 0.05) 0%, transparent 50%);
}
</style>
</head>
<body class="grid-bg">
<div class="gradient-overlay min-h-screen">
<!-- Header -->
<header class="border-b border-[var(--border)] py-4 px-6 animate-in">
<div class="max-w-7xl mx-auto flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="w-10 h-10 rounded-lg bg-[var(--accent)] flex items-center justify-center">
<svg class="w-5 h-5 text-[var(--bg)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 7h6m0 10v-3m-3 3h.01M9 17h.01M9 14h.01M12 14h.01M15 11h.01M12 11h.01M9 11h.01M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
</svg>
</div>
<div>
<h1 class="text-lg font-bold">Lease vs Buy Analyzer</h1>
<p class="text-xs text-[var(--muted)]">Financial Decision Tool</p>
</div>
</div>
<div class="text-right">
<p class="text-xs text-[var(--muted)]">Analysis Method</p>
<p class="mono text-sm text-[var(--accent)]">NPV + Incremental</p>
</div>
</div>
</header>
<main class="max-w-7xl mx-auto p-6">
<!-- Decision Banner -->
<div id="decisionBanner" class="card p-6 mb-6 animate-in stagger-1">
<div class="flex flex-col md:flex-row items-center justify-between gap-4">
<div>
<p class="text-sm text-[var(--muted)] mb-1">Recommendation</p>
<div class="flex items-center gap-4">
<span id="recommendation" class="result-badge result-lease">LEASE</span>
<span class="text-[var(--muted)]">saves</span>
<span id="savingsAmount" class="mono text-2xl font-bold text-[var(--accent)]">$0</span>
</div>
</div>
<div class="flex gap-6 text-center">
<div>
<p class="text-xs text-[var(--muted)]">Buy NPV</p>
<p id="buyNPV" class="mono text-xl font-bold">$0</p>
</div>
<div class="w-px bg-[var(--border)]"></div>
<div>
<p class="text-xs text-[var(--muted)]">Lease NPV</p>
<p id="leaseNPV" class="mono text-xl font-bold">$0</p>
</div>
<div class="w-px bg-[var(--border)]"></div>
<div>
<p class="text-xs text-[var(--muted)]">Incremental</p>
<p id="incrementalNPV" class="mono text-xl font-bold">$0</p>
</div>
</div>
</div>
</div>
<div class="grid lg:grid-cols-3 gap-6">
<!-- Input Panel -->
<div class="lg:col-span-1 space-y-4">
<!-- Asset Details -->
<div class="card p-5 animate-in stagger-2">
<h2 class="font-semibold mb-4 flex items-center gap-2">
<svg class="w-4 h-4 text-[var(--accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
</svg>
Asset Details
</h2>
<div class="space-y-4">
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Asset Cost</label>
<div class="relative">
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-[var(--muted)]">$</span>
<input type="number" id="assetCost" value="500000" class="input-field pl-8 mono">
</div>
</div>
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Useful Life (Years)</label>
<input type="number" id="usefulLife" value="5" min="1" max="30" class="input-field mono">
</div>
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Salvage Value</label>
<div class="relative">
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-[var(--muted)]">$</span>
<input type="number" id="salvageValue" value="50000" class="input-field pl-8 mono">
</div>
</div>
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Depreciation Method</label>
<select id="deprMethod" class="input-field">
<option value="straight">Straight-Line</option>
<option value="double">Double Declining</option>
<option value="macrs5">MACRS 5-Year</option>
</select>
</div>
</div>
</div>
<!-- Financing Parameters -->
<div class="card p-5 animate-in stagger-3">
<h2 class="font-semibold mb-4 flex items-center gap-2">
<svg class="w-4 h-4 text-[var(--accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
Financing & Tax
</h2>
<div class="space-y-4">
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">WACC (%)</label>
<input type="number" id="wacc" value="10" step="0.1" class="input-field mono">
</div>
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Corporate Tax Rate (%)</label>
<input type="number" id="taxRate" value="25" step="0.1" class="input-field mono">
</div>
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Annual Lease Payment</label>
<div class="relative">
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-[var(--muted)]">$</span>
<input type="number" id="leasePayment" value="120000" class="input-field pl-8 mono">
</div>
</div>
<div>
<label class="text-xs text-[var(--muted)] mb-1 block">Lease Term (Years)</label>
<input type="number" id="leaseTerm" value="5" min="1" max="30" class="input-field mono">
</div>
</div>
</div>
</div>
<!-- Results Panel -->
<div class="lg:col-span-2 space-y-6">
<!-- Charts -->
<div class="card p-5 animate-in stagger-3">
<div class="flex items-center justify-between mb-4">
<h2 class="font-semibold">NPV Comparison</h2>
<div class="flex gap-4 text-xs">
<span class="flex items-center gap-1">
<span class="w-3 h-3 rounded bg-[#3b82f6]"></span> Buy
</span>
<span class="flex items-center gap-1">
<span class="w-3 h-3 rounded bg-[var(--accent)]"></span> Lease
</span>
</div>
</div>
<canvas id="comparisonChart" height="200"></canvas>
</div>
<!-- Cash Flow Table -->
<div class="card p-5 animate-in stagger-4">
<h2 class="font-semibold mb-4">Cash Flow Analysis</h2>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="text-left text-[var(--muted)] border-b border-[var(--border)]">
<th class="pb-3 font-medium">Year</th>
<th class="pb-3 font-medium text-right">Buy CF</th>
<th class="pb-3 font-medium text-right">Depr. Shield</th>
<th class="pb-3 font-medium text-right">Lease CF</th>
<th class="pb-3 font-medium text-right">Diff (L-B)</th>
</tr>
</thead>
<tbody id="cashFlowTable"></tbody>
</table>
</div>
</div>
<!-- Sensitivity Analysis -->
<div class="card p-5 animate-in stagger-4">
<h2 class="font-semibold mb-4">Sensitivity Analysis</h2>
<!-- Sensitivity Sliders -->
<div class="grid md:grid-cols-2 gap-6 mb-6">
<div>
<div class="flex justify-between text-xs mb-2">
<span class="text-[var(--muted)]">WACC</span>
<span id="sensWaccVal" class="mono text-[var(--accent)]">10%</span>
</div>
<input type="range" id="sensWacc" min="5" max="20" step="0.5" value="10" class="slider-track">
</div>
<div>
<div class="flex justify-between text-xs mb-2">
<span class="text-[var(--muted)]">Tax Rate</span>
<span id="sensTaxVal" class="mono text-[var(--accent)]">25%</span>
</div>
<input type="range" id="sensTax" min="0" max="40" step="1" value="25" class="slider-track">
</div>
<div>
<div class="flex justify-between text-xs mb-2">
<span class="text-[var(--muted)]">Lease Payment</span>
<span id="sensLeaseVal" class="mono text-[var(--accent)]">100%</span>
</div>
<input type="range" id="sensLease" min="50" max="150" step="5" value="100" class="slider-track">
</div>
<div>
<div class="flex justify-between text-xs mb-2">
<span class="text-[var(--muted)]">Salvage Value</span>
<span id="sensSalvageVal" class="mono text-[var(--accent)]">100%</span>
</div>
<input type="range" id="sensSalvage" min="0" max="200" step="10" value="100" class="slider-track">
</div>
</div>
<!-- Tornado Chart -->
<div class="mt-6">
<h3 class="text-sm text-[var(--muted)] mb-4">Tornado Chart - Impact on Decision</h3>
<canvas id="tornadoChart" height="180"></canvas>
</div>
</div>
<!-- Break-even Analysis -->
<div class="card p-5 animate-in stagger-4">
<h2 class="font-semibold mb-4">Break-Even Analysis</h2>
<canvas id="breakevenChart" height="200"></canvas>
<p class="text-xs text-[var(--muted)] mt-3 text-center">
Indifference Point: <span id="breakevenPoint" class="mono text-[var(--accent)]">$0</span> annual lease payment
</p>
</div>
</div>
</div>
</main>
</div>
<script>
// State initialization
const state = {
assetCost: 500000,
usefulLife: 5,
salvageValue: 50000,
wacc: 10,
taxRate: 25,
leasePayment: 120000,
leaseTerm: 5,
deprMethod: 'straight',
sensWacc: 10,
sensTax: 25,
sensLease: 100,
sensSalvage: 100
};
// MACRS 5-year rates
const MACRS_5 = [0.20, 0.32, 0.192, 0.1152, 0.1152, 0.0576];
// Format currency
function formatCurrency(val) {
const absVal = Math.abs(val);
const sign = val < 0 ? '-' : '';
if (absVal >= 1000000) {
return sign + '$' + (absVal / 1000000).toFixed(2) + 'M';
} else if (absVal >= 1000) {
return sign + '$' + (absVal / 1000).toFixed(1) + 'K';
}
return sign + '$' + absVal.toFixed(0);
}
// Calculate depreciation schedule
function getDepreciationSchedule(cost, life, method) {
const schedule = [];
const safeCost = Math.max(0, cost);
const safeLife = Math.max(1, life);
if (method === 'straight') {
const annual = safeCost / safeLife;
for (let i = 0; i < safeLife; i++) {
schedule.push(annual);
}
} else if (method === 'double') {
let bookValue = safeCost;
const rate = 2 / safeLife;
for (let i = 0; i < safeLife; i++) {
const depr = Math.min(bookValue * rate, bookValue);
schedule.push(depr);
bookValue -= depr;
}
} else if (method === 'macrs5') {
const years = Math.min(safeLife, 6);
for (let i = 0; i < years; i++) {
schedule.push(safeCost * MACRS_5[i]);
}
}
return schedule;
}
// Calculate NPV
function calculateNPV(cashFlows, rate) {
const safeRate = Math.max(0.001, rate);
return cashFlows.reduce((npv, cf, t) => {
return npv + cf / Math.pow(1 + safeRate, t);
}, 0);
}
// Main calculation function
function calculate() {
const cost = state.assetCost;
const life = Math.max(1, state.usefulLife);
const salvage = state.salvageValue * (state.sensSalvage / 100);
const wacc = Math.max(0.001, state.sensWacc / 100);
const tax = Math.max(0, Math.min(1, state.sensTax / 100));
const leasePmt = state.leasePayment * (state.sensLease / 100);
const leaseTerm = Math.max(1, state.leaseTerm);
const deprMethod = state.deprMethod;
// Depreciation schedule
const deprSchedule = getDepreciationSchedule(cost, life, deprMethod);
const maxYears = Math.max(life, leaseTerm);
// Cash flows arrays
const buyCF = [];
const leaseCF = [];
const deprShields = [];
const diffCF = [];
// Year 0: Initial investment for buy
buyCF.push(-cost);
leaseCF.push(0);
deprShields.push(0);
diffCF.push(cost); // Lease saves initial outlay
// Years 1 to maxYears
for (let t = 1; t <= maxYears; t++) {
// Buy cash flows
let buyYearCF = 0;
// Depreciation tax shield
const depr = t <= deprSchedule.length ? deprSchedule[t - 1] : 0;
const deprShield = depr * tax;
deprShields.push(deprShield);
buyYearCF += deprShield;
// Salvage value at end of life
if (t === life) {
// After-tax salvage
const bookValue = cost - deprSchedule.reduce((a, b, i) => i < t ? a + b : a, 0);
const taxOnSalvage = Math.max(0, salvage - bookValue) * tax;
buyYearCF += salvage - taxOnSalvage;
}
buyCF.push(buyYearCF);
// Lease cash flows
let leaseYearCF = 0;
if (t <= leaseTerm) {
// After-tax lease payment (tax deductible)
leaseYearCF = -leasePmt * (1 - tax);
}
leaseCF.push(leaseYearCF);
// Incremental (Lease - Buy)
diffCF.push(leaseYearCF - buyYearCF);
}
// Calculate NPVs
const buyNPV = calculateNPV(buyCF, wacc);
const leaseNPV = calculateNPV(leaseCF, wacc);
const incrementalNPV = calculateNPV(diffCF, wacc);
// Update display
updateDisplay(buyNPV, leaseNPV, incrementalNPV, buyCF, leaseCF, deprShields, diffCF, maxYears);
// Update charts
drawComparisonChart(buyCF, leaseCF, maxYears);
drawTornadoChart();
drawBreakevenChart();
populateCashFlowTable(buyCF, leaseCF, deprShields, diffCF, maxYears);
}
function updateDisplay(buyNPV, leaseNPV, incrementalNPV, buyCF, leaseCF, deprShields, diffCF, maxYears) {
const recommendation = document.getElementById('recommendation');
const savingsAmount = document.getElementById('savingsAmount');
// NPV comparison (lower cost is better)
const leaseIsBetter = leaseNPV > buyNPV;
if (leaseIsBetter) {
recommendation.textContent = 'LEASE';
recommendation.className = 'result-badge result-lease';
savingsAmount.textContent = formatCurrency(leaseNPV - buyNPV);
savingsAmount.style.color = 'var(--accent)';
} else {
recommendation.textContent = 'BUY';
recommendation.className = 'result-badge result-buy';
savingsAmount.textContent = formatCurrency(buyNPV - leaseNPV);
savingsAmount.style.color = '#3b82f6';
}
document.getElementById('buyNPV').textContent = formatCurrency(-buyNPV);
document.getElementById('leaseNPV').textContent = formatCurrency(-leaseNPV);
document.getElementById('incrementalNPV').textContent = formatCurrency(incrementalNPV);
}
function populateCashFlowTable(buyCF, leaseCF, deprShields, diffCF, maxYears) {
const tbody = document.getElementById('cashFlowTable');
tbody.innerHTML = '';
for (let t = 0; t <= maxYears; t++) {
const row = document.createElement('tr');
row.className = 'border-b border-[var(--border)] last:border-0';
const buyVal = buyCF[t] || 0;
const leaseVal = leaseCF[t] || 0;
const deprVal = deprShields[t] || 0;
const diffVal = diffCF[t] || 0;
row.innerHTML = `
<td class="py-2 mono text-[var(--muted)]">${t}</td>
<td class="py-2 mono text-right ${buyVal >= 0 ? 'text-[var(--accent)]' : 'text-[var(--danger)]'}">${formatCurrency(buyVal)}</td>
<td class="py-2 mono text-right text-[var(--accent)]">${formatCurrency(deprVal)}</td>
<td class="py-2 mono text-right ${leaseVal >= 0 ? 'text-[var(--accent)]' : 'text-[var(--danger)]'}">${formatCurrency(leaseVal)}</td>
<td class="py-2 mono text-right font-semibold ${diffVal >= 0 ? 'text-[var(--accent)]' : 'text-[var(--danger)]'}">${formatCurrency(diffVal)}</td>
`;
tbody.appendChild(row);
}
}
function drawComparisonChart(buyCF, leaseCF, maxYears) {
const canvas = document.getElementById('comparisonChart');
const ctx = canvas.getContext('2d');
const dpr = window.devicePixelRatio || 1;
canvas.width = canvas.offsetWidth * dpr;
canvas.height = 200 * dpr;
ctx.scale(dpr, dpr);
const width = canvas.offsetWidth;
const height = 200;
const padding = { top: 20, right: 20, bottom: 30, left: 60 };
const chartWidth = width - padding.left - padding.right;
const chartHeight = height - padding.top - padding.bottom;
ctx.clearRect(0, 0, width, height);
// Calculate cumulative NPV for each year
const buyCumulative = [];
const leaseCumulative = [];
let buySum = 0, leaseSum = 0;
for (let t = 0; t <= maxYears; t++) {
buySum += (buyCF[t] || 0);
leaseSum += (leaseCF[t] || 0);
buyCumulative.push(buySum);
leaseCumulative.push(leaseSum);
}
const allValues = [...buyCumulative, ...leaseCumulative];
const minVal = Math.min(...allValues);
const maxVal = Math.max(...allValues);
const range = Math.max(Math.abs(maxVal - minVal), 1);
// Draw grid
ctx.strokeStyle = '#2d3a4f';
ctx.lineWidth = 1;
for (let i = 0; i <= 4; i++) {
const y = padding.top + (chartHeight / 4) * i;
ctx.beginPath();
ctx.moveTo(padding.left, y);
ctx.lineTo(width - padding.right, y);
ctx.stroke();
}
// Draw axes labels
ctx.fillStyle = '#8896ab';
ctx.font = '10px "Space Mono", monospace';
ctx.textAlign = 'right';
for (let i = 0; i <= 4; i++) {
const y = padding.top + (chartHeight / 4) * i;
const val = maxVal - (range / 4) * i;
ctx.fillText(formatCurrency(val), padding.left - 8, y + 3);
}
// X-axis labels
ctx.textAlign = 'center';
for (let t = 0; t <= maxYears; t++) {
const x = padding.left + (chartWidth / maxYears) * t;
ctx.fillText(t.toString(), x, height - 8);
}
// Draw lines
function drawLine(data, color) {
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.beginPath();
data.forEach((val, i) => {
const x = padding.left + (chartWidth / Math.max(1, maxYears)) * i;
const y = padding.top + chartHeight - ((val - minVal) / range) * chartHeight;
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.stroke();
}
drawLine(buyCumulative, '#3b82f6');
drawLine(leaseCumulative, '#10b981');
// Draw points
function drawPoints(data, color) {
ctx.fillStyle = color;
data.forEach((val, i) => {
const x = padding.left + (chartWidth / Math.max(1, maxYears)) * i;
const y = padding.top + chartHeight - ((val - minVal) / range) * chartHeight;
ctx.beginPath();
ctx.arc(x, y, 4, 0, Math.PI * 2);
ctx.fill();
});
}
drawPoints(buyCumulative, '#3b82f6');
drawPoints(leaseCumulative, '#10b981');
}
function drawTornadoChart() {
const canvas = document.getElementById('tornadoChart');
const ctx = canvas.getContext('2d');
const dpr = window.devicePixelRatio || 1;
canvas.width = canvas.offsetWidth * dpr;
canvas.height = 180 * dpr;
ctx.scale(dpr, dpr);
const width = canvas.offsetWidth;
const height = 180;
const padding = { top: 10, right: 30, bottom: 10, left: 100 };
const chartWidth = width - padding.left - padding.right;
const chartHeight = height - padding.top - padding.bottom;
ctx.clearRect(0, 0, width, height);
// Calculate base NPV difference
const baseNPV = calculateCurrentNPVDiff();
// Variables to test
const variables = [
{ name: 'WACC', base: state.wacc, low: state.wacc * 0.7, high: state.wacc * 1.3 },
{ name: 'Tax Rate', base: state.taxRate, low: state.taxRate * 0.5, high: Math.min(40, state.taxRate * 1.5) },
{ name: 'Lease Payment', base: state.leasePayment, low: state.leasePayment * 0.8, high: state.leasePayment * 1.2 },
{ name: 'Salvage Value', base: state.salvageValue, low: state.salvageValue * 0.5, high: state.salvageValue * 1.5 }
];
const impacts = variables.map(v => {
const lowNPV = calculateNPVDiffWithVar(v.name, v.low);
const highNPV = calculateNPVDiffWithVar(v.name, v.high);
return {
name: v.name,
lowImpact: lowNPV - baseNPV,
highImpact: highNPV - baseNPV
};
});
// Sort by absolute range
impacts.sort((a, b) =>
Math.abs(b.highImpact - b.lowImpact) - Math.abs(a.highImpact - a.lowImpact)
);
const maxImpact = Math.max(...impacts.flatMap(i => [Math.abs(i.lowImpact), Math.abs(i.highImpact)]));
const scale = Math.max(maxImpact, 1);
const barHeight = chartHeight / impacts.length - 10;
const centerY = padding.left + chartWidth / 2;
// Draw center line
ctx.strokeStyle = '#2d3a4f';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(centerY, padding.top);
ctx.lineTo(centerY, height - padding.bottom);
ctx.stroke();
impacts.forEach((impact, i) => {
const y = padding.top + i * (chartHeight / impacts.length) + 5;
// Draw label
ctx.fillStyle = '#8896ab';
ctx.font = '11px "DM Sans", sans-serif';
ctx.textAlign = 'right';
ctx.fillText(impact.name, padding.left - 10, y + barHeight / 2 + 4);
// Low impact bar (negative)
const lowWidth = (impact.lowImpact / scale) * (chartWidth / 2);
ctx.fillStyle = 'rgba(239, 68, 68, 0.7)';
ctx.fillRect(centerY + Math.min(0, lowWidth), y, Math.abs(lowWidth), barHeight);
// High impact bar (positive)
const highWidth = (impact.highImpact / scale) * (chartWidth / 2);
ctx.fillStyle = 'rgba(16, 185, 129, 0.7)';
ctx.fillRect(centerY, y, Math.max(0, highWidth), barHeight);
});
// Draw scale
ctx.fillStyle = '#8896ab';
ctx.font = '9px "Space Mono", monospace';
ctx.textAlign = 'center';
ctx.fillText('-' + formatCurrency(scale), centerY - chartWidth / 4, height - 2);
ctx.fillText('0', centerY, height - 2);
ctx.fillText(formatCurrency(scale), centerY + chartWidth / 4, height - 2);
}
function calculateCurrentNPVDiff() {
const originalSens = { ...state };
const npv = calculateNPVDiffInternal();
Object.assign(state, originalSens);
return npv;
}
function calculateNPVDiffWithVar(varName, value) {
const originalSens = { ...state };
switch(varName) {
case 'WACC': state.sensWacc = value; break;
case 'Tax Rate': state.sensTax = value; break;
case 'Lease Payment': state.sensLease = (value / state.leasePayment) * 100; break;
case 'Salvage Value': state.sensSalvage = (value / state.salvageValue) * 100; break;
}
const npv = calculateNPVDiffInternal();
Object.assign(state, originalSens);
return npv;
}
function calculateNPVDiffInternal() {
const cost = state.assetCost;
const life = Math.max(1, state.usefulLife);
const salvage = state.salvageValue * (state.sensSalvage / 100);
const wacc = Math.max(0.001, state.sensWacc / 100);
const tax = Math.max(0, Math.min(1, state.sensTax / 100));
const leasePmt = state.leasePayment * (state.sensLease / 100);
const leaseTerm = Math.max(1, state.leaseTerm);
const deprSchedule = getDepreciationSchedule(cost, life, state.deprMethod);
const maxYears = Math.max(life, leaseTerm);
const diffCF = [];
diffCF.push(cost); // Year 0: Lease saves purchase cost
for (let t = 1; t <= maxYears; t++) {
const depr = t <= deprSchedule.length ? deprSchedule[t - 1] : 0;
const deprShield = depr * tax;
let buyYearCF = deprShield;
if (t === life) {
const bookValue = cost - deprSchedule.reduce((a, b, i) => i < t ? a + b : a, 0);
const taxOnSalvage = Math.max(0, salvage - bookValue) * tax;
buyYearCF += salvage - taxOnSalvage;
}
let leaseYearCF = t <= leaseTerm ? -leasePmt * (1 - tax) : 0;
diffCF.push(leaseYearCF - buyYearCF);
}
return calculateNPV(diffCF, wacc);
}
function drawBreakevenChart() {
const canvas = document.getElementById('breakevenChart');
const ctx = canvas.getContext('2d');
const dpr = window.devicePixelRatio || 1;
canvas.width = canvas.offsetWidth * dpr;
canvas.height = 200 * dpr;
ctx.scale(dpr, dpr);
const width = canvas.offsetWidth;
const height = 200;
const padding = { top: 20, right: 20, bottom: 40, left: 60 };
const chartWidth = width - padding.left - padding.right;
const chartHeight = height - padding.top - padding.bottom;
ctx.clearRect(0, 0, width, height);
// Calculate NPV diff for different lease payments
const originalSensLease = state.sensLease;
const leasePayments = [];
const npvDiffs = [];
const minLease = state.leasePayment * 0.5;
const maxLease = state.leasePayment * 1.5;
for (let pmt = minLease; pmt <= maxLease; pmt += (maxLease - minLease) / 20) {
state.sensLease = (pmt / state.leasePayment) * 100;
leasePayments.push(pmt);
npvDiffs.push(calculateNPVDiffInternal());
}
state.sensLease = originalSensLease;
const minNPV = Math.min(...npvDiffs);
const maxNPV = Math.max(...npvDiffs);
const range = Math.max(Math.abs(maxNPV - minNPV), 1);
// Draw grid
ctx.strokeStyle = '#2d3a4f';
ctx.lineWidth = 1;
for (let i = 0; i <= 4; i++) {
const y = padding.top + (chartHeight / 4) * i;
ctx.beginPath();
ctx.moveTo(padding.left, y);
ctx.lineTo(width - padding.right, y);
ctx.stroke();
}
// Draw zero line
const zeroY = padding.top + chartHeight - ((-minNPV) / range) * chartHeight;
ctx.strokeStyle = 'rgba(239, 68, 68, 0.5)';
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(padding.left, zeroY);
ctx.lineTo(width - padding.right, zeroY);
ctx.stroke();
ctx.setLineDash([]);
// Y-axis labels
ctx.fillStyle = '#8896ab';
ctx.font = '10px "Space Mono", monospace';
ctx.textAlign = 'right';
for (let i = 0; i <= 4; i++) {
const y = padding.top + (chartHeight / 4) * i;
const val = maxNPV - (range / 4) * i;
ctx.fillText(formatCurrency(val), padding.left - 8, y + 3);
}
// X-axis label
ctx.textAlign = 'center';
ctx.fillText('Annual Lease Payment', width / 2, height - 5);
// Draw line
ctx.strokeStyle = '#10b981';
ctx.lineWidth = 2;
ctx.beginPath();
npvDiffs.forEach((npv, i) => {
const x = padding.left + (chartWidth / (npvDiffs.length - 1)) * i;
const y = padding.top + chartHeight - ((npv - minNPV) / range) * chartHeight;
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.stroke();
// Fill area
ctx.fillStyle = 'rgba(16, 185, 129, 0.1)';
ctx.beginPath();
npvDiffs.forEach((npv, i) => {
const x = padding.left + (chartWidth / (npvDiffs.length - 1)) * i;
const y = padding.top + chartHeight - ((npv - minNPV) / range) * chartHeight;
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.lineTo(width - padding.right, zeroY);
ctx.lineTo(padding.left, zeroY);
ctx.closePath();
ctx.fill();
// Find breakeven point
let breakeven = null;
for (let i = 1; i < npvDiffs.length; i++) {
if ((npvDiffs[i-1] >= 0 && npvDiffs[i] < 0) || (npvDiffs[i-1] < 0 && npvDiffs[i] >= 0)) {
// Linear interpolation
const ratio = npvDiffs[i-1] / (npvDiffs[i-1] - npvDiffs[i]);
breakeven = leasePayments[i-1] + ratio * (leasePayments[i] - leasePayments[i-1]);
break;
}
}
if (breakeven !== null) {
const beX = padding.left + ((breakeven - minLease) / (maxLease - minLease)) * chartWidth;
ctx.fillStyle = '#f59e0b';
ctx.beginPath();
ctx.arc(beX, zeroY, 6, 0, Math.PI * 2);
ctx.fill();
document.getElementById('breakevenPoint').textContent = formatCurrency(breakeven);
} else {
document.getElementById('breakevenPoint').textContent = 'N/A';
}
}
// Event listeners for inputs
const inputMappings = {
assetCost: 'assetCost',
usefulLife: 'usefulLife',
salvageValue: 'salvageValue',
wacc: 'wacc',
taxRate: 'taxRate',
leasePayment: 'leasePayment',
leaseTerm: 'leaseTerm',
deprMethod: 'deprMethod'
};
Object.keys(inputMappings).forEach(id => {
const el = document.getElementById(id);
if (el) {
el.addEventListener('input', (e) => {
const value = e.target.type === 'number' ? parseFloat(e.target.value) || 0 : e.target.value;
state[id] = value;
// Sync sensitivity sliders with main inputs
if (id === 'wacc') {
state.sensWacc = value;
document.getElementById('sensWacc').value = value;
document.getElementById('sensWaccVal').textContent = value + '%';
}
if (id === 'taxRate') {
state.sensTax = value;
document.getElementById('sensTax').value = value;
document.getElementById('sensTaxVal').textContent = value + '%';
}
calculate();
});
}
});
// Sensitivity sliders
document.getElementById('sensWacc').addEventListener('input', (e) => {
state.sensWacc = parseFloat(e.target.value);
document.getElementById('sensWaccVal').textContent = state.sensWacc + '%';
calculate();
});
document.getElementById('sensTax').addEventListener('input', (e) => {
state.sensTax = parseFloat(e.target.value);
document.getElementById('sensTaxVal').textContent = state.sensTax + '%';
calculate();
});
document.getElementById('sensLease').addEventListener('input', (e) => {
state.sensLease = parseFloat(e.target.value);
document.getElementById('sensLeaseVal').textContent = state.sensLease + '%';
calculate();
});
document.getElementById('sensSalvage').addEventListener('input', (e) => {
state.sensSalvage = parseFloat(e.target.value);
document.getElementById('sensSalvageVal').textContent = state.sensSalvage + '%';
calculate();
});
// Handle resize
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(calculate, 100);
});
// Initial calculation
calculate();
</script>
</body>
</html>
How It Works:
Financial Methodology
-
Buy Analysis: Calculates the present value of purchasing the asset, including:
- Initial outlay (Year 0)
- Depreciation tax shields (using straight-line, double declining, or MACRS)
- After-tax salvage value at end of life
-
Lease Analysis: Calculates the present value of leasing, including:
- After-tax lease payments (lease payments are tax-deductible)
- No depreciation benefits (lessor retains these)
-
Incremental Approach: Shows the difference (Lease - Buy) to identify which option creates more value
Key Features:
- Real-time recalculation as you adjust any parameter
- Sensitivity sliders to stress-test your decision
- Tornado chart showing which variables have the biggest impact
- Break-even analysis showing the exact lease payment where you'd be indifferent
- Detailed cash flow table for audit trail
Decision Rule:
- If Incremental NPV > 0: Lease (it's cheaper to lease)
- If Incremental NPV < 0: Buy (it's cheaper to own)