示例#1
0
// ->filterValuesSmallerThan()
$t->diag('->filterValuesSmallerThan()');
$data = array(1, 2, 3, 4, 5, 6, 7, 8);
$cs = new ChartSource();
$keys = $cs->filterValuesSmallerThan($data, 5);
$t->cmp_ok($keys, '===', array(4 => 5, 5 => 6, 6 => 7, 7 => 8), '::filterValuesSmallerThan() returns the elements of the input array whose value is larger than the given bound');
// ->filterValuesDifferentThan()
$t->diag('->filterValuesDifferentThan()');
$data = array(1, 1.1, 0.9, 2, 3, 1, 1);
$cs = new ChartSource();
$keys = $cs->filterValuesDifferentThan($data, 1);
$t->cmp_ok($keys, '===', array(0 => 1, 5 => 1, 6 => 1), '::filterValuesDifferentThan() returns the elements of the input array whose value is equal than the given bound');
// ->filterValuesOutsideRange
$t->diag('->filterValuesOutsideRange()');
$data = array(1, 2, 3, 4, 5, 6, 7, 1, 3, 4);
$cs = new ChartSource();
$keys = $cs->filterValuesOutsideRange($data, 1, 4);
$t->cmp_ok($keys, '===', array(0 => 1, 1 => 2, 2 => 3, 7 => 1, 8 => 3), '::filterValuesOutsideRange() returns the elements of the input array whose value is equal or larger than the lower bound and lower of the upper bound');
// ->buildXAxisData()
$t->diag('->buildXAxisData()');
$q1 = Doctrine_Core::getTable('Charge')->createQuery('c')->select('c.*')->leftJoin('c.Category ct')->where('ct.Name = ?', 'Tax');
$q2 = Doctrine_Core::getTable('Charge')->createQuery('c')->select('c.*')->leftJoin('c.Category ct')->where('ct.Name = ?', 'Fuel');
$e1 = $q1->execute();
$e2 = $q2->execute();
$s1 = new ChartDataSerie(array('raw_data' => $e1, 'label' => 'Taxes', 'id' => 'tax'));
$s2 = new ChartDataSerie(array('raw_data' => $e2, 'label' => 'Fuel', 'id' => 'fuel'));
$c1 = $q1->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
$d1 = array();
foreach ($c1 as $key => $value) {
    $d1[] = strtotime($value['date']);
}
示例#2
0
 /**
  * Builds and stores a ChartSource object, containing the raw data required to
  * plot the chart.
  *
  * @see ChartSource
  *
  * @return true of false, depending if a ChartSource has been correctly built.
  */
 public function buildChartSource()
 {
     $gs = new ChartSource();
     $gs->addParams($this->getParameters());
     // setting chart parameters
     $gs->addParams($this->getOption('chart_parameters'));
     // getting source data
     $vehicle_display = $this->getParameter('vehicle_display', 'single');
     $category_display = $this->getParameter('category_display', 'stacked');
     $series = $this->getChartSourceData($vehicle_display, $category_display);
     if (!$series) {
         return false;
     }
     $gs->setSeries($series);
     $this->getLogger()->info(sprintf('Chart %s source does not exist exist. Building chart data source.', $this->getChartName()));
     $this->chart_source = $gs;
     return true;
 }
 public function getChartSource($scenario, $categories_names = array(), $params = array())
 {
     $vd = $scenario[0];
     $cd = $scenario[1];
     $scn = $this->getCase($vd, $cd);
     $defalut_categories = array('Fuel', 'Tax', 'Maintenance');
     $default_vehicles = array('car_gs_1', 'car_gs_2', 'car_gs_3');
     switch ($scn) {
         case 1:
             $q = Doctrine_Core::getTable('Charge')->createQuery('c')->select('c.*')->leftJoin('c.User u')->andWhere('u.Username = ?', array('user_gs'));
             if ($categories_names) {
                 $q->leftJoin('c.Category ct')->andWhereIn('ct.name', $categories_names);
             }
             $history = $this->handleLimits($q, $scenario, $params);
             $q = $q->execute();
             $v = Doctrine_Core::getTable('Vehicle')->findByUserId($this->getUserId('user_gs'));
             $vid = $this->extractIds($v);
             $cid = $this->getCategoriesIdByName($categories_names);
             $series = array(new ChartDataSerie(array('raw_data' => $q, 'label' => 'all stacked', 'id' => 'allstacked', 'vehicle_id' => $vid, 'category_id' => $cid)));
             break;
         case 2:
             $cat = $categories_names ? $categories_names : $defalut_categories;
             $v = Doctrine_Core::getTable('Vehicle')->findByUserId($this->getUserId('user_gs'));
             $vid = $this->extractIds($v);
             $series = array();
             foreach ($cat as $key => $name) {
                 $q = Doctrine_Core::getTable('Charge')->createQuery('c')->select('c.*')->leftJoin('c.Category ct')->andWhere('ct.Name = ?', $name)->leftJoin('c.User u')->andWhere('u.Username = ?', array('user_gs'));
                 $history = $this->handleLimits($q, $scenario, $params);
                 $q = $q->execute();
                 $cid = $this->getCategoriesIdByName($name);
                 $series[$key] = new ChartDataSerie(array('raw_data' => $q, 'label' => 'tax', 'id' => 'tax', 'vehicle_id' => $vid, 'category_id' => $cid));
             }
             break;
         case 3:
             $cat = $categories_names ? $categories_names : array();
             $cid = $this->getCategoriesIdByName($cat);
             $series = array();
             foreach ($default_vehicles as $key => $name) {
                 $q = Doctrine_Core::getTable('Charge')->createQuery('c')->select('c.*')->leftJoin('c.Vehicle v')->andWhere('v.Name = ?', $name)->leftJoin('c.User u')->andWhere('u.Username = ?', array('user_gs'));
                 if ($categories_names) {
                     $q->leftJoin('c.Category ct')->andWhereIn('ct.name', $categories_names);
                 }
                 $history = $this->handleLimits($q, $scenario, $params);
                 $q = $q->execute();
                 $v = Doctrine_Core::getTable('Vehicle')->findOneByName($name);
                 $vid = $this->extractIds($v);
                 $series[$key] = new ChartDataSerie(array('raw_data' => $q, 'label' => 'car_gs_1', 'id' => 'car_gs_1', 'vehicle_id' => $vid, 'category_id' => $cid));
             }
             break;
         case 4:
             $cat = $categories_names ? $categories_names : $defalut_categories;
             $series = array();
             foreach ($default_vehicles as $vk => $vname) {
                 foreach ($cat as $ck => $cname) {
                     $q = Doctrine_Core::getTable('Charge')->createQuery('c')->select('c.*')->leftJoin('c.Vehicle v')->andWhere('v.Name = ?', $vname)->leftJoin('c.Category ct')->andWhere('ct.Name = ?', $cname)->leftJoin('c.User u')->andWhere('u.Username = ?', array('user_gs'));
                     $history = $this->handleLimits($q, $scenario, $params);
                     $q = $q->execute();
                     $v = Doctrine_Core::getTable('Vehicle')->findOneByName($vname);
                     $vid = $this->extractIds($v);
                     $c = Doctrine_Core::getTable('Category')->findOneByName($cname);
                     $cid = $this->extractIds($c);
                     $series[] = new ChartDataSerie(array('raw_data' => $q, 'label' => 'car_gs_1_tax', 'id' => 'car_gs_1_tax', 'vehicle_id' => $vid, 'category_id' => $cid));
                 }
             }
             break;
         default:
             throw new sfException('Case not defined');
             break;
     }
     $cat = $categories_names ? $categories_names : $defalut_categories;
     $cid = $this->getCategoriesIdByName($cat);
     $params = array('vehicle_display' => $vd, 'category_display' => $cd, 'categories_list' => $cid, 'full_history' => $history);
     if (isset($scenario[3])) {
         if ('distance' == $scenario[2]) {
             $params['kilometers_from'] = $scenario[3];
             $params['kilometers_to'] = $scenario[4];
         } else {
             $params['date_from'] = $scenario[3];
             $params['date_to'] = $scenario[4];
         }
     }
     $g = new ChartSource();
     $g->addParams($params);
     $g->setSeries($series);
     return $g;
 }