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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> <title>复利增长模型</title> <style> html, body { height: 100%; margin: 0px; padding: 0px; background-color: #f5faff; } input{ width: 190px; height: 27px; background:none; outline:none; border:none; font-size: 14px; font-weight: 700; font-family: "Microsoft soft"; }
.inputContent { overflow: hidden; background-color: white; border-radius: 3px; outline-style: none ; border: 1px solid #ccc; margin: 5px auto; width: 300px; height: 30px; }
.inputLabel { padding: 0 5px; font-size: 14px; font-weight: 700; color: grey; }
.inputContentClick{ border-color: #66afe9 !important; box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)!important; }
.flexBottom{ position: fixed !important; bottom: 0px; } .fullScreen { width: 100%; height: 100%; margin: 0; padding: 0; background-color: #f5faff; position: fixed !important; bottom: 0px; z-index: 100; } .closeFullScreen { position: fixed; bottom: 0.5rem; right: 0.5rem } .openFullScreen { font-size: 1.5rem; user-select:none; position: fixed; top: 0.5rem; right: 0.5rem; z-index: 99; } .fullScreenShowDiv { height: calc(100% - 3rem); width: calc(100% - 2rem); margin: 1rem; }
.button{ width: 140px; line-height: 38px; text-align: center; font-weight: bold; color: #fff; text-shadow:1px 1px 1px #333; border-radius: 5px; margin:0 20px 20px 0; position: relative; overflow: hidden; } .button:nth-child(6n){ margin-right: 0; } .button.gray{ color: #8c96a0; text-shadow:1px 1px 1px #fff; border:1px solid #dce1e6; box-shadow: 0 1px 2px #fff inset,0 -1px 0 #a8abae inset; background: -webkit-linear-gradient(top,#f2f3f7,#e4e8ec); background: -moz-linear-gradient(top,#f2f3f7,#e4e8ec); background: linear-gradient(top,#f2f3f7,#e4e8ec); } .round,.side,.tags{ padding-right: 30px; } .tags:after{ font-weight: normal; position: absolute; display: inline-block; content: "ADD TO"; top:-3px; right: -33px; color: #fff; text-shadow:none; width: 85px; height:25px; line-height: 28px; -webkit-transform:rotate(45deg) scale(.7,.7); -moz-transform:rotate(45deg) scale(.7,.7); transform:rotate(45deg) scale(.7,.7); } .gray.tags:after{ background: #8c96a0; border:2px solid #fff; } </style> </head> <body> <div class="fullScreen"> <div class="fullScreenShowDiv"> <div id="fullScreenMain" style="transform:rotate(90deg);transform-origin:0 100%;position: relative;"></div> </div> <span class="closeFullScreen" onclick="document.getElementsByClassName('fullScreen')[0].style.display='none'"> <svg class="icon" style="width: 1em;height: 1em;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="569"><path d="M354.133333 682.666667H256v-42.666667h170.666667v170.666667H384v-98.133334L243.2 853.333333l-29.866667-29.866666L354.133333 682.666667z m358.4 0l140.8 140.8-29.866666 29.866666-140.8-140.8V810.666667h-42.666667v-170.666667h170.666667v42.666667h-98.133334zM354.133333 384L213.333333 243.2l29.866667-29.866667L384 354.133333V256h42.666667v170.666667H256V384h98.133333z m358.4 0H810.666667v42.666667h-170.666667V256h42.666667v98.133333L823.466667 213.333333l29.866666 29.866667L712.533333 384z" fill="#444444" p-id="570"></path></svg> </span> </div> <span class="openFullScreen" onclick="document.getElementsByClassName('fullScreen')[0].style.display=''"> <svg class="icon" style="width: 1em;height: 1em;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="533"><path d="M285.866667 810.666667H384v42.666666H213.333333v-170.666666h42.666667v98.133333l128-128 29.866667 29.866667-128 128z m494.933333 0l-128-128 29.866667-29.866667 128 128V682.666667h42.666666v170.666666h-170.666666v-42.666666h98.133333zM285.866667 256l128 128-29.866667 29.866667-128-128V384H213.333333V213.333333h170.666667v42.666667H285.866667z m494.933333 0H682.666667V213.333333h170.666666v170.666667h-42.666666V285.866667l-128 128-29.866667-29.866667 128-128z" fill="#444444" p-id="534"></path></svg> </span> <div id="main" style="width: 100%; height:calc(100% - 300px);clear: both"></div> <div style = "width: 100%; height: 256px" class="flexBottom"> <div style = "width: 305px; margin: auto"> <div class="inputContent"> <label for="name" class="inputLabel">组合名</label> <input id="name" onfocus="inputfocus(this)" onblur="inputBlur(this)"/> </div> <div class="inputContent"> <label for="principal" class="inputLabel">初始值</label> <input id="principal" onfocus="inputfocus(this)" onblur="inputBlur(this)"/> </div> <div class="inputContent"> <label for="interestRate" class="inputLabel">利率/年</label> <input id="interestRate" onfocus="inputfocus(this)" onblur="inputBlur(this)"/> </div> <div class="inputContent"> <label for="time" class="inputLabel">周期/月</label> <input id="time" onfocus="inputfocus(this)" onblur="inputBlur(this)"/> </div> <div class="inputContent"> <label for="append" class="inputLabel">周期追加</label> <input id="append" onfocus="inputfocus(this)" onblur="inputBlur(this)"/> </div> <div style="width: 325px"> <div style="margin: auto"> <button type="button" class="button gray" onclick="defaultShow()">重新展示</button> <button type="button" class="button gray tags" onclick="defaultAppend()">追加展示</button> </div> </div> </div> </div> </body> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script> <script> var fullScreenH = document.getElementsByClassName('fullScreenShowDiv')[0].offsetHeight; var fullScreenW = document.getElementsByClassName('fullScreenShowDiv')[0].offsetWidth; document.getElementById("fullScreenMain").style.height = fullScreenW + 'px'; document.getElementById("fullScreenMain").style.bottom = fullScreenW + 'px'; document.getElementById("fullScreenMain").style.width = fullScreenH + 'px';
document.getElementsByClassName('fullScreen')[0].style.display='none';
var nameIndex = 1; var myChart = echarts.init(document.getElementById('main')); var fullScreenChart = echarts.init(document.getElementById('fullScreenMain')); window.currentData = [];
/** * 计算周期值 */ function getCompoundInterest(principal, interestRate, time, append) { interestRate = formatRate(interestRate); interestRate = Math.pow(interestRate + 1, 1/12); append = isNumber(append) ? append : 0; var principals = [principal]; var title = [0]; var calculationProcess = [principal]; for (let i = 1; i <= time; i++) { calculationProcess[i] = principal + "*" + interestRate + "+" + append + "=" + principal; principal = principal * interestRate + append; principals[i] = Math.floor(principal * 100) / 100; title[i] = i; } var data = {}; data.legend = ["周期"]; data.xAxis = title; data.value = principals; return [principals, calculationProcess]; }
/** * 显示值到视图,覆盖之前的 */ function showCompoundInterest(principal, interestRate, time, append, name){ nameIndex = 1; var res = getCompoundInterest(principal, interestRate, time, append); window.currentData = null; window.currentData = [resToData(res[0], name, principal, interestRate, time, append)]; myChart.setOption(getOptionByData(window.currentData), true); fullScreenChart.setOption(getOptionByData(window.currentData), true); }
/** * 追加值到视图 */ function appendShowCompoundInterest(principal, interestRate, time, append, name){ var res = getCompoundInterest(principal, interestRate, time, append); var appendData = resToData(res[0], name, principal, interestRate, time, append); var flag = true; for (var i = 0; i < window.currentData.length; i++) { if(window.currentData[i].name == appendData.name) { window.currentData[i] = appendData; flag = false; } } if(flag) { window.currentData.push(appendData); } myChart.setOption(getOptionByData(window.currentData)); fullScreenChart.setOption(getOptionByData(window.currentData)); }
/** * 构造值 */ function resToData(res, name, principal, interestRate, time, append){ if(name==null){ name = "default" } return { name: name, data: res, principal: principal, interestRate: interestRate, time: time, append: append }; }
/** * 检测是否是数值型 */ function isNumber(value) { return typeof value === "number"; }
/** * 格式化利率 */ function formatRate(rate) { return rate == null? 1 :rate; } /** * 构造Echart配置 */ function getOptionByData(data) { var maxAxis = 0; var legends = []; var datas = []; var dataXAxis = []; for(let index=0; index<data.length; index++){ legends[index] = data[index].name; let one = data[index]; one.type = 'line'; datas[index] = one; maxAxis = one.data.length > maxAxis ? one.data.length : maxAxis; } for(let i=0;i<maxAxis;i++){ dataXAxis[i] = i; }
option = { title: { text: '增长趋势' }, tooltip: { trigger: 'axis' }, legend: { data: legends }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, toolbox: { feature: { saveAsImage: {show: false} } }, xAxis: { type: 'category', boundaryGap: false, data: dataXAxis }, yAxis: { type: 'value' }, series: datas };
console.log(option); return option; }
/** * 获取对象的值,空则返回默认值 */ function getInputValOrElse(id, defaultVal){ var ob = document.getElementById(id); var value = ob.value; ob.value = ""; if(value == null || value == ""){ value = defaultVal + ""; } return execStringExpression(value) }
/** * 获取名称的值,空则给默认值 */ function getNameOrDefault() { var ob = document.getElementById("name"); var value = ob.value; ob.value = ""; if(value == null || value == ""){ value = "未命名组合" + nameIndex++; } return value; }
/** * 将字符当做命令执行(输入框可以输入简单公式) */ function execStringExpression(expr, info) { let exprF = expr.replace(/\$\{([^}]+)\}/g, '${this[\'$1\'] || \'\'}'); const fn = new Function(`return \`${exprF}\``).bind(info || {}); try { return eval(fn()) } catch(e) { console.log(e); } return '' }
/** * 获取输入值并执行回调函数 */ function getValueOrDefault(doFun){ var name = getNameOrDefault(); var principal = getInputValOrElse("principal", 0); var interestRate = getInputValOrElse("interestRate", 0); var time = getInputValOrElse("time", 0); var append = getInputValOrElse("append", 0); doFun(parseInt(principal), parseFloat(interestRate), parseInt(time), parseFloat(append), name); }
/** * 展示方法入口 */ function defaultShow(){ getValueOrDefault(showCompoundInterest); }
/** * 追加方法入口 */ function defaultAppend(){ getValueOrDefault(appendShowCompoundInterest); }
/** * 点击趋势数据的时候回填输入信息 */ myChart.on('click', function (param) { var name = param.seriesName; for (var i = 0; i < window.currentData.length; i++) { if(window.currentData[i].name == name) { var crt = window.currentData[i]; document.getElementById("name").value = crt.name; document.getElementById("principal").value = crt.principal; document.getElementById("interestRate").value = crt.interestRate; document.getElementById("time").value = crt.time; document.getElementById("append").value = crt.append; break; } } });
function inputfocus(ob) { ob.parentNode.className = "inputContent inputContentClick"; }
function inputBlur(ob) { ob.parentNode.className = "inputContent"; }
showCompoundInterest(100, 0.8, 10, 0, "演示组合"); </script> </html>
|