AJAX
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Current_Resource.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
Chart_SVG.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class Chart_SVG |
| 6 | { |
| 7 | private $width; |
| 8 | private $height; |
| 9 | private $views; |
| 10 | private $daily_views; |
| 11 | private $daily_visitors; |
| 12 | private $daily_sessions; |
| 13 | private $x_labels; |
| 14 | private $y_labels; |
| 15 | private $y_label_highest; |
| 16 | private $x_step; |
| 17 | private $y_step; |
| 18 | private $view_coords; |
| 19 | private $visitor_coords; |
| 20 | private $session_coords; |
| 21 | |
| 22 | public function __construct($views, $width, $height) |
| 23 | { |
| 24 | $this->views = $views; |
| 25 | $this->width = $width; |
| 26 | $this->height = $height; |
| 27 | |
| 28 | $this->daily_views = array_map(function ($data_point) { |
| 29 | return $data_point[1]; |
| 30 | }, $this->views->daily_views()); |
| 31 | |
| 32 | $this->daily_visitors = array_map(function ($data_point) { |
| 33 | return $data_point[1]; |
| 34 | }, $this->views->daily_visitors()); |
| 35 | |
| 36 | $this->daily_sessions = array_map(function ($data_point) { |
| 37 | return $data_point[1]; |
| 38 | }, $this->views->daily_sessions()); |
| 39 | |
| 40 | $this->x_labels = array_map(function ($data_point) { |
| 41 | return $data_point[0]->format('M j'); |
| 42 | }, $this->views->daily_views()); |
| 43 | |
| 44 | $this->y_labels = $this->get_y_labels(); |
| 45 | |
| 46 | $this->x_step = $this->width / count($this->daily_views); |
| 47 | $this->y_step = $this->height / $this->y_label_highest; |
| 48 | |
| 49 | $this->view_coords = $this->get_point_coordinates($this->daily_views); |
| 50 | $this->visitor_coords = $this->get_point_coordinates($this->daily_visitors); |
| 51 | $this->session_coords = $this->get_point_coordinates($this->daily_sessions); |
| 52 | } |
| 53 | |
| 54 | public function get_svg_data() |
| 55 | { |
| 56 | $svg_data = [ |
| 57 | 'width' => $this->width, |
| 58 | 'height' => $this->height, |
| 59 | 'xLabels' => $this->x_labels, |
| 60 | 'yLabels' => $this->y_labels, |
| 61 | 'yEnd' => $this->width - $this->x_step, |
| 62 | 'view_coords' => $this->view_coords, |
| 63 | 'view_coords_string' => $this->get_polyline_coords($this->view_coords), |
| 64 | 'visitor_coords' => $this->visitor_coords, |
| 65 | 'visitor_coords_string' => $this->get_polyline_coords($this->visitor_coords), |
| 66 | 'session_coords' => $this->session_coords, |
| 67 | 'session_coords_string' => $this->get_polyline_coords($this->session_coords), |
| 68 | ]; |
| 69 | |
| 70 | return $svg_data; |
| 71 | } |
| 72 | |
| 73 | private function get_y_labels() |
| 74 | { |
| 75 | $most_views = max($this->daily_views); |
| 76 | |
| 77 | $y_label_step = round($most_views / 10); |
| 78 | // Prevent a step equal to zero |
| 79 | $y_label_step = $y_label_step == 0 ? 1 : $y_label_step; |
| 80 | $total_steps = round($most_views / $y_label_step) + 1; |
| 81 | $this->y_label_highest = $y_label_step * $total_steps; |
| 82 | |
| 83 | $y_labels[] = [ |
| 84 | 'tickY' => 0, |
| 85 | 'textY' => -3, |
| 86 | 'label' => 0, |
| 87 | ]; |
| 88 | for ($i = 1; $i <= $total_steps; $i++) { |
| 89 | $y = ($this->height / $total_steps) * $i; |
| 90 | $y_labels[] = [ |
| 91 | 'tickY' => $y, |
| 92 | 'textY' => $y - 3, |
| 93 | 'label' => $y_label_step * $i, |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | return $y_labels; |
| 98 | } |
| 99 | |
| 100 | private function get_point_coordinates(array $views) : array |
| 101 | { |
| 102 | $coordinates = []; |
| 103 | |
| 104 | for ($i = 0; $i < count($views); $i++) { |
| 105 | $x = $i * $this->x_step; |
| 106 | $y = $views[$i] * $this->y_step; |
| 107 | $coordinates[] = [ |
| 108 | 'x' => $x, |
| 109 | 'y' => $y, |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | return $coordinates; |
| 114 | } |
| 115 | |
| 116 | // The Y point is flipped because SVGs place (0,0) at the top-left, rather than the bottom-left |
| 117 | private function get_polyline_coords(array $coordinates) : string |
| 118 | { |
| 119 | $points = ''; |
| 120 | foreach ($coordinates as $coordinate) { |
| 121 | $points .= $coordinate['x'] . ',-' . $coordinate['y'] . ' '; |
| 122 | } |
| 123 | $points .= ($this->width - $this->x_step) . ',0 '; |
| 124 | $points .= '0,0'; |
| 125 | |
| 126 | return $points; |
| 127 | } |
| 128 | } |
| 129 |