/**
  * Transform data to pie chart specific format
  *
  * @param DataInterface $data
  * @param array $chartOptions
  *   Format of $chartOptions variable:
  *   array(
  *     "name" => "chart_name",
  *     "data_schema" => array(
  *         "label" => array("field_name" => "name", "label" => "oro.xxx.firstName"),
  *         "value" => array("field_name" => "salary", "label" => "oro.xxx.salary"),
  *     ),
  *     "settings" => array(
  *         "fraction_input_data_field" => "value"
  *         "fraction_output_data_field" => "fraction"
  *     ),
  *   )
  *
  * @return DataInterface
  */
 public function transform(DataInterface $data, array $chartOptions)
 {
     $inputKey = $chartOptions['settings'][self::FRACTION_INPUT_DATA_FIELD];
     $outputKey = $chartOptions['settings'][self::FRACTION_OUTPUT_DATA_FIELD];
     $inputArrayData = $data->toArray();
     $total = 0;
     foreach ($inputArrayData as $row) {
         $total += $row[$inputKey];
     }
     $resultArray = array();
     foreach ($inputArrayData as $row) {
         $row[$outputKey] = $total > 0 ? round($row[$inputKey] / $total, 4) : 1;
         $resultArray[] = $row;
     }
     return new ArrayData($resultArray);
 }
Example #2
0
 /**
  * @return array
  */
 public function toArray()
 {
     $result = array();
     foreach ($this->sourceData->toArray() as $sourceKey => $sourceItem) {
         if ($this->hasNestedRecords($sourceItem)) {
             foreach ($sourceItem as $nestedItem) {
                 $record = array();
                 foreach ($this->mapping as $name => $fieldName) {
                     $record[$name] = $this->getValue($nestedItem, $fieldName);
                 }
                 $result[$sourceKey][] = $record;
             }
         } else {
             $record = array();
             foreach ($this->mapping as $name => $fieldName) {
                 $record[$name] = $this->getValue($sourceItem, $fieldName);
             }
             $result[] = $record;
         }
     }
     return $result;
 }
 /**
  * @param DataInterface $data
  * @param array         $chartOptions
  *
  * @throws \InvalidArgumentException
  */
 protected function initialize(DataInterface $data, array $chartOptions)
 {
     if (empty($chartOptions['default_settings']['groupingOption'])) {
         throw new \InvalidArgumentException('Options "groupingOption" is not set');
     }
     /** @var MappedData $data */
     $this->sourceData = $data->getSourceData()->toArray();
     $this->labelKey = $chartOptions['data_schema']['label']['field_name'];
     $this->valueKey = $chartOptions['data_schema']['value']['field_name'];
     $this->groupingOption = $chartOptions['default_settings']['groupingOption'];
     if (empty($chartOptions['default_settings']['period'])) {
         throw new \InvalidArgumentException('Options "period" is not set');
     }
     $this->period = $chartOptions['default_settings']['period'];
     $now = new \DateTime();
     foreach ($this->dateFormatMap as $key => $format) {
         $this->dateFormatSize[$key] = strlen($now->format($format));
     }
 }
 /**
  * @dataProvider transformDataProvider
  */
 public function testTransform(DataInterface $data, array $chartOptions, DataInterface $expectedData)
 {
     $this->assertEquals($expectedData->toArray(), $this->transformer->transform($data, $chartOptions)->toArray());
 }
Example #5
0
 /**
  * Render chart
  *
  * @return string
  */
 public function render()
 {
     $context = $this->vars;
     $context['data'] = $this->data->toArray();
     return $this->twig->render($this->template, $context);
 }