(function () {
if (typeof Chart === 'undefined') { console.error('Chart.js failed to load'); return; }
const RATES = {"1Y":{"M":{"N":[3.7e-05,4.3e-05,4.6e-05,4.9e-05,5e-05,4.9e-05,4.8e-05,4.7e-05,4.4e-05,4.1e-05,4e-05,3.8e-05,3.8e-05,4e-05,4.2e-05,4.5e-05,4.9e-05,5.3e-05,5.6e-05,5.9e-05,6.2e-05,6.5e-05,6.8e-05,7.5e-05,8.2e-05,9e-05,9.9e-05,0.000108,0.000115,0.000123,0.000132,0.000141,0.000152,0.000167,0.000184,0.000204,0.000226,0.000251,0.000285,0.000324,0.000367,0.000417,0.000472,0.000533,0.000602,0.000678,0.000763,0.000856,0.000951,0.001058,0.001183,0.001332,0.001514,0.001715,0.001944,0.002203,0.002496,0.002828,0.00319,0.003598,0.004059,0.00458,0.005139,0.005759,0.00644,0.00718,0.007981,0.009084,0.01034,0.011769,0.013396,0.015248,0.017082,0.018933,0.020758,0.022733,0.024866,0.027165,0.029637,0.032291,0.035132,0.038165],"S":[5.5e-05,6.5e-05,7.2e-05,7.7e-05,7.9e-05,7.8e-05,7.7e-05,7.6e-05,7.2e-05,6.8e-05,6.6e-05,6.4e-05,6.4e-05,6.7e-05,7.2e-05,7.8e-05,8.5e-05,9.3e-05,9.8e-05,0.000104,0.00011,0.000116,0.000123,0.000135,0.000149,0.000164,0.00018,0.000198,0.000212,0.000227,0.000244,0.000262,0.000282,0.000312,0.000345,0.000382,0.000424,0.000472,0.000536,0.000609,0.000692,0.000785,0.000888,0.001004,0.001133,0.001276,0.001434,0.001608,0.001784,0.001982,0.002212,0.002487,0.002819,0.003187,0.003601,0.004068,0.004595,0.005189,0.005831,0.006552,0.00736,0.008266,0.009021,0.009818,0.010646,0.01197,0.013418,0.014656,0.016541,0.018669,0.02107,0.023781,0.026641,0.029527,0.032374,0.035454,0.038781,0.042367,0.046223,0.050361,0.054792,0.059523]},"F":{"N":[2.4e-05,2.4e-05,2.5e-05,2.5e-05,2.6e-05,2.6e-05,2.7e-05,2.7e-05,2.7e-05,2.7e-05,2.8e-05,2.8e-05,2.9e-05,3.1e-05,3.2e-05,3.5e-05,3.8e-05,4e-05,4.2e-05,4.4e-05,4.5e-05,4.7e-05,4.8e-05,5.3e-05,5.8e-05,6.3e-05,6.9e-05,7.5e-05,8.2e-05,8.9e-05,9.6e-05,0.000104,0.000113,0.000122,0.000133,0.000144,0.000156,0.00017,0.000185,0.000201,0.000218,0.000237,0.000258,0.00029,0.000328,0.000375,0.000433,0.000503,0.000579,0.00067,0.000778,0.000904,0.001051,0.001198,0.001365,0.001556,0.001773,0.00202,0.002294,0.002606,0.00296,0.003361,0.003797,0.004283,0.004817,0.0054,0.006034,0.006714,0.00747,0.008312,0.009248,0.01029,0.011655,0.01319,0.014912,0.01684,0.018994,0.021392,0.023854,0.026285,0.028899,0.0317],"S":[3.8e-05,3.9e-05,4e-05,4.1e-05,4.2e-05,4.3e-05,4.4e-05,4.6e-05,4.6e-05,4.7e-05,4.8e-05,4.9e-05,5e-05,5.3e-05,5.7e-05,6.2e-05,6.7e-05,7.3e-05,7.6e-05,7.9e-05,8.3e-05,8.6e-05,8.9e-05,9.8e-05,0.000108,0.000119,0.00013,0.000142,0.000155,0.000168,0.000182,0.000198,0.000215,0.000234,0.000254,0.000276,0.0003,0.000326,0.000355,0.000385,0.000418,0.000454,0.000495,0.000556,0.000629,0.000719,0.000828,0.00096,0.001103,0.001274,0.001476,0.00171,0.001983,0.002253,0.002559,0.002907,0.0033,0.003746,0.004236,0.004789,0.005413,0.006117,0.006699,0.007316,0.007956,0.008967,0.010073,0.011067,0.012212,0.013475,0.014869,0.016407,0.018583,0.02103,0.023776,0.02685,0.030284,0.034107,0.038032,0.041909,0.046077,0.050543]}},"5Y":{"M":{"N":[0.54,0.568,0.58,0.588,0.6,0.588,0.576,0.564,0.53,0.525,0.515,0.51,0.508,0.544,0.58,0.61,0.66,0.7,0.736,0.781,0.836,0.902,0.983,1.078,1.173,1.271,1.37,1.47,1.575,1.698,1.843,2.014,2.216,2.476,2.76,3.096,3.487,3.945,4.476,5.071,5.738,6.484,7.315,8.236,9.24,10.334,11.546,12.912,14.491,16.324,18.451,20.899,23.692,26.846,30.386,34.356,38.81,43.812,49.358,55.524,62.344,69.835,77.997,87.465,98.46,111.249,126.168,143.608,162.804,183.427,205,227.409,250.492,274.692,300.381,328.06,357.818,389.736,0,null,null,null],"S":[0.8352,0.8891476793,0.9179338843,0.9364444444,0.9630252101,0.9526113537,0.9399272727,0.9292571429,0.8806965174,0.8767766497,0.8661363636,0.8667487685,0.8688224299,0.9383406114,1.0084897959,1.0663358779,1.1591397849,1.2362711864,1.3081806452,1.3958297872,1.503375,1.6307210526,1.7831714976,1.9612951542,2.1441680162,2.3305626168,2.5191854419,2.7143941842,2.9148190045,3.151393007,3.431875,3.759625,4.1446087889,4.6425,5.1816,5.8152,6.5588238128,7.4244708029,8.424,9.5468234737,10.8040468423,12.2048941525,13.7636236877,15.490495338,17.364,19.4008490478,21.647250052,24.1752,27.0812262339,30.4473078506,34.3340278356,38.7884288011,43.8465194489,49.5352619346,55.8808643867,62.964,70.8640696308,79.6752,88.8712797822,98.4408,108.2650107403,119.3300582514,131.6941869288,145.2168076583,161.3531444926,180.6058024999,202.4467535868,227.3160342149,256.0831416371,287.2513735898,320.1444876639,354.6680476834,390.6692476861,428.4119192166,468.4773761558,511.6462642329,558.0575357677,607.8376238455,0,0,0,0]},"F":{"N":[0.297,0.302,0.309,0.314,0.319,0.321,0.326,0.328,0.333,0.343,0.355,0.372,0.39,0.42,0.44,0.46,0.49,0.517,0.536,0.562,0.595,0.638,0.691,0.755,0.83,0.9,0.98,1.05,1.16,1.24,1.34,1.45,1.57,1.74,1.891,2.054,2.232,2.426,2.637,2.889,3.194,3.571,4.041,4.629,5.323,6.144,7.111,8.241,9.556,11.042,12.71,14.577,16.663,18.988,21.619,24.597,27.967,31.778,36.043,40.816,46.123,51.979,58.394,65.395,73.044,81.432,90.667,100.881,112.74,126.468,142.308,160.528,181.418,204.787,230.38,257.676,286.617,317.112,0,null,null,null],"S":[0.4790322581,0.4913492063,0.5030232558,0.517740458,0.5300676692,0.5413880597,0.5537205882,0.5650218978,0.5749640288,0.5924545455,0.6164527027,0.6504,0.6830909091,0.7445454545,0.7882352941,0.8252261307,0.8862200957,0.9415091743,0.9795044248,1.0315189873,1.0999203187,1.1858736059,1.291766323,1.4174056604,1.5643227666,1.7,1.8527007299,1.9893497758,2.2001652893,2.3593129771,2.5549647887,2.7705357143,3.0060329341,3.336,3.6260164975,3.9400327103,4.2816,4.6504332344,5.0556496815,5.5380498339,6.1240330579,6.8468165323,7.7436502375,8.859651633,10.1732177638,11.7216,13.5380192373,15.6540602796,18.1088839779,20.8696439904,23.953646148,27.3876727033,31.2044254645,35.4345070779,40.1948281528,45.5460889843,51.5612312709,58.3216658863,65.4092370489,72.8001731052,80.4020513581,88.931657817,98.4257257819,108.9105330852,120.6601392468,133.9062711213,148.0687136533,163.2673921192,181.3045322387,202.4687971535,227.1895334385,255.9436658224,289.2508979546,326.5102923426,367.3160489727,410.8374056047,456.981248965,505.6029678418,0,0,0,0]}},"10Y":{"M":{"N":[0.545,0.568,0.58,0.588,0.6,0.588,0.576,0.564,0.53,0.5304,0.552,0.5784,0.5845356,0.6189876,0.6591816,0.7051176,0.7567956,0.8142156,0.8773776,0.9451332,1.0186308,1.0990188,1.1862972,1.2827628,1.3884156,1.5055524,1.63647,1.7823168,1.946538,2.141766,2.3725944,2.6424684,2.9594268,3.4764,3.9156,4.4172,4.986,5.6304,6.3564,7.1556,8.0364,9.0156,10.1136,11.364,12.7824,14.3928,16.2228,18.3024,20.6688,23.3556,26.4036,29.8548,33.7524,38.1024,42.9552,48.3504,54.3228,60.9048,68.412,76.992,86.7972,98.0016,110.8032,125.1348,140.9436,158.1252,176.7888,197.0508,218.748,241.9044,266.5308,292.614,320.1144,null,null,null,null,null,null,null,null,null],"S":[0.8631167401,0.9073085339,0.9354424779,0.9548378378,0.9806896552,0.969442623,0.9586382979,0.9470943396,0.8985348837,0.906,0.9492,1.002,1.0186308,1.0863864,1.1644776,1.2529044,1.3516668,1.4607648,1.5813468,1.7122644,1.8535176,2.0074032,2.1750696,2.3576652,2.560932,2.7860184,3.0363696,3.3165792,3.6312408,4.0033224,4.4420112,4.9564944,5.5571076,6.534,7.3644,8.31,9.3828,10.5948,11.958,13.4556,15.1032,16.9272,18.9696,21.2868,23.9064,26.868,30.2184,34.0116,38.3088,43.1652,48.6492,54.8268,61.7616,69.204,77.1612,85.6152,95.0976,105.6852,117.0448867353,129.8969625702,144.4365679653,160.8885508148,179.5062279727,200.6504775678,224.3024225698,250.3762833189,278.5579,308.9939136185,342.2475387447,377.865,415.8966241629,456.3635715314,499.2540596114,0,0,0,0,0,0,0,0,0]},"F":{"N":[0.3096,0.3144,0.3192,0.324,0.3312,0.3384,0.3492,0.3624,0.378,0.396,0.4164,0.4368,0.4398372,0.4616568,0.4869216,0.51678,0.5489352,0.5845356,0.6247296,0.6706656,0.7223436,0.780912,0.8463708,0.9210168,1.0002564,1.0863864,1.1794068,1.2793176,1.3884156,1.5067008,1.6353216,1.7754264,1.9281636,2.1888,2.3904,2.6244,2.9016,3.234,3.6336,4.1064,4.6692,5.3412,6.1416,7.0932,8.1828,9.4272,10.8444,12.4524,14.2728,16.3308,18.654,21.2724,24.2208,27.516,31.218,35.3604,39.9732,45.0864,50.7192,56.9304,63.7776,71.3232,79.638,89.0676,99.756,111.87,125.598,141.15,158.7636,178.4244,199.992,223.5732,249.2652,null,null,null,null,null,null,null,null,null],"S":[0.5112,0.5232,0.5352,0.5472,0.5616,0.5796,0.6024,0.63,0.6624,0.6984,0.7368,0.7788,0.7878024,0.83259,0.884268,0.9428364,1.0082952,1.0806444,1.159884,1.2506076,1.3528152,1.4665068,1.5951276,1.739826,1.8960084,2.0636748,2.2439736,2.4392016,2.6505072,2.8801872,3.12939,3.4004124,3.6944028,4.1964,4.5828,5.0328,5.5644,6.198,6.9588,7.8564,8.9232,10.1928,11.7,13.4856,15.522,17.838,20.4636,23.43,26.7732,30.5328,34.7508,39.4752,44.7636,50.4228,56.4984,62.9748,70.2468,78.3744,87.1602436972,96.7308601785,107.1543319875,118.5005031252,130.8473569733,145.1076562215,161.5644466863,180.5478721843,202.0069790964,226.2599528595,253.9075819418,284.8930662399,319.0134716852,356.4635734565,397.42725177,0,0,0,0,0,0,0,0,0]}},"20Y":{"M":{"N":[0.5484,0.568,0.58,0.5898,0.6054,0.6246,0.6492,0.6798,0.7164,0.759,0.8082,0.8634,0.869688,0.933984,1.005612,1.0857,1.175376,1.275204,1.386876,1.516032,1.665492,1.837512,2.03604,2.263896,2.522208,2.815488,3.14712,3.521616,3.943488,4.414992,4.942332,5.535096,6.20682,7.4202,8.349,9.405,10.6044,11.9664,13.5126,15.2556,17.22,19.4352,21.933,24.7332,27.8688,31.3716,35.2728,39.6036,44.5404,50.1738,56.6004,63.9282,72.2778,81.6186,91.9494,103.2378,115.5558,128.9778,143.58,159.4482,176.664,195.3078,215.4588,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"S":[0.906,0.9466666667,0.9753173777,1.0002,1.035,1.077,1.1286,1.1904,1.2636,1.3476,1.443,1.5498,1.568484,1.691436,1.829616,1.983588,2.155044,2.34624,2.559996,2.807028,3.091848,3.420096,3.797412,4.228872,4.718988,5.273964,5.901132,6.608388,7.403628,8.290236,9.280056,10.390008,11.644908,13.9104,15.6354,17.589,19.8006,22.3032,25.1334,28.3104,31.8762,35.877,40.3656,45.2454,50.5338,56.2416,62.658,69.8484,77.6768384055,86.5310775373,96.5428735769,107.8576594909,120.6338992907,134.9272213999,150.7318041064,167.995737273,186.82775,207.339560756,229.6462399146,253.8809907753,280.1666207064,308.6260950509,339.3801650302,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},"F":{"N":[0.363,0.3756,0.3894,0.4032,0.42,0.4392,0.4614,0.4866,0.5154,0.5484,0.5856,0.6264,0.63168,0.679056,0.73038,0.787344,0.84882,0.915372,0.988692,1.069344,1.157892,1.255464,1.362624,1.481064,1.614732,1.767012,1.94298,2.148276,2.389668,2.669976,2.99766,3.382308,3.833508,4.641,5.2866,6.0258,6.873,7.8432,8.9532,10.2186,11.6616,13.3068,15.1812,17.3046,19.7004,22.3938,25.4088,28.7694,32.496,36.6306,41.2158,46.2978,51.9294,58.2918,65.487,73.6152,82.7856,93.1182,104.7414,117.6774,131.8848,147.4482,164.4516,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"S":[0.624,0.651,0.6792,0.7086,0.7428,0.7824,0.828,0.8796,0.9372,1.0026,1.0752,1.1556,1.1703,1.26336,1.365444,1.476552,1.597248,1.72866,1.871352,2.028708,2.201292,2.390232,2.597784,2.826768,3.08508,3.378924,3.717324,4.110996,4.572348,5.10702,5.730804,6.46062,7.313388,8.841,10.0524,11.4354,13.014,14.814,16.866,19.1946,21.837,24.834,28.2318,31.9542,36.0102,40.4064,45.3552,50.9022,56.9667253038,63.6318262878,70.9525639255,78.9878381754,87.8054545689,97.7651921234,109.031384808,121.7612959762,136.1268445875,152.3171371124,170.5338648226,190.8119474425,213.0838803977,237.48203408,264.1373104357,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}}};
const PERIODS = { "1Y": 5, "5Y": 5, "10Y": 10, "20Y": 20 };
const PLAN_LABEL = { "1Y": "1 年續保", "5Y": "5 年續保", "10Y": "10 年續保", "20Y": "20 年續保" };
const COL_ANNUAL = "#6B6E88";
const COL_LOCKED = "#FF0068";
let state = { plan: "5Y", gender: "M", smoking: "N", age: 30, sa: 1000000, view: "cum" };
let chart = null;
function getRate(plan, g, s, age) {
const idx = age - 18;
const arr = RATES[plan][g][s];
if (idx < 0 || idx >= arr.length) return null;
const v = arr[idx];
return (v == null || v === 0) ? null : v;
}
function premium(plan, g, s, age, sa) {
const r = getRate(plan, g, s, age);
if (r == null) return null;
return plan === "1Y" ? r * sa * 12 : r * sa / 1000;
}
function maxStartAgeFor(plan, g, s) {
const period = PERIODS[plan];
for (let a = 99; a >= 18; a--) {
const lockedOK = plan === "1Y" ? true : (getRate(plan, g, s, a) != null);
let annualOK = true;
for (let i = 0; i < period; i++) {
if (getRate("1Y", g, s, a + i) == null) { annualOK = false; break; }
}
if (lockedOK && annualOK) return a;
}
return 18;
}
function buildSeries() {
const { plan, gender, smoking, age, sa } = state;
const period = PERIODS[plan];
const labels = [], annualSeries = [], lockedSeries = [];
let annualCum = 0, lockedCum = 0;
const annualCumSeries = [], lockedCumSeries = [];
for (let i = 0; i < period; i++) {
const a = age + i;
labels.push(a + " 歲");
const ay = premium("1Y", gender, smoking, a, sa);
annualSeries.push(ay);
annualCum += (ay || 0);
annualCumSeries.push(annualCum);
if (plan !== "1Y") {
const ly = premium(plan, gender, smoking, age, sa);
lockedSeries.push(ly);
lockedCum += (ly || 0);
lockedCumSeries.push(lockedCum);
}
}
return { labels, annualSeries, lockedSeries, annualCumSeries, lockedCumSeries };
}
function fmt(v) { return "HK$" + Math.round(v).toLocaleString(); }
function renderLegend() {
const el = document.getElementById("btpc-legend");
const showLocked = state.plan !== "1Y";
let html = `
1 年續保(每年重新計算)`;
if (showLocked) {
html += `
${PLAN_LABEL[state.plan]}(鎖定 ${PERIODS[state.plan]} 年)`;
}
el.innerHTML = html;
}
function renderCallout(series) {
const el = document.getElementById('btpc-callout');
if (!el) return;
const period = PERIODS[state.plan];
const annTotal = series.annualCumSeries[period - 1];
if (state.plan === '1Y') {
el.className = 'chart-callout scheme-gray';
el.innerHTML = `
${period} 年總保費
${fmt(annTotal)}
平均 ${fmt(annTotal / period)} / 年
`;
return;
}
const lockTotal = series.lockedCumSeries[period - 1];
const saving = annTotal - lockTotal;
const cheaper = saving >= 0;
const pct = annTotal > 0 ? Math.abs(saving / annTotal * 100) : 0;
el.className = 'chart-callout ' + (cheaper ? '' : 'scheme-red');
el.innerHTML = `
${cheaper ? `${period} 年總保費節省(選 ${PLAN_LABEL[state.plan]})` : `${period} 年總保費差額(選 ${PLAN_LABEL[state.plan]} 多付)`}
${fmt(Math.abs(saving))}
${cheaper ? '慳' : '多付'} ${pct.toFixed(1)}%
`;
}
function renderChart(series) {
if (chart) { chart.destroy(); chart = null; }
const cum = state.view === "cum";
const annualData = cum ? series.annualCumSeries : series.annualSeries;
const lockedData = cum ? series.lockedCumSeries : series.lockedSeries;
const showLocked = state.plan !== "1Y";
const datasets = [{
label: "1 年續保 " + (cum ? "累計" : "每年"),
data: annualData,
borderColor: COL_ANNUAL,
backgroundColor: cum ? "rgba(107,110,136,0.07)" : COL_ANNUAL,
pointBackgroundColor: COL_ANNUAL,
pointBorderColor: COL_ANNUAL,
tension: 0, borderWidth: 2.6, pointRadius: 5, pointHoverRadius: 8,
fill: cum && !showLocked
}];
if (showLocked) {
datasets.push({
label: PLAN_LABEL[state.plan] + " " + (cum ? "累計" : "每年"),
data: lockedData,
borderColor: COL_LOCKED,
backgroundColor: COL_LOCKED,
pointBackgroundColor: COL_LOCKED,
pointBorderColor: COL_LOCKED,
tension: 0, borderWidth: 3, pointRadius: 5, pointHoverRadius: 8, fill: false
});
}
const allVals = [].concat(annualData, lockedData || []).filter(v => v != null);
const yMax = Math.max(...allVals) * 1.10;
const yMin = cum ? 0 : Math.min(...allVals) * 0.90;
chart = new Chart(document.getElementById("btpc-chart"), {
type: "line",
data: { labels: series.labels, datasets },
options: {
responsive: true, maintainAspectRatio: false,
layout: { padding: { top: 20, right: 24, left: 8, bottom: 8 } },
plugins: {
legend: { display: false },
tooltip: { callbacks: { label: (ctx) => ctx.dataset.label + ": HK$" + Math.round(ctx.parsed.y).toLocaleString() } }
},
scales: {
x: { grid: { display: false }, ticks: { font: { size: 12 }, color: '#6B6E88' } },
y: {
beginAtZero: cum, min: yMin, max: yMax,
ticks: { font: { size: 12 }, color: '#6B6E88', callback: (v) => "HK$" + Math.round(v).toLocaleString() },
grid: { color: 'rgba(25,19,87,0.07)' },
title: { display: true, text: (cum ? "累計保費 (HK$)" : "每年保費 (HK$)"), font: { size: 12 }, color: '#6B6E88' }
}
}
}
});
}
function render() {
const series = buildSeries();
renderLegend();
renderCallout(series);
renderChart(series);
}
function clampAge() {
const maxA = maxStartAgeFor(state.plan, state.gender, state.smoking);
if (state.age > maxA) state.age = maxA;
if (state.age < 18) state.age = 18;
const ageInput = document.getElementById("btpc-fAge");
ageInput.max = maxA;
ageInput.value = state.age;
}
document.getElementById("btpc-tabs").addEventListener("click", (e) => {
const btn = e.target.closest(".tab");
if (!btn) return;
document.querySelectorAll("#btpc-tabs .tab").forEach(b => b.classList.remove("active"));
btn.classList.add("active");
state.plan = btn.dataset.plan;
clampAge(); render();
});
document.getElementById("btpc-fGender").addEventListener("change", (e) => { state.gender = e.target.value; clampAge(); render(); });
document.getElementById("btpc-fSmoking").addEventListener("change", (e) => { state.smoking = e.target.value; clampAge(); render(); });
document.getElementById("btpc-fAge").addEventListener("change", (e) => { state.age = parseInt(e.target.value, 10) || 30; clampAge(); render(); });
document.getElementById("btpc-fSa").addEventListener("change", (e) => { state.sa = parseInt(e.target.value, 10) || 1000000; render(); });
document.getElementById("btpc-fView").addEventListener("change", (e) => { state.view = e.target.value; render(); });
clampAge();
render();
})();