/** * Creates pie chart image of opportunities by lead_source. * param $datax- the sales stage data to display in the x-axis * param $datay- the sum of opportunity amounts for each opportunity in each sales stage * to display in the y-axis * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.. * All Rights Reserved.. * Contributor(s): ______________________________________.. */ function pipeline_by_lead_source($legends = array('foo', 'bar'), $user_id = array('1'), $cache_file_name = 'a_file', $refresh = true, $width = 900, $height = 500) { global $log, $current_user; $log->debug("Entering pipeline_by_lead_source(" . $legends . ") method ..."); global $app_strings, $lang_crm, $current_module_strings, $log, $charset, $tmp_dir; global $theme; include_once 'Image/Graph.php'; include_once 'Image/Canvas.php'; $font = calculate_font_name($lang_crm); if (!file_exists($cache_file_name) || !file_exists($cache_file_name . '.map') || $refresh == true) { $log =& LoggerManager::getLogger('opportunity charts'); $log->debug("starting pipeline chart"); $log->debug("legends is:"); $log->debug($legends); $log->debug("user_id is: "); $log->debug($user_id); $log->debug("cache_file_name is: {$cache_file_name}"); //Now do the db queries //query for opportunity data that matches $legends and $user $where = ""; //build the where clause for the query that matches $datax $count = count($legends); if ($count > 0) { $where .= " leadsource in ( "; $ls_i = 0; foreach ($legends as $key => $value) { if ($ls_i != 0) { $where .= ", "; } $where .= "'" . addslashes($key) . "'"; $ls_i++; } $where .= ")"; } $opp = new Potentials(); $opp_list = $opp->get_full_list("vtiger_potential.amount DESC, vtiger_potential.closingdate DESC", $where); //build pipeline by lead source data $total = 0; $count = array(); $sum = array(); if (isset($opp_list)) { foreach ($opp_list as $record) { if (!isset($sum[$record->column_fields['leadsource']])) { $sum[$record->column_fields['leadsource']] = 0; } if (isset($record->column_fields['amount']) && isset($record->column_fields['leadsource'])) { // Strip all non numbers from this string. $amount = CurrencyField::convertFromMasterCurrency(preg_replace('/[^0-9]/', '', floor($record->column_fields['amount'])), $current_user->conv_rate); $sum[$record->column_fields['leadsource']] = $sum[$record->column_fields['leadsource']] + $amount / 1000; if (isset($count[$record->column_fields['leadsource']])) { $count[$record->column_fields['leadsource']]++; } else { $count[$record->column_fields['leadsource']] = 1; } $total = $total + $amount / 1000; } } } $visible_legends = array(); $data = array(); $aTargets = array(); $aAlts = array(); foreach ($legends as $lead_source_key => $lead_source_translation) { if (isset($sum[$lead_source_key])) { array_push($data, $sum[$lead_source_key]); if ($lead_source_key != '') { array_push($visible_legends, $lead_source_translation); } else { // put none in if the vtiger_field is blank. array_push($visible_legends, $current_module_strings['NTC_NO_LEGENDS']); } $cvid = getCvIdOfAll("Potentials"); array_push($aTargets, "index.php?module=Potentials&action=ListView&leadsource=" . urlencode($lead_source_key) . "&query=true&type=dbrd&viewname=" . $cvid); array_push($aAlts, $count[$lead_source_key] . " " . $current_module_strings['LBL_OPPS_IN_LEAD_SOURCE'] . " {$lead_source_translation}\t"); } } $log->debug("sum is:"); $log->debug($sum); $log->debug("count is:"); $log->debug($count); $log->debug("total is: {$total}"); if ($total == 0) { $log->debug("Exiting pipeline_by_lead_source method ..."); return $current_module_strings['ERR_NO_OPPS']; } if ($theme == "blue") { $font_color = "#212473"; } else { $font_color = "#000000"; } $canvas =& Image_Canvas::factory('png', array('width' => $width, 'height' => $height, 'usemap' => true)); $imagemap = $canvas->getImageMap(); $graph =& Image_Graph::factory('graph', $canvas); $font =& $graph->addNew('font', calculate_font_name($lang_crm)); // set the font size to 11 pixels $font->setSize(8); $font->setColor($font_color); $graph->setFont($font); // create the plotarea layout $title =& Image_Graph::factory('title', array('Test', 10)); $plotarea =& Image_Graph::factory('plotarea', array('category', 'axis')); $footer =& Image_Graph::factory('title', array('Footer', 8)); $graph->add(Image_Graph::vertical($title, Image_Graph::vertical($plotarea, $footer, 90), 5)); // Generate colours $colors = color_generator(count($visible_legends), '#33CCFF', '#3322FF'); $index = 0; $dataset =& Image_Graph::factory('dataset'); $fills =& Image_Graph::factory('Image_Graph_Fill_Array'); foreach ($visible_legends as $legend) { $dataset->addPoint($legend, $data[$index], array('url' => $aTargets[$index], 'alt' => $aAlts[$index])); $fills->addColor($colors[$index]); $log->debug('point =' . $legend . ',' . $data[$index]); $index++; } // create the pie chart and associate the filling colours $gbplot =& $plotarea->addNew('pie', $dataset); $plotarea->hideAxis(); $gbplot->setFillStyle($fills); // Setup title $titlestr = $current_module_strings['LBL_TOTAL_PIPELINE'] . $current_user->currency_symbol . $total . $app_strings['LBL_THOUSANDS_SYMBOL']; //$titlestr = $current_module_strings['LBL_TOTAL_PIPELINE'].$current_user->currency_symbol.$total; $title->setText($titlestr); // format the data values $valueproc =& Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted', $current_user->currency_symbol . "%d"); // set markers $marker =& $graph->addNew('value_marker', IMAGE_GRAPH_VALUE_Y); $marker->setDataPreprocessor($valueproc); $marker->setFillColor('#FFFFFF'); $marker->setBorderColor($font_color); $marker->setFontColor($font_color); $marker->setFontSize(8); $pointingMarker =& $graph->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$marker)); $gbplot->setMarker($pointingMarker); // set legend $legend_box =& $plotarea->addNew('legend'); $legend_box->setPadding(array('top' => 20, 'bottom' => 0, 'left' => 0, 'right' => 0)); $legend_box->setFillColor('#F5F5F5'); $legend_box->showShadow(); $subtitle = $current_module_strings['LBL_OPP_SIZE'] . $current_user->currency_symbol . $current_module_strings['LBL_OPP_SIZE_VALUE']; $footer->setText($subtitle); $footer->setAlignment(IMAGE_GRAPH_ALIGN_TOP_LEFT); $imgMap = $graph->done(array('tohtml' => true, 'border' => 0, 'filename' => $cache_file_name, 'filepath' => './', 'urlpath' => '')); //$imgMap = htmlspecialchars($output); save_image_map($cache_file_name . '.map', $imgMap); } else { $imgMap_fp = fopen($cache_file_name . '.map', "rb"); $imgMap = fread($imgMap_fp, filesize($cache_file_name . '.map')); fclose($imgMap_fp); } $fileModTime = filemtime($cache_file_name . '.map'); $return = "\n{$imgMap}"; $log->debug("Exiting pipeline_by_lead_source method ..."); return $return; }
/** Function to render the Horizontal Graph */ function pie_chart($referdata, $refer_code, $width, $height, $left, $right, $top, $bottom, $title, $target_val, $cache_file_name, $html_image_name) { global $log, $root_directory, $lang_crm, $theme; //We'll be getting the values in the form of a string separated by commas $datay = explode("::", $referdata); // The datay values $datax = explode("::", $refer_code); // The datax values $target_val = urldecode($target_val); $target = explode("::", $target_val); $alts = array(); $temp = array(); for ($i = 0; $i < count($datax); $i++) { $name = $datax[$i]; $pos = substr_count($name, " "); $alts[] = htmlentities($name) . "=%d"; //If the datax value of a string is greater, adding '\n' to it so that it'll come in 2nd line if (strlen($name) >= 14) { $name = substr($name, 0, 34); } if ($pos >= 2) { $val = explode(" ", $name); $n = count($val) - 1; $x = ""; for ($j = 0; $j < count($val); $j++) { if ($j != $n) { $x .= " " . $val[$j]; } else { $x .= "@#" . $val[$j]; } } $name = $x; } $name = str_replace("@#", "\n", $name); $temp[] = $name; } $datax = $temp; if ($theme == "blue") { $font_color = "#212473"; } else { $font_color = "#000000"; } $width = $width + 140; $canvas =& Image_Canvas::factory('png', array('width' => $width, 'height' => $height, 'usemap' => true)); $imagemap = $canvas->getImageMap(); $graph =& Image_Graph::factory('graph', $canvas); $font =& $graph->addNew('font', calculate_font_name($lang_crm)); // set the font size to 11 pixels $font->setSize(8); $font->setColor($font_color); $graph->setFont($font); // create the plotarea layout $title =& Image_Graph::factory('title', array($title, 10)); $plotarea =& Image_Graph::factory('plotarea', array('category', 'axis')); $footer =& Image_Graph::factory('title', array('Footer', 8)); $legend_box =& Image_Graph::factory('legend'); $graph->add(Image_Graph::vertical($title, $plotarea, 5)); // To create unique lables we need to keep track of lable name and its count $uniquex = array(); // Generate colours $colors = color_generator(count($datay), '#33DDFF', '#3322FF'); $dataset =& Image_Graph::factory('dataset'); $fills =& Image_Graph::factory('Image_Graph_Fill_Array'); $sum = 0; for ($i = 0; $i < count($datay); $i++) { if (isset($_REQUEST['display_view']) && $_REQUEST['display_view'] == 'MATRIX') { $datax[$i] = trim($datax[$i]); if (strlen($datax[$i]) <= 10) { $datax[$i] = $datax[$i]; } else { $datax[$i] = substr($datax[$i], 0, 10) . ".."; } } // To have unique names even in case of duplicates let us add the id $datalabel = $datax[$i]; $datax_appearance = $uniquex[$datax[$i]]; if ($datax_appearance == null) { $uniquex[$datax[$i]] = 1; } else { $datalabel = $datax[$i] . ' [' . $datax_appearance . ']'; $uniquex[$datax[$i]] = $datax_appearance + 1; } $dataset->addPoint($datalabel, $datay[$i], array('url' => $target[$i], 'alt' => $alts[$i])); $sum += $datay[$i]; $fills->addColor($colors[$i]); } // create an array with % values $pcvalues = array(); for ($i = 0; $i < count($datay); $i++) { $pcvalues[$datay[$i]] = sprintf('%0.1f%%', 100 * $datay[$i] / $sum); } // create the pie chart and associate the filling colours $gbplot =& $plotarea->addNew('pie', $dataset); $plotarea->setPadding(array('top' => 20, 'bottom' => 0, 'left' => 0, 'right' => 50)); $plotarea->hideAxis(); $gbplot->setFillStyle($fills); // format the data values $marker_array =& Image_Graph::factory('Image_Graph_DataPreprocessor_Array', array($pcvalues)); // set markers $marker =& $graph->addNew('value_marker', IMAGE_GRAPH_VALUE_Y); $marker->setDataPreprocessor($marker_array); $marker->setFillColor('#FFFFFF'); $marker->setBorderColor($font_color); $marker->setFontColor($font_color); $marker->setFontSize(8); $pointingMarker =& $graph->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$marker)); $gbplot->setMarker($pointingMarker); // set legend $legend_box =& $plotarea->addNew('legend'); $legend_box->setPadding(array('top' => 20, 'bottom' => 0, 'left' => 0, 'right' => 0)); $legend_box->setFillColor('#F5F5F5'); $legend_box->showShadow(); $img = $graph->done(array('tohtml' => true, 'border' => 0, 'filename' => $cache_file_name, 'filepath' => '', 'urlpath' => '')); save_image_map($cache_file_name . '.map', $img); return $img; }
public static function getPieChart($xaxisData, $yaxisData, $title = '', $width = '', $height = '', $charttype = 'vertical', $cachedFileName = false, $target = false, $color = '') { global $log, $lang_crm, $default_charset; require_once 'include/utils/utils.php'; require_once 'include/utils/GraphUtils.php'; include_once 'Image/Graph.php'; include_once 'Image/Canvas.php'; if ($cachedFileName === false) { $cache_file_name = 'cache/images/pie_chart_' . time() . '.png'; } else { $cache_file_name = $cachedFileName; } if (empty($width)) { $width = '500'; } if (empty($height)) { $height = '400'; } if ($target === false) { $target = array(); } $alts = array(); $temp = array(); for ($i = 0; $i < count($xaxisData); $i++) { $name = html_entity_decode($xaxisData[$i], ENT_QUOTES, $default_charset); $pos = substr_count($name, " "); $alts[] = $name; //If the datax value of a string is greater, adding '\n' to it so that it'll come in 2nd line if (strlen($name) >= 14) { $name = substr($name, 0, 34); } if ($pos >= 2) { $val = explode(" ", $name); $n = count($val) - 1; $x = ""; for ($j = 0; $j < count($val); $j++) { if ($j != $n) { $x .= " " . $val[$j]; } else { $x .= "@#" . $val[$j]; } } $name = $x; } $name = str_replace("@#", "\n", $name); $temp[] = $name; } $xaxisData = $temp; $width = $width + $width / 5; $canvas =& Image_Canvas::factory('png', array('width' => $width, 'height' => $height, 'usemap' => true)); $imagemap = $canvas->getImageMap(); $graph =& Image_Graph::factory('graph', $canvas); $font =& $graph->addNew('font', calculate_font_name($lang_crm)); $font->setSize(8); $font->setColor($color); $graph->setFont($font); // create the plotarea layout $title =& Image_Graph::factory('title', array($title, 10)); $plotarea =& Image_Graph::factory('plotarea', array('category', 'axis')); $graph->add(Image_Graph::vertical($title, $plotarea, 5)); // To create unique lables we need to keep track of lable name and its count $uniquex = array(); // Generate colours $colors = color_generator(count($yaxisData), '#33DDFF', '#3322FF'); $dataset =& Image_Graph::factory('dataset'); $fills =& Image_Graph::factory('Image_Graph_Fill_Array'); $sum = 0; $pcvalues = array(); for ($i = 0; $i < count($yaxisData); $i++) { $sum += $yaxisData[$i]; } for ($i = 0; $i < count($yaxisData); $i++) { // To have unique names even in case of duplicates let us add the id $datalabel = $xaxisData[$i]; $xaxisData_appearance = $uniquex[$xaxisData[$i]]; if ($xaxisData_appearance == null) { $uniquex[$xaxisData[$i]] = 1; } else { $datalabel = $xaxisData[$i] . ' [' . $xaxisData_appearance . ']'; $uniquex[$xaxisData[$i]] = $xaxisData_appearance + 1; } $dataset->addPoint($datalabel, $yaxisData[$i], array('url' => $target[$i], 'alt' => $alts[$i] . '=' . sprintf('%0.1f%%', 100 * $yaxisData[$i] / $sum))); $pcvalues[$yaxisData[$i]] = sprintf('%0.1f%%', 100 * $yaxisData[$i] / $sum); $fills->addColor($colors[$i]); } if ($sum == 0) { return null; } // create the pie chart and associate the filling colours $gbplot =& $plotarea->addNew('pie', $dataset); $plotarea->setPadding(array('top' => 0, 'bottom' => 0, 'left' => 0, 'right' => $width / 20)); $plotarea->hideAxis(); $gbplot->setFillStyle($fills); // format the data values $marker_array =& Image_Graph::factory('Image_Graph_DataPreprocessor_Array', array($pcvalues)); // set markers $marker =& $graph->addNew('value_marker', IMAGE_GRAPH_VALUE_Y); $marker->setDataPreprocessor($marker_array); $marker->setFillColor('#FFFFFF'); $marker->setBorderColor($color); $marker->setFontColor($color); $marker->setFontSize(8); $pointingMarker =& $graph->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$marker)); $gbplot->setMarker($pointingMarker); $legend_box =& $plotarea->addNew('legend'); $legend_box->setPadding(array('top' => 20, 'bottom' => 0, 'left' => 0, 'right' => 0)); $legend_box->setFillColor('#F5F5F5'); $legend_box->showShadow(); $img = $graph->done(array('tohtml' => true, 'border' => 0, 'filename' => $cache_file_name, 'filepath' => '', 'urlpath' => '')); return $img; }