[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/fact/controller/ -> PhabricatorFactChartController.php (source)

   1  <?php
   2  
   3  final class PhabricatorFactChartController extends PhabricatorFactController {
   4  
   5    public function processRequest() {
   6      $request = $this->getRequest();
   7      $user = $request->getUser();
   8  
   9      $table = new PhabricatorFactRaw();
  10      $conn_r = $table->establishConnection('r');
  11      $table_name = $table->getTableName();
  12  
  13      $series = $request->getStr('y1');
  14  
  15      $specs = PhabricatorFactSpec::newSpecsForFactTypes(
  16        PhabricatorFactEngine::loadAllEngines(),
  17        array($series));
  18      $spec = idx($specs, $series);
  19  
  20      $data = queryfx_all(
  21        $conn_r,
  22        'SELECT valueX, epoch FROM %T WHERE factType = %s ORDER BY epoch ASC',
  23        $table_name,
  24        $series);
  25  
  26      $points = array();
  27      $sum = 0;
  28      foreach ($data as $key => $row) {
  29        $sum += (int)$row['valueX'];
  30        $points[(int)$row['epoch']] = $sum;
  31      }
  32  
  33      if (!$points) {
  34        // NOTE: Raphael crashes Safari if you hand it series with no points.
  35        throw new Exception('No data to show!');
  36      }
  37  
  38      // Limit amount of data passed to browser.
  39      $count = count($points);
  40      $limit = 2000;
  41      if ($count > $limit) {
  42        $i = 0;
  43        $every = ceil($count / $limit);
  44        foreach ($points as $epoch => $sum) {
  45          $i++;
  46          if ($i % $every && $i != $count) {
  47            unset($points[$epoch]);
  48          }
  49        }
  50      }
  51  
  52      $x = array_keys($points);
  53      $y = array_values($points);
  54  
  55      $id = celerity_generate_unique_node_id();
  56      $chart = phutil_tag(
  57        'div',
  58        array(
  59          'id' => $id,
  60          'style' => 'border: 1px solid #6f6f6f; '.
  61                     'margin: 1em 2em; '.
  62                     'background: #ffffff; '.
  63                     'height: 400px; ',
  64        ),
  65        '');
  66  
  67      require_celerity_resource('raphael-core');
  68      require_celerity_resource('raphael-g');
  69      require_celerity_resource('raphael-g-line');
  70  
  71      Javelin::initBehavior('line-chart', array(
  72        'hardpoint' => $id,
  73        'x' => array($x),
  74        'y' => array($y),
  75        'xformat' => 'epoch',
  76        'colors' => array('#0000ff'),
  77      ));
  78  
  79      $panel = new AphrontPanelView();
  80      $panel->setHeader('Count of '.$spec->getName());
  81      $panel->appendChild($chart);
  82  
  83      $crumbs = $this->buildApplicationCrumbs();
  84      $crumbs->addTextCrumb(pht('Chart'));
  85  
  86      return $this->buildApplicationPage(
  87        array(
  88          $crumbs,
  89          $panel,
  90        ),
  91        array(
  92          'title' => 'Chart',
  93          'device' => false,
  94        ));
  95    }
  96  
  97  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1