diff --git a/demo.php b/demo.php index 387e0ca..77600ed 100644 --- a/demo.php +++ b/demo.php @@ -1,122 +1,166 @@ 25, "label" => "Sunday"), - array("y" => 15, "label" => "Monday"), - array("y" => 25, "label" => "Tuesday"), - array("y" => 5, "label" => "Wednesday"), - array("y" => 10, "label" => "Thursday"), - array("y" => 0, "label" => "Friday"), - array("y" => 20, "label" => "Saturday") -); - -?> -format('m-d'); // Month-Day format - - // Initialize player array if not existing - if (!isset($dataPointsPerPlayer[$playername])) { - $dataPointsPerPlayer[$playername] = []; + + // Basic validation of JSON structure + if (is_array($jsonArray) && isset($jsonArray['stats']['playername'], $jsonArray['created'], $jsonArray['winplace'])) { + $playername = $jsonArray['stats']['playername']; + $winplace = $jsonArray['winplace']; + $createdTimestamp = $jsonArray['created']; + + // Process the date + try { + $date = new DateTime($createdTimestamp); + $label = $date->format('m-d'); // Month-Day format + + // Initialize player array if not existing + if (!isset($dataPointsPerPlayer[$playername])) { + $dataPointsPerPlayer[$playername] = []; + } + + // Add to dataPoints for this player + $dataPointsPerPlayer[$playername][] = [ + "y" => $winplace, + "label" => $label + ]; + } catch (Exception $e) { + // Log or handle date parsing error + error_log("Could not parse date '$createdTimestamp' in file: " . $filepath); + continue; + } + } else { + // Log or handle invalid JSON structure + error_log("Invalid JSON structure or missing keys in file: " . $filepath); + continue; } - - // Add to dataPoints for this player - $dataPointsPerPlayer[$playername][] = array( - "y" => $jsonArray['winplace'], - "label" => $label - ); + } + closedir($handle); + + // Prepare data for the chart if processing was successful + if (empty($dataError) && !empty($dataPointsPerPlayer)) { + foreach ($dataPointsPerPlayer as $player => $dataPoints) { + // Sort data points by label (date) for each player + usort($dataPoints, function($a, $b) { + return strcmp($a['label'], $b['label']); + }); + + $chartData[] = [ + 'type' => 'line', + 'showInLegend' => true, + 'legendText' => htmlspecialchars($player), // Escape player name for legend + 'dataPoints' => $dataPoints + ]; + } + // Encode the final chart data + $encodedChartData = json_encode($chartData, JSON_NUMERIC_CHECK); + if ($encodedChartData === false) { + $dataError = "Failed to encode chart data into JSON."; + $encodedChartData = '[]'; // Reset to empty array on encoding failure + } + } elseif (empty($dataError)) { + $dataError = "No valid data found in '$directory' to generate chart."; } } - closedir($handle); } -// Now, $dataPointsPerPlayer is an array that contains an array of data points for each player -// You can access the data for a specific player like this: -// $playerData = $dataPointsPerPlayer['Lanta01']; - -// Output the array for debugging purposes -// At the end of your PHP script, where you previously printed the array -// Instead, we will encode the $dataPointsPerPlayer array into JSON -$chartData = []; -foreach ($dataPointsPerPlayer as $player => $dataPoints) { - $chartData[] = [ - 'type' => 'line', - 'showInLegend' => true, - 'legendText' => $player, - 'dataPoints' => $dataPoints - ]; -} - -// Store the JSON encoded data in a PHP variable -$encodedChartData = json_encode($chartData, JSON_NUMERIC_CHECK); - -// You can then pass this to your JavaScript code like this -echo ""; - - +// Note: The original $dataPoints array with days of the week was unused, so it's removed. ?> - - -
- - + +