Monday 16 January 2012

Sugarcrm custom chart

Creating a sugar chart dashlet is done the same way as you would create a simple dashlet, just extending 2 functions, for this reason I will not cover the way a sugar dashlet is created. Notes: The dashlets will be named  "CustomLineDashlet" and "CustomPieDashlet" and the module is "abc_Sample". You can use the predefined sugar chart definitions for parsing data (modules\Charts\chartdefs.php) or you can create your own to meet any custom demands. This is done by editing(creating if dose not exist) "custom\Charts\chartDefs.ext.php". For CustomPieDashlet : custom\Charts\chartDefs.ext.php:
$chartDefs['custom_pie_chart'] = array('type' => 'code',
 'id' => 'custom_pie_chart',
 'label' => 'custom_pie_chart label',
 'chartUnits' => 'The unit definition',
 'chartType' => 'pie chart',
 // important value that will be used to group the data
 'groupBy' => array( 'name' ), 
 'base_url'=> 
  array(  'module' => 'abc_Sample',
   'action' => 'index',
   'query' => 'true',
   'searchFormTab' => 'advanced_search',
   )   
);
custom\modules\abc_Sample\Dashlets\CustomPieDashlet\CustomPieDashlet.php
....
public function display() {
 $currency_symbol = $GLOBALS['sugar_config']['default_currency_symbol'];
 require ("modules/Charts/chartdefs.php");
 // here we load the chart definition (custom\Charts\chartDefs.ext.php)
 $chartDef = $chartDefs['custom_pie_chart'];
 require_once ('include/SugarCharts/SugarChart.php');
 $sugarChart = new SugarChart();
 $sugarChart -> setProperties('', translate('LBL_OPP_SIZE', 'Charts') . ' ' . currency_symbol . '1' . translate('LBL_OPP_THOUSANDS', 'Charts'), $chartDef['chartType']);
 $sugarChart -> base_url = $chartDef['base_url'];
 $sugarChart -> group_by = $chartDef['groupBy'];
 $sugarChart -> url_params = array();
 $sugarChart -> getData($this -> constructQuery());
 $xmlFile = $sugarChart -> getXMLFileName($this -> id);
 $sugarChart -> saveXMLFile($xmlFile, $sugarChart -> generateXML());
 return $this -> getTitle('
') . '
' . $sugarChart -> display($this -> id, $xmlFile, '100%', '480', false) . '
'; } protected function constructQuery() { // this is the query used to get data and create the xml // for pie chart we have a simple query $query = " SELECT count(*) as total, name FROM table GROUP BY name ORDER BY name"; return $query; }
For CustomLineDashlet : custom\Charts\chartDefs.ext.php:
$chartDefs['custom_line_chart'] = array(  'type' => 'code',
 'id' => 'custom_line_chart',
 'label' => 'custom_line_chart label',
 'chartUnits' => 'The unit definition',
 'chartType' => 'line chart',
 // the legend will be the name and the x intervals will be the date
 'groupBy' => array( 'date','name' ), 
 'base_url'=> 
  array(  'module' => 'sam_Sample',
   'action' => 'index',
   'query' => 'true',
   'searchFormTab' => 'advanced_search'
 )   
);
custom\modules\abc_Sample\Dashlets\CustomLineDashlet\CustomLineDashlet.php
....
public function display() {
 $currency_symbol = $GLOBALS['sugar_config']['default_currency_symbol'];
 require ("modules/Charts/chartdefs.php");
 // here we load the chart definition (custom\Charts\chartDefs.ext.php)
 $chartDef = $chartDefs['custom_line_chart'];
 require_once ('include/SugarCharts/SugarChart.php');
 $sugarChart = new SugarChart();
 $sugarChart -> setProperties('', translate('LBL_OPP_SIZE', 'Charts') . ' ' . currency_symbol . '1' . translate('LBL_OPP_THOUSANDS', 'Charts'), $chartDef['chartType']);
 $sugarChart -> base_url = $chartDef['base_url'];
 $sugarChart -> group_by = $chartDef['groupBy'];
 $sugarChart -> url_params = array();
 $sugarChart -> getData($this -> constructQuery());
 $xmlFile = $sugarChart -> getXMLFileName($this -> id);
 $sugarChart -> saveXMLFile($xmlFile, $sugarChart -> generateXML());
 return $this -> getTitle('
') . '
' . $sugarChart -> display($this -> id, $xmlFile, '100%', '480', false) . '
'; } protected function constructQuery() { // this is the query used to get data and create the xml // for pie chart we have a simple query $query = " SELECT count(*) as total, name, date FROM table GROUP BY date,name ORDER BY name"; return $query; }
the meta files ( custom\modules\abc_Sample\Dashlets\CustomLineDashlet\CustomPieDashlet.meta.php and custom\modules\abc_Sample\Dashlets\CustomLineDashlet\CustomLineDashlet.meta.php) will look like this:
$dashletMeta['CustomSamplePieCart'] = array('module'  => 'abc_Sample',
            'title'       => 'Custom Pie Chart', 
                                          'description' => 'Custom description',
                                          'icon'        => 'themes/default/images/icon_sam_Sample_32.gif',
                                          'category'    => 'Charts');
and
$dashletMeta['CustomSampleLineCart'] = array('module'  => 'abc_Sample',
            'title'       => 'Custom Chart', 
                                          'description' => 'Custom description',
                                          'icon'        => 'themes/default/images/icon_sam_Sample_32.gif',
                                          'category'    => 'Charts');


Notes:
custom\Charts\chartDefs.ext.php is the place where you will define your chart type, important fields:
'groupBy' => array( 'date','name' ) (the x and legend fields)
'chartType' => 'line chart', (the swf name, php will capitalize and add .swf, so it will be lineChar.swf)

in the sql your x values will have to coincide (be the same) for all lines

No comments:

Post a Comment