/**
  *	MAP
  **/
 public static function formatDataForMap($dataByEntity, $dimension, $times)
 {
     $normalizedData = [];
     foreach ($dataByEntity as $entityData) {
         $arr = array("id" => $entityData["id"], "key" => $entityData["key"], "entity" => $entityData["entity"], "values" => []);
         //main values
         //do we have some values for given entity at all?
         if (!array_key_exists($dimension->property, $entityData["values"])) {
             //nope, bail on this entity
             continue;
         }
         $mainValues = $entityData["values"][$dimension->property];
         $i = 0;
         //only getting one value per country per specify value
         $hasData = true;
         //if user chose not to get interpolated data, zero out tolerance
         if ($dimension->mode == "no-interpolation") {
             $dimension->tolerance = 0;
         }
         $defaultYear = 1960;
         $time = isset($dimension->targetYear) ? $dimension->targetYear : $defaultYear;
         $value = Chart::getValue($dimension, $time, $entityData["values"][$dimension->property]);
         if (Chart::hasValue($value)) {
             $timeArr[$dimension->property] = $value;
         } else {
             $hasData = false;
         }
         $arr["values"] = [$value];
         if ($hasData) {
             $normalizedData[$entityData["id"]] = $arr;
         }
     }
     return $normalizedData;
 }
Ejemplo n.º 2
0
 /**
  *	STACK AREA CHART
  **/
 public static function formatDataForStackBarChart($dataByEntity, $dimensionsByKey, $times, $groupByVariable)
 {
     $normalizedData = [];
     //start stack bar chart
     //need to sort times first, sort by key
     ksort($times);
     //main array, where we store data
     $arr = [];
     //format data for stack bar chart, need to always have time for all times
     foreach ($times as $time => $timeValue) {
         //array where we store data for all properties for all entities for given time
         $entitiesArr = [];
         //flag whether for given time, there's enough relevant data
         $hasData = true;
         //loop through entities
         foreach ($dataByEntity as $entityData) {
             //array where we store data for all properties for given entity for given time
             $entityTimeArr = [];
             //for each dimension
             foreach ($dimensionsByKey as $dimension) {
                 //skip categorical properties (color/shape)
                 if ($dimension->property === "color" || $dimension->property === "shape") {
                     continue;
                 }
                 if (!empty($entityData["values"][$dimension->property])) {
                     $value = Chart::getValue($dimension, $time, $entityData["values"][$dimension->property]);
                     if (Chart::hasValue($value)) {
                         $entityTimeArr[$dimension->property] = $value;
                         //also store time, useful for legend
                         $entityTimeArr["time"] = $time;
                     } else {
                         //for stack bar chart, we need to have data for all properties
                         $hasData = false;
                         break 2;
                     }
                 } else {
                     $hasData = false;
                     break 2;
                 }
             }
             //if we have all data for given property and time, store it
             if ($hasData) {
                 $entitiesArr[$entityData["id"]] = $entityTimeArr;
             }
         }
         //if data for all entities, store it in the main array
         if ($hasData) {
             $arr[$time] = $entitiesArr;
             //$i++;
         }
     }
     foreach ($dataByEntity as $entityData) {
         $entity = array("id" => $entityData["id"], "key" => $entityData["key"], "entity" => $entityData["entity"], "values" => []);
         $normalizedData[$entityData["id"]] = $entity;
     }
     //loop through all found times with data for all entities
     foreach ($arr as $time => $singleTimeArr) {
         //loop through all single times
         foreach ($singleTimeArr as $entityId => $values) {
             //fetch what we already have for entity
             $entityArr;
             if (array_key_exists($entityId, $normalizedData)) {
                 //we don't have anything for entity, create object and put it into main result array
                 $entityArr = $normalizedData[$entityId];
             } else {
                 //something weird, bail
                 continue;
             }
             //loop through all properties
             $entityValues = [];
             foreach ($values as $property => $value) {
                 $entityValues[$property] = $value;
             }
             $entityValues["x"] = $time;
             $entityArr["values"][] = $entityValues;
             //reupdate
             $normalizedData[$entityId] = $entityArr;
         }
     }
     return $normalizedData;
 }