private function show($mysqli) { $categories = Db\InvestmentsApi::getCategories($mysqli); $res = array(); foreach ($categories as $i) { $res[] = array('term' => $i->name, 'definition' => $i->description, 'icon' => $i->icon); } echo json_encode(array("DefList", $res)); }
public function yearReport($mysqli, $year) { echo '<h1>' . $year . ' Annual Report</h1>'; $data = Db\AnnualApi::getYear($mysqli, $year); echo '<h2>Income</h2>'; echo '<table border=1 style="border-collapse: collapse;"><tr><th>Key</th><th>Value</th></tr>'; foreach ($data as $key => $value) { printf('<tr><td>%s</td><td>%s</td>', $key, $value); } echo '</table>'; $this->renderAnnualPie($year); $categories = Db\InvestmentsApi::getCategories($mysqli); $data = Db\InvestmentsApi::getInvestments($mysqli, $year, $year); echo '<h2>Investments</h2>'; echo '<table border=1 style="border-collapse: collapse;"><tr><th>Month</th>'; foreach ($categories as $i) { printf('<th>%s</th>', $i->name); } echo '<th>Total</th></tr>'; for ($i = 1; $i <= 12; $i++) { if (empty($data[$year][$i])) { continue; } echo '<tr><td>' . date("F", mktime(0, 0, 0, $i, 1, $year)) . '</td>'; foreach ($categories as $category) { if (isset($data[$year][$i][$category->key])) { $value = $data[$year][$i][$category->key]; } else { $value = ''; } printf('<td>%s</td>', $value); } printf('<td>%s</td>', array_sum($data[$year][$i])); echo '</tr>'; } echo '</table>'; $id = 'canvaschart' . uniqid(); echo '<div class="canvascontainer line"><canvas data-src="/data/investments/year/' . $year . '" id="' . $id . '"></div>'; }
private function multiYear($mysqli, $mode, $start_year, $end_year) { $segmented = Db\InvestmentsApi::getInvestmentsSegmented($mysqli, $start_year, $end_year); if (!$segmented) { // year() is better at showing empty data $this->year($mysqli, $start_year); return; } if ($mode == self::MODE_RETIREMENT) { $categories = array((object) array('name' => 'Non-retirement', 'key' => 'NO_RET'), (object) array('name' => 'Retirement', 'key' => 'RET')); } else { if ($mode == self::MODE_TAX) { $categories = array((object) array('name' => 'Post-tax', 'key' => 'POST_TAX'), (object) array('name' => 'Pre-tax', 'key' => 'PRE_TAX')); } else { if ($mode == self::MODE_TAX_RET) { $categories = array((object) array('name' => 'Non-retirement funds', 'key' => 'NO_RET'), (object) array('name' => 'Pre-tax retirement funds', 'key' => 'PRE_TAX'), (object) array('name' => 'Post-tax retirement funds', 'key' => 'POST_TAX')); } else { $categories = Db\InvestmentsApi::getCategories($mysqli); } } } $cdata = $this->getBaseData($categories); // compute date range $start_month = $end_month = null; for ($year = $start_year; $year <= $end_year; $year++) { for ($i = 1; $i <= 12; $i++) { if (isset($segmented[$year][$i])) { if (!$start_month) { $start_month = $year * 12 + $i - 1; } $end_month = $year * 12 + $i - 1; } } } // transform $segmented to $data depending on $mode $data = array(); foreach ($segmented as $year => $i) { foreach ($i as $month => $j) { foreach ($j as $key => $k) { if ($mode == self::MODE_RETIREMENT) { if (empty($data[$year][$month])) { $data[$year][$month] = array('RET' => 0, 'NO_RET' => 0); } $data[$year][$month]['RET'] += $k->value_ret; $data[$year][$month]['NO_RET'] += $k->value_no_ret; } else { if ($mode == self::MODE_TAX) { if (empty($data[$year][$month])) { $data[$year][$month] = array('PRE_TAX' => 0, 'POST_TAX' => 0); } $data[$year][$month]['PRE_TAX'] += $k->value_ret_pretax; $data[$year][$month]['POST_TAX'] += $k->value_posttax; } else { if ($mode == self::MODE_TAX_RET) { if (empty($data[$year][$month])) { $data[$year][$month] = array('NO_RET' => 0, 'PRE_TAX' => 0, 'POST_TAX' => 0); } $data[$year][$month]['NO_RET'] += $k->value_no_ret; $data[$year][$month]['PRE_TAX'] += $k->value_ret_pretax; $data[$year][$month]['POST_TAX'] += $k->value_ret_posttax; } else { $data[$year][$month][$key] = $k->value; } } } } } } // redo labels $labels = array(); for ($i = $start_month; $i <= $end_month; $i++) { $labels[] = $cdata['labels'][$i % 12] . ' ' . intval($i / 12); } $cdata['labels'] = $labels; // format data for chart $max_value = 0; for ($i = count($categories) - 1; $i >= 0; $i--) { $category = $categories[$i]; for ($j = $start_month; $j <= $end_month; $j++) { if (isset($data[intval($j / 12)][$j % 12 + 1][$category->key])) { $value = $data[intval($j / 12)][$j % 12 + 1][$category->key]; } else { $value = null; } $cdata['datasets'][$i]['itemData'][$j - $start_month] = $value; if ($value !== null && isset($cdata['datasets'][$i + 1]['data'][$j - $start_month])) { $value += $cdata['datasets'][$i + 1]['data'][$j - $start_month]; } $cdata['datasets'][$i]['data'][$j - $start_month] = $value; $max_value = max($max_value, $value); } } echo json_encode(array("Line", $cdata, $this->getOptions($max_value, $start_year + 1 >= $end_year), $this->getExtra($categories))); }