protected function _createSourcesGraph()
 {
     // load pChart graphic library
     $this->_loadLibs();
     // définition des données à afficher
     $dataSet = new pData();
     // need config, to know which data user wants to display : visits, unique visitors, pageviews
     $sefConfig = Sh404sefFactory::getConfig();
     // Google does not allow combining dimension=ga:medium with metric = unique visitors
     $dataType = $sefConfig->analyticsDashboardDataType == 'ga:visitors' ? 'ga:visits' : $sefConfig->analyticsDashboardDataType;
     $dataTypeString = str_replace('ga:', '', $dataType);
     // sort data for proper display
     usort($this->_rawData['sources'], array($this, '_sortSourcesDataCompareFunction'));
     // we walk the array, pulling out alternatively
     // the first and last items
     // making the new array having the largest item
     // followed by the smallest, then second largest
     // then second smallest, ...
     // which makes drawing labels much easier
     $tmpArray = array();
     $even = false;
     $max = count($this->_rawData['sources']);
     for ($i = 0; $i < $max; $i++) {
         if ($even) {
             // pull last item in sorted array
             $tmpArray[] = array_pop($this->_rawData['sources']);
         } else {
             // pull array first item
             $tmpArray[] = array_shift($this->_rawData['sources']);
         }
         // flag inversion
         $even = !$even;
     }
     // get data from response
     $data = array();
     $types = array();
     foreach ($tmpArray as $entry) {
         $value = $entry->{$dataTypeString};
         // do not add empty values, as pChart would choke on that and display a warning
         if (!empty($value)) {
             $data[] = $value;
             $types[] = Sh404sefHelperAnalytics::getReferralLabel($entry->dimension['medium']);
         }
     }
     $dataSet->AddPoint($data, "visits");
     $dataSet->AddPoint($types, "types");
     $dataSet->addSerie('visits');
     $dataSet->SetAbsciseLabelSerie("types");
     $label = JText::_('COM_SH404SEF_ANALYTICS_REPORT_SOURCES') . JText::_('COM_SH404SEF_ANALYTICS_REPORT_BY_LABEL') . Sh404sefHelperAnalytics::getDataTypeTitle();
     $dataSet->SetSerieName($label, "visits");
     // Initialise the graph
     $w = intval(0.45 * $this->_options['cpWidth']);
     $w = empty($w) ? 160 : $w;
     $radius = intval($w * 0.22);
     $margin = 5;
     $h = intval($w * 0.8);
     $centreX = intval(0.5 * $w);
     $centreY = intval(0.5 * $h);
     $chart = new pChart($w, $h);
     $fontSize = 8;
     // prepare graph
     $chart->setFontProperties($this->_baseChartPath . DS . 'Fonts/arial.ttf', $fontSize);
     $chart->loadColorPalette($this->_baseChartPath . DS . 'palettes' . DS . 'tones-2-green-soft.php');
     $chart->setGraphArea($margin, $margin, $w - $margin, $h - $margin);
     // This will draw a shadow under the pie chart
     $chart->drawFilledCircle($centreX + 4, $centreY + 4, $radius, 200, 200, 200);
     // Draw the pie chart
     $d = $dataSet->GetData();
     if (!empty($d)) {
         $chart->drawBasicPieGraph($d, $dataSet->GetDataDescription(), $centreX, $centreY, $radius, PIE_PERCENTAGE_LABEL_VALUE, 255, 255, 218);
     }
     // create a temporary file for
     $user =& JFactory::getUser();
     // make sure the root tmp dir exists
     $rootDir = 'tmp' . DS . 'sh404sef_analytics';
     Sh404sefHelperFiles::createDirAndIndex(JPATH_ROOT . DS . $rootDir);
     // file name is variable to avoid browser cache
     $basePath = $rootDir . DS . md5('useless_' . $user->id . '_hashing') . DS;
     // create path and make sure there's an index.html file in it
     Sh404sefHelperFiles::createDirAndIndex(JPATH_ROOT . DS . $basePath);
     $imageFileName = Sh404sefHelperAnalytics::createTempFile($basePath, $this->_options['report'] . '.' . $this->_options['accountId'] . '.sources.' . $dataTypeString);
     $chart->Render(JPATH_ROOT . DS . $imageFileName);
     // need cleaning up, so let's remove all report files for that user older than an hour or so
     Sh404sefHelperAnalytics::cleanReportsImageFiles(JPATH_ROOT . DS . $basePath, $age = 4000);
     return JURI::root() . $imageFileName;
 }