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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
| <template>
<div class="roi-calculator">
<h1>投资回报率计算器</h1>
<div class="calculator-form">
<div class="form-group">
<label>初始投资金额(元)</label>
<input
type="number"
v-model.number="form.principal"
@input="calculate"
>
</div>
<div class="form-group">
<label>年化收益率(%)</label>
<input
type="number"
v-model.number="form.rate"
step="0.1"
@input="calculate"
>
</div>
<div class="form-group">
<label>投资期限(年)</label>
<input
type="number"
v-model.number="form.years"
@input="calculate"
>
</div>
<div class="form-group">
<label>计算方式</label>
<select v-model="form.method" @change="calculate">
<option value="compound">复利</option>
<option value="simple">单利</option>
</select>
</div>
</div>
<div class="result-section" v-if="result.totalAmount > 0">
<h2>计算结果</h2>
<div class="result-grid">
<div class="result-item">
<span class="label">最终金额</span>
<span class="value">¥{{ formatCurrency(result.totalAmount) }}</span>
</div>
<div class="result-item">
<span class="label">总收益</span>
<span class="value profit">¥{{ formatCurrency(result.profit) }}</span>
</div>
<div class="result-item">
<span class="label">收益率</span>
<span class="value">{{ result.profitRate.toFixed(2) }}%</span>
</div>
</div>
<div class="chart-container">
<canvas ref="chartCanvas"></canvas>
</div>
</div>
</div>
</template>
<script setup>
import { ref, reactive, computed, nextTick, onMounted } from 'vue'
import { Chart } from 'chart.js'
// 响应式数据
const form = reactive({
principal: 10000,
rate: 5,
years: 1,
method: 'compound'
})
const result = reactive({
totalAmount: 0,
profit: 0,
profitRate: 0
})
const chartCanvas = ref(null)
let chartInstance = null
// 计算逻辑
const calculate = () => {
const { principal, rate, years, method } = form
if (!principal || !rate || !years) {
resetResult()
return
}
let totalAmount
if (method === 'compound') {
// 复利计算
totalAmount = principal * Math.pow(1 + rate / 100, years)
} else {
// 单利计算
totalAmount = principal * (1 + rate / 100 * years)
}
const profit = totalAmount - principal
const profitRate = (profit / principal) * 100
Object.assign(result, {
totalAmount,
profit,
profitRate
})
// 更新图表
updateChart()
}
const resetResult = () => {
Object.assign(result, {
totalAmount: 0,
profit: 0,
profitRate: 0
})
}
// 格式化货币
const formatCurrency = (value) => {
return new Intl.NumberFormat('zh-CN', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(value)
}
// 图表更新
const updateChart = async () => {
if (!chartCanvas.value) return
await nextTick()
const chartData = generateChartData()
if (chartInstance) {
chartInstance.data = chartData
chartInstance.update()
} else {
chartInstance = new Chart(chartCanvas.value, {
type: 'line',
data: chartData,
options: {
responsive: true,
plugins: {
title: {
display: true,
text: '投资增长曲线'
}
},
scales: {
y: {
beginAtZero: true,
ticks: {
callback: (value) => '¥' + formatCurrency(value)
}
}
}
}
})
}
}
// 生成图表数据
const generateChartData = () => {
const years = []
const amounts = []
for (let i = 0; i <= form.years; i++) {
years.push(`第${i}年`)
let amount
if (form.method === 'compound') {
amount = form.principal * Math.pow(1 + form.rate / 100, i)
} else {
amount = form.principal * (1 + form.rate / 100 * i)
}
amounts.push(amount)
}
return {
labels: years,
datasets: [{
label: '投资金额',
data: amounts,
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
tension: 0.1
}]
}
}
// 生命周期
onMounted(() => {
calculate()
})
</script>
<style scoped>
.roi-calculator {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.calculator-form {
background: #f5f5f5;
padding: 20px;
border-radius: 8px;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: 500;
}
.form-group input,
.form-group select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.result-section {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.result-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 30px;
}
.result-item {
text-align: center;
padding: 15px;
background: #f9f9f9;
border-radius: 8px;
}
.result-item .label {
display: block;
font-size: 14px;
color: #666;
margin-bottom: 5px;
}
.result-item .value {
display: block;
font-size: 24px;
font-weight: bold;
color: #333;
}
.result-item .profit {
color: #4caf50;
}
.chart-container {
position: relative;
height: 300px;
}
@media (max-width: 768px) {
.result-grid {
grid-template-columns: 1fr;
}
}
</style>
|