/** * This table shows target-vs-actual allocations and info to help with rebalancing. * * @param int $year the year for which to report investment allocations. * Reports data from the latest month in [$year - 1, $year] that has data. */ private function rebalancetable($mysqli, $year) { $allocations = Db\TargetApi::getTargets($mysqli); $data = Db\InvestmentsApi::getInvestments($mysqli, $year - 1, $year); if (!$allocations || !$data) { $this->noDataChart(); return; } $year_index = isset($data[$year]) ? $year : $year - 1; $month_index = max(array_keys($data[$year_index])); $total = 0; foreach ($allocations as $i) { if (isset($data[$year_index][$month_index][$i->key])) { $total += $data[$year_index][$month_index][$i->key]; } } $total = (int) $total; if (!$total) { $this->noDataChart(); return; } $rows = array(); $rowSortData = array(); foreach ($allocations as $i) { $current = (int) @$data[$year_index][$month_index][$i->key]; $currentPercent = 100 * $current / $total; $target = $i->percent * $total / 100; $rows[] = array($i->name, $i->percent, round(100 * $current / $total, 2), '$ ' . round($current / 10) * 10, '$ ' . ($current < $target ? '(' : '') . abs(round(($current - $target) / 10) * 10) . ($current < $target ? ')' : ''), $current < $target ? 'Add funds' : 'Remove funds'); $rowSortData[] = array($i->name, $i->percent, round($current / $total, 5), $current, round($current - $target, 5), round($current - $target, 5)); } $tdata = array('headers' => array('Category', 'Target %', 'Actual %', '$ Actual', '$ Over (Under)', 'Action'), 'headerSortType' => array('', 'number', 'number', 'number', 'number', ''), 'rows' => $rows, 'rowSortData' => $rowSortData); echo json_encode(array("Table", $tdata, array('sortable' => true))); }
private function year($mysqli, $year) { $data = Db\InvestmentsApi::getInvestments($mysqli, $year, $year); $categories = Db\InvestmentsApi::getCategories($mysqli); $cdata = $this->getBaseData($categories); $max_value = 0; for ($i = count($categories) - 1; $i >= 0; $i--) { $category = $categories[$i]; for ($j = 0; $j < 12; $j++) { if (isset($data[$year][$j + 1][$category->key])) { $value = $data[$year][$j + 1][$category->key]; } else { $value = null; } if ($value !== null && isset($cdata['datasets'][$i + 1]['data'][$j])) { $value += $cdata['datasets'][$i + 1]['data'][$j]; } $cdata['datasets'][$i]['data'][$j] = $value; $max_value = max($max_value, $value); } } echo json_encode(array("Line", $cdata, $this->getOptions($max_value, true), $this->getExtra($categories))); }
private function threecell($mysqli) { $data = Db\InvestmentsApi::getInvestments($mysqli, 0, 3000); if (!$data) { echo json_encode("Table", null); return; } $tmp = array_keys($data); $newest_year = $tmp[count($tmp) - 1]; $tmp = array_keys($data[$newest_year]); $newest_month = $tmp[count($tmp) - 1]; $newest = $newest_year * 12 + $newest_month - 1; $back_three = $newest - 3; $newest_net = array_sum($data[$newest_year][$newest_month]); if ($newest_month > 3) { $back_three = @array_sum($data[$newest_year][$newest_month - 3]); } else { $back_three = @array_sum($data[$newest_year - 1][$newest_month + 9]); } $last_year = @array_sum($data[$newest_year - 1][$newest_month]); $tdata = array('headers' => array('3-month Growth', '1-year growth', 'Total savings & investments'), 'rows' => array(array($back_three ? '$' . number_format($newest_net - $back_three, 2) : '--', $last_year ? '$' . number_format($newest_net - $last_year, 2) : '--', '$' . number_format($newest_net, 2)))); echo json_encode(array("Table", $tdata)); }
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>'; }