コード例 #1
0
 public function testAddColumns()
 {
     $col0 = new ColumnDescription('foo', ValueType::STRING);
     $col1 = new ColumnDescription('bar', ValueType::NUMBER);
     $data = new DataTable();
     $data->addColumns(array($col0, $col1));
     $this->assertSame(2, $data->getNumberOfColumns(), "data must contain 2 columns");
     $get0 = $data->getColumnDescription(0);
     $this->assertEquals($col0, $get0, "first column must match");
     $get1 = $data->getColumnDescription(1);
     $this->assertEquals($col1, $get1, "second column must match");
 }
コード例 #2
0
 public static function &reduceData(Query $query, DataTable &$data)
 {
     // If we aren't narrowing the selection, return the source data
     $select = $query->getSelect();
     if ($select === null) {
         return $data;
     }
     // Get a list of the fields we want to select
     $selectFieldIds = $select->getAllColumnIds();
     $neededFieldIds = $selectFieldIds;
     // If we're going to pivot, we also need to preserve any/all
     // of the pivot fields.
     $pivot = $query->getPivot();
     if ($pivot !== null) {
         $neededFieldIds = array_merge($neededFieldIds, $pivot->getAllColumnIds());
     }
     // Keep just the unique field ids we need
     $neededFieldIds = array_unique($neededFieldIds);
     // If the number of unique selected fields equals the number of
     // input columns, we need all the data in the source table, so
     // just return the source table itself.
     $numInputColumns = $data->getNumberOfColumns();
     if (count($neededFieldIds) == $numInputColumns) {
         return $data;
     }
     // We're removing at least one column from the source table
     $result = new DataTable();
     // Copy the columns we're saving
     $preservedColumnIndexes = array();
     for ($ci = 0; $ci < $numInputColumns; $ci++) {
         $column = $data->getColumnDescription($ci);
         if (!in_array($column->getId(), $neededFieldIds)) {
             continue;
         }
         $result->addColumn($column);
         $preservedColumnIndexes[] = $ci;
     }
     // For each row, copy the cells from the columns we're saving
     $numInputRows = $data->getNumberOfRows();
     for ($ri = 0; $ri < $numInputRows; $ri++) {
         $inputRow = $data->getRow($ri);
         // Copy just the columns we're saving
         $row = new TableRow();
         foreach ($preservedColumnIndexes as $ci) {
             $row->addCell($inputRow->getCell($ci));
         }
         $result->addRow($row);
     }
     return $result;
 }
コード例 #3
0
 /**
  * Apply default settings to rowFields and dataFields if needed
  * @throws NoDataFieldsException If there are no row fields
  * @throws Exception if the DataTable cannot answer this pivot request
  */
 protected function fixupFields()
 {
     $this->isFixed = true;
     // If they didn't give us any column fields, there is nothing to pivot!
     if (empty($this->columnFields)) {
         return;
     }
     // If they didn't give us any row fields, by default the first field is the row field
     if ($this->rowFields === null) {
         $this->rowFields = array($this->dataTable->getColumnDescription(0)->getId());
     }
     // If they didn't give us any data fields, by default ALL fields that are neither
     // column fields nor row fields are data fields
     if ($this->dataFields === null) {
         $dataFields = array();
         $allColumnIds = $this->getAllColumnIds($this->dataTable->getColumnDescriptions());
         foreach ($allColumnIds as $fieldId) {
             if (!in_array($fieldId, $this->columnFields) && !in_array($fieldId, $this->rowFields)) {
                 $dataFields[] = $fieldId;
             }
         }
         $this->dataFields = $dataFields;
     }
     if (count($this->dataFields) === 0) {
         throw new NoDataFieldsException();
     }
     // Verify that the same field is not specified in multiple types
     $this->verifyNoDuplicateFields($this->columnFields, $this->rowFields, 'column', 'row');
     $this->verifyNoDuplicateFields($this->columnFields, $this->dataFields, 'column', 'data');
     $this->verifyNoDuplicateFields($this->rowFields, $this->dataFields, 'row', 'data');
     // Verify the minimum number of fields exist in this DataTable to perform this pivot
     $numColumns = $this->dataTable->getNumberOfColumns();
     $minimumNumColumns = count($this->columnFields) + count($this->rowFields) + count($this->dataFields);
     if ($numColumns < $minimumNumColumns) {
         throw new Exception("Not enough columns in this DataTable to pivot (you have " . $numColumns . ", minimum is " . $minimumNumColumns . ")");
     }
 }
コード例 #4
0
 /**
  * @param DataTable $dataTable
  * @param bool $includeValues [optional]
  * @param bool $includeFormatting [optional]
  * @param bool $renderDateAsDateConstructor [optional]
  * @return string JSON encoded string describing $dataTable
  */
 public function renderDataTable(DataTable $dataTable, $includeValues = true, $includeFormatting = true, $renderDateAsDateConstructor = true)
 {
     $numColumns = $dataTable->getNumberOfColumns();
     $columns = $dataTable->getColumnDescriptions();
     $output = "{";
     $output .= "\"cols\":[";
     for ($i = 0; $i < $numColumns; $i++) {
         $column = $columns[$i];
         $output .= $this->renderColumnDescriptionJson($column);
         if ($i + 1 < $numColumns) {
             $output .= ",";
         }
     }
     $output .= "]";
     if ($includeValues) {
         $numRows = $dataTable->getNumberOfRows();
         $output .= ",\"rows\":[";
         for ($ri = 0; $ri < $numRows; $ri++) {
             $row = $dataTable->getRow($ri);
             $numCells = $row->getNumberOfCells();
             $output .= "{\"c\":[";
             for ($ci = 0; $ci < $numCells; $ci++) {
                 $cell = $row->getCell($ci);
                 if ($ci + 1 < $numCells) {
                     // For fields that are NOT the last field, renderCellJson() may return
                     // an empty string if the value is NULL
                     // @see https://code.google.com/p/google-visualization-java/source/browse/trunk/src/main/java/com/google/visualization/datasource/render/JsonRenderer.java
                     $cellOutput = $this->renderCellJson($cell, $includeFormatting, $this->optimizeNullValues, $renderDateAsDateConstructor);
                     $cellOutput .= ",";
                 } else {
                     // For fields that ARE the last field, a JSON string is returned
                     // even for NULL values
                     $cellOutput = $this->renderCellJson($cell, $includeFormatting, false, $renderDateAsDateConstructor);
                 }
                 $output .= $cellOutput;
             }
             $output .= "]";
             // Row properties
             $customProperties = $this->renderCustomPropertiesString($row->getCustomProperties());
             if ($customProperties !== null) {
                 $output .= ",\"p\":" . $customProperties;
             }
             $output .= "}";
             if ($ri + 1 < $numRows) {
                 $output .= ",";
             }
         }
         $output .= "]";
     }
     // Table properties
     $customProperties = $this->renderCustomPropertiesString($dataTable->getCustomProperties());
     if ($customProperties !== null) {
         $output .= ",\"p\":" . $customProperties;
     }
     $output .= "}";
     return $output;
 }