Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
// Interactive forestry financial model simulator.
// It will use React for the UI, recharts for data visualization, and custom slider components.
// The server will render the initial HTML, and the client-side JavaScript will handle the interactivity.
/** @jsxImportSource https://esm.sh/react */
import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from "https://esm.sh/recharts";
// Custom Slider component
const Slider = ({ value, onChange, min, max, step }) => (
<input
type="range"
min={min}
max={max}
step={step}
value={value}
onChange={(e) => onChange(Number(e.target.value))}
className="w-full"
/>
);
function App() {
const [landSize, setLandSize] = useState(1000);
const [sections, setSections] = useState(5);
const [cycleLength, setCycleLength] = useState(25);
const [timberId, setTimberId] = useState(200);
const [harvestCost, setHarvestCost] = useState(30);
const [timberPrice, setTimberPrice] = useState(150);
const [annualCosts, setAnnualCosts] = useState(100);
const [disasterChance, setDisasterChance] = useState(1);
const calculateYearlyData = () => {
let data = [];
const initialInvestment = landSize * 5000;
let cumulativeProfit = -initialInvestment;
let sectionSize = landSize / sections;
for (let year = 0; year <= cycleLength; year++) {
let annualRevenue = 0;
let annualCost = landSize * annualCosts;
if (Math.random() * 100 < disasterChance) {
annualCost += landSize * 1000;
}
if (year % (cycleLength / sections) === 0 && year !== 0) {
const harvestedTrees = sectionSize * timberId;
const harvestRevenue = harvestedTrees * timberPrice;
const harvestCosts = harvestedTrees * harvestCost;
annualRevenue += harvestRevenue - harvestCosts;
}
cumulativeProfit += annualRevenue - annualCost;
data.push({
year,
cumulativeProfit: Math.round(cumulativeProfit),
annualRevenue: Math.round(annualRevenue),
annualCost: Math.round(annualCost)
});
}
return data;
};
const data = calculateYearlyData();
return (
<div className="max-w-3xl mx-auto p-4">
<h1 className="text-2xl font-bold mb-4">Advanced Forestry Financial Model Explorer</h1>
<div className="space-y-4 mb-8">
<div>
<label className="block text-sm font-medium text-gray-700">Land Size (acres): {landSize}</label>
<Slider value={landSize} onChange={setLandSize} min={100} max={5000} step={100} />
</div>
<div>
<label className="block text-sm font-medium text-gray-700">Number of Sections: {sections}</label>
<Slider value={sections} onChange={setSections} min={1} max={10} step={1} />
</div>
<div>
<label className="block text-sm font-medium text-gray-700">Cycle Length (years): {cycleLength}</label>
<Slider value={cycleLength} onChange={setCycleLength} min={10} max={50} step={1} />
</div>
<div>
<label className="block text-sm font-medium text-gray-700">Timber Density (trees/acre): {timberId}</label>
<Slider value={timberId} onChange={setTimberId} min={50} max={500} step={10} />
</div>
<div>
<label className="block text-sm font-medium text-gray-700">Harvest Cost ($/tree): {harvestCost}</label>
<Slider value={harvestCost} onChange={setHarvestCost} min={10} max={100} step={5} />
</div>
<div>
<label className="block text-sm font-medium text-gray-700">Timber Price ($/tree): {timberPrice}</label>
<Slider value={timberPrice} onChange={setTimberPrice} min={50} max={300} step={10} />
</div>
<div>
<label className="block text-sm font-medium text-gray-700">Annual Costs ($/acre): {annualCosts}</label>
<Slider value={annualCosts} onChange={setAnnualCosts} min={50} max={500} step={10} />
</div>
<div>
jbwinters-forestryfinancialmodel.web.val.run
September 4, 2024