/** * @param int $year the year for which to get investment allocations. * Reports data from the latest month in [$year - 1, $year] that has data. */ private function actual($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; } $colors = new Util\Colors(count($allocations)); $cdata = array(); foreach ($allocations as $i) { $cdata[] = array('label' => $i->name, 'color' => $colors->getNext(), 'value' => round(100 * @$data[$year_index][$month_index][$i->key] / $total, 1)); } $options = array('tooltipTemplate' => "<%if (label){%><%=label%>: <%}%><%= value %>% (\$<%= Math.round(value * {$total} / 100000) %>k)", 'legendTemplate' => "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"border-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>", 'responsive' => true); $extra = array('headerText' => 'Actual allocations'); echo json_encode(array("Pie", $cdata, $options, $extra)); }
private function getBaseData($categories) { $colors = new Util\Colors(count($categories)); $cdata = array('labels' => array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), 'datasets' => array()); foreach ($categories as $i => $category) { $cdata['datasets'][$i] = array('label' => $category->name, 'fillColor' => $colors->getNext(), 'pointColor' => $colors->getCurrent(), 'strokeColor' => 'black', 'data' => array(), 'itemData' => array()); } return $cdata; }
private function incomeVsSavings($mysqli, $start_year, $end_year, $show_expenses = false) { $data = Db\AnnualApi::getMultiYear($mysqli, $start_year, $end_year); $colors = new Util\Colors(3); $color1 = $colors->getNext(); $color2 = $colors->getNext(); $color3 = $colors->getNext(); $cdata = array('labels' => array(), 'datasets' => array(array('label' => $show_expenses ? 'Expenses' : 'Gross Income', 'fillColor' => $show_expenses ? $color3 : $color1, 'data' => array()), array('label' => 'Savings', 'fillColor' => $color2, 'data' => array()))); $current_year = date("Y"); for ($i = $start_year; $i <= $end_year; $i++) { $gross_income = isset($data[$i]['GROSS_INCOME']) ? $data[$i]['GROSS_INCOME'] : 0; $investments = isset($data[$i]['SAVINGS']) ? $data[$i]['SAVINGS'] : 0; $expenses = $gross_income * 2 - array_sum($data[$i]); $cdata['labels'][] = $i . ($i >= $current_year ? ' (est)' : ''); $cdata['datasets'][0]['data'][] = $show_expenses ? $expenses : $gross_income; $cdata['datasets'][1]['data'][] = $investments; } $options = array('multiTooltipTemplate' => "<%= ' \$' + value.toFixed(2).replace(/(\\d)(?=(\\d{3})+\\.)/g, '\$1,') %>", 'legendTemplate' => "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"border-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>", 'responsive' => true); echo json_encode(array("Bar", $cdata, $options, null)); }