0

I need to render a graph, but i got datas every 2 meters. To smooth my render, I want to complete missing datas every odd meter, with - the same date - depth between next and prev key - average value between next and prev key

My foreach looks like

        foreach ($datas as $data) {
           $chartline[] = array(
              'date' =>  $data->date, 
              'depth' => $data->z, 
              'value' => round($data->value, 2)
           );
        }

var_dump shows :

  0 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 2
      'value' => float 23.45
  1 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 4
      'value' => float 20.48
  2 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 6
      'value' => float 19.76
  3 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 8
      'value' => float 18.78
  4 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 10
      'value' => float 17.9
  5 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 12
      'value' => float 17.04
  6 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 14
      'value' => float 16.71
  7 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 16
      'value' => float 16.25

and i want to transform as :

0 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 1
      'value' => float 23.45

  1 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 2
      'value' => float 23.45
  2 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 3
      'value' => float (AVERAGE BETWEEN 23.45 the previous key AND 20.48 the next key)
  3 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 4
      'value' => float 20.48
  4 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 5
      'value' => float (AVERAGE BETWEEN 20.48 the previous key AND 17.9 the next key)
  5 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 6
      'value' => float 17.9
.
.
.
.
.

How I can add the values ​​that I need ?

If you have any ideas or hints !

1
  • Why would this smoothen the lines? You're still going to be drawing the same straight line, if you want to smoothen lines you want to draw curves instead, but that needs to take into account multiple data points and should generate more new points than you had original inputs. Let me know if you're interested or not(the basic idea is to map what the delta would be for each existing point, then add new points based on the transition points that would result, and repeat N times, where N is at least 2(otherwise it'll look jagged)). Commented Jul 24, 2014 at 14:27

1 Answer 1

1

Index your array by depth then for each odd number get the average of depth+1 and depth-1

$indexedByDepth = array();

foreach($array as $value) {
    $indexedByDepth[(int) $value['depth']] = $value;
}

foreach(range(1,max(array_keys($indexedByDepth)) as $i) {
    if($i % 2 == 0)
        continue;

    if(isset($indexedByDepth[$i-1]) && isset($indexedByDepth[$i+1])) {

        $average = ($indexedByDepth[$i-1]['value'] + $indexedByDepth[$i+1]['value'])/2;

        $array[] = array(
            'date' => $indexedByDepth[$i-1]['date'],
            'depth' => (float) $i,
            'value' => $average,
        );  
    }
    elseif(isset($indexedByDepth[$i-1])) {
        $array[] = $indexedByDepth[$i-1];   
    }
    elseif(isset($indexedByDepth[$i+1])) {
        $array[] = $indexedByDepth[$i+1];
    }
}

usort($array,function($a,$b) { return $a['depth'] - $b['depth']; });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.