You are here

class Util in Charts 8

Same name and namespace in other branches
  1. 8.4 src/Util/Util.php \Drupal\charts\Util\Util
  2. 8.3 src/Util/Util.php \Drupal\charts\Util\Util
  3. 5.0.x src/Util/Util.php \Drupal\charts\Util\Util

Hierarchy

  • class \Drupal\charts\Util\Util

Expanded class hierarchy of Util

1 file declares its use of Util
charts.module in ./charts.module
Charts module file that provides hook_theme.

File

src/Util/Util.php, line 5

Namespace

Drupal\charts\Util
View source
class Util {

  /**
   * @param $view
   * @param $labelValues
   * @param $labelField
   * @param $color
   * @return array
   */
  public static function viewsData($view, $labelValues, $labelField, $color) {
    $data = array();
    foreach ($view->result as $id => $row) {
      $numberFields = 0;
      $rowData = array();
      foreach ($labelValues as $fieldId => $rowDataValue) {
        $rowData[$numberFields] = array(
          'value' => $view->field[$fieldId]
            ->getValue($row),
          'label_field' => $view->field[$labelField]
            ->getValue($row),
          'label' => $view->field[$fieldId]
            ->label(),
          // 'label' => $view->display_handler->display['id'], to use display_id
          'color' => $color[$fieldId],
        );
        $numberFields++;
      }
      $data[$id] = $rowData;
    }
    return $data;
  }

  /**
   * Removes unselected fields
   */
  public static function removeUnselectedFields($valueField) {
    $fieldValues = array();
    foreach ($valueField as $key => $value) {
      if (!empty($value)) {
        $fieldValues[$key] = $value;
      }
    }
    return $fieldValues;
  }

  /**
   * Creates chart data to be used later by visualization frameworks
   */
  public static function createChartableData($data) {
    $chartData = array();
    $categories = array();
    $seriesData = array();
    for ($i = 0; $i < count($data[0]); $i++) {
      $seriesRowData = array(
        'name' => '',
        'color' => '',
        'data' => array(),
      );
      for ($j = 0; $j < count($data); $j++) {
        $categories[$j] = $data[$j][$i]['label_field'];
        $seriesRowData['name'] = $data[$j][$i]['label'];

        // $seriesRowData['name'] = $data[$j][$i]['label_field'];
        $seriesRowData['color'] = $data[$j][$i]['color'];
        array_push($seriesRowData['data'], (int) $data[$j][$i]['value']);
      }
      array_push($seriesData, $seriesRowData);
    }
    $chartData[0] = $categories;
    $chartData[1] = $seriesData;
    return $chartData;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Util::createChartableData public static function Creates chart data to be used later by visualization frameworks
Util::removeUnselectedFields public static function Removes unselected fields
Util::viewsData public static function