Search code examples
phpjqueryarraysajaxpie-chart

How to properly split data from email into an array


Preface: My overall goal is to create a pie chart showing the data below. The pie chart would have slices depicted as 1-8, with matching percentages based on the numerical value to the right of the colon.

I have the following data that is sent automatically in an email:

1:64.00
2:63.07
3:62.78
4:61.87
5:47.47
6:43.97
7:36.99
8:19.85
Sent from: [email redacted]
Parameters:3000,0
Time Server:2018.11.05 08:21:53
Time Local: 2018.11.04 22:21:53

There always this many lines sent in the email.

What I am trying to do is splice out the lines for data 1-8, which I have successfully done with this portion of the code:

if (strpos($row['subject'], 'Currency Relative Strength') !== false) {
                            $a1 = preg_split('/\r\n|\r|\n/', $row['body']);                                                
                            $a = array("label" => $b[0], "p1" => $a1[1], "p2" => $a1[2], "p3" => $a1[3], "p4" => $a1[4], "p5" => $a1[5], "p6" => $a1[6], "p7" => $a1[7]);
                            $array[] = $a;
                        }

It looks like this: [{"p0":"1:64.00","p1":"2:63.07","p2":"3:62.78","p3":"4:61.87","p4":"5:47.47","p5":"6:43.97","p6":"7:36.99","p7":"8:19.85"}]

The issue I am running into is that I am trying to follow the documentation here: https://canvasjs.com/docs/charts/integration/jquery/chart-types/jquery-pie-chart/

Which for the datapoints portion requires an array that is completely different from the one I managed to put together. This either leaves me needing to completely restructure the array, OR use a different pie chart system, which I am not against if anybody has any suggestions. I also understand that if I choose to go the route of canvasjs then to get an automatically updating pie chart with data that updates every minute, I have to implement something more like this:

CanvasJS: Making a chart dynamic with data.php, json encode and ajax(bandwidth meters)

To anybody willing to provide either assistance with the current code to better fit canvas js, OR suggest a whole different pie chart system that might work better with the array I have managed to build, I appreciate you very much! Btw I am not married to the idea of a jquery pie chart, I just figured it might be a better way to go...


Solution

  • Given that

    $message = '
    ...
    ';
    

    and the format for plotting JQuery pie chart

    dataPoints: [ 
                    { label: "Samsung",  y: 30.3, legendText: "Samsung"}, 
                    ...
                ] 
    

    The data points can be extracted as follow.

    $lines = explode("\n", trim($message));
    $firstEightLines = array_slice($lines, 0, 8);
    
    $dataPoints = array_map(function($line) {
        list($index, $point) = explode(":", $line);
        return [
            'label' => "p{$index}",
            'legendText' =>  "p{$index}",
            'y' =>(float)$point,
        ];
    }, $firstEightLines);
    
    var_dump($dataPoints);
    /*
    array(8) {
      [0]=>
      array(3) {
        ["label"]=>
        string(2) "p1"
        ["legendText"]=>
        string(2) "p1"
        ["y"]=>
        float(64)
      }
      ...*/