function get_cummulative_graph($year = 0, $antigens = 0, $district = 0, $facility = 0) { $graph_sub_title = ""; if ($year == 0) { $year = date('y'); } //Start creating the sql query $sql = "select reporting_period, "; $antigen_count = 0; $antigen_array = array(); $antigen_titles = array("dpt1_admin" => "DPT 1", "dpt2_admin" => "DPT 2", "dpt3_admin" => "DPT 3", "opv1_admin" => "OPV 1", "opv2_admin" => "OPV 2", "opv3_admin" => "OPV 3", "opv_birth_admin" => "OPV Birth", "pn1_admin" => "Pneumococal 1", "pn2_admin" => "Pneumococal 2", "pn3_admin" => "Pneumococal 3", "tt_pregnant" => "TT Pregnant Women", "tt_trauma" => "TT Trauma", "yellow_admin" => "Yellow Fever", "measles_admin" => "Measles", "bcg_admin" => "BCG"); $antigen_data = array(); $selected_antigens = array(); $this->load->database(); //If no antigens have been specified, get data for dpt1 and 3 if (strlen($antigens) == 1) { $selected_antigens = array("dpt1_admin", "dpt3_admin"); } else { $selected_antigens = explode("-", $antigens); array_pop($selected_antigens); } $counter = 1; //For each of the selected antigens, append it's retrieval chunk onto the sql query foreach ($selected_antigens as $selected_antigen) { array_push($antigen_array, $antigen_titles[$selected_antigen]); if (sizeof($selected_antigens) != $counter) { $sql .= "sum(" . $selected_antigen . ") as `" . $antigen_titles[$selected_antigen] . "`, "; } else { $sql .= "sum(" . $selected_antigen . ") as `" . $antigen_titles[$selected_antigen] . "` "; } $counter++; } //Finish creating the query based on whether the user is filtering down to a district or not if ($district == 0) { $graph_sub_title = "Nationwide"; $sql .= " from dhis_data where reporting_period like '%{$year}%' group by reporting_period"; } else { if ($district > 0) { if ($facility == 0) { $district_object = Districts::getDistrict($district); $graph_sub_title = "In " . $district_object->name . " District"; $sql .= " from dhis_data d left join facilities f on d.facility_code = f.facilitycode where reporting_period like '%{$year}%' and f.district = '" . $district . "' group by reporting_period"; } if ($facility > 0) { $facility_name = Facilities::getFacilityName($facility); $graph_sub_title = "In " . $facility_name; $sql .= " from dhis_data where reporting_period like '%{$year}%' and facility_code = '" . $facility . "' group by reporting_period"; } } } $query = $this->db->query($sql); $immunizations = $query->result_array(); foreach ($immunizations as $immunization) { foreach ($antigen_array as $antigen_dataset) { $antigen_data[$antigen_dataset][$immunization['reporting_period']] = $immunization["{$antigen_dataset}"]; } } $chart = '<chart caption="Immunization Data" subcaption="' . $graph_sub_title . ' For \'' . $year . '" connectNullData="1" showValues="0" formatNumberScale="0" lineDashGap="6" xAxisName="Month" yAxisName="Cummulative Immunized" showValues="0" showBorder="0" showAlternateHGridColor="0" divLineAlpha="10" bgColor="FFFFFF" exportEnabled="1" exportHandler="' . base_url() . 'Scripts/FusionCharts/ExportHandlers/PHP/FCExporter.php" exportAtClient="0" exportAction="download"> <categories> <category label="Jan"/> <category label="Feb"/> <category label="Mar"/> <category label="Apr"/> <category label="May"/> <category label="Jun"/> <category label="Jul"/> <category label="Aug"/> <category label="Sep"/> <category label="Oct"/> <category label="Nov"/> <category label="Dec"/> </categories>'; //Loop through all the months in the specified year $str_start = $year . "-01-01"; $str_end = $year . "-12-31"; $start = $month = strtotime($str_start); $end = strtotime($str_end); $loop_from = $str_start; $loop_to = $str_end; $days = array(); $counter = 0; //create an array of the various months while (strtotime($loop_from) <= strtotime($loop_to)) { $days[$counter] = date('M-y', strtotime($loop_from)); $loop_from = date("d-m-Y", strtotime("+1 month", strtotime($loop_from))); $counter++; } foreach ($antigen_array as $antigen_dataset) { $chart .= "<dataset seriesName='{$antigen_dataset}'>"; $antigen_dataset_data = $antigen_data[$antigen_dataset]; //Keep track of the cummulative totals $cummulative = 0; $counter = 0; while ($month < $end) { //get the description of the months $current_month = date('M-y', $month); if (isset($antigen_dataset_data[$current_month])) { //check if the next value is non-existent, if so, display a dotted line, else, display a kawaida line if (sizeof($antigen_dataset_data) != $counter) { $cummulative += $antigen_dataset_data[$current_month]; if (isset($antigen_dataset_data[$days[$counter + 1]])) { $chart .= '<set value="' . $cummulative . '"/>'; } else { $chart .= '<set value="' . $cummulative . '" dashed="1"/>'; } } } else { $chart .= '<set />'; } $counter++; $month = strtotime("+1 month", $month); } $start = $month = strtotime($str_start); $chart .= "</dataset>"; } $chart .= "</chart>"; echo $chart; }