Migrations
3 years ago
ajax
3 years ago
models
3 years ago
queries
3 years ago
sql
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
dashboard_options.php
3 years ago
dashboard_widget.php
3 years ago
email_reports.php
3 years ago
filters.php
3 years ago
freemius.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
real_time.php
3 years ago
rest_api.php
3 years ago
settings.php
3 years ago
track_resource_changes.php
3 years ago
view_counter.php
3 years ago
chart_svg.php
120 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 $x_labels; |
| 13 | private $y_labels; |
| 14 | private $y_label_highest; |
| 15 | private $x_step; |
| 16 | private $y_step; |
| 17 | private $view_coords; |
| 18 | private $visitor_coords; |
| 19 | |
| 20 | public function __construct($views, $width, $height) |
| 21 | { |
| 22 | $this->views = $views; |
| 23 | $this->width = $width; |
| 24 | $this->height = $height; |
| 25 | |
| 26 | $this->daily_views = array_map(function ($data_point) { |
| 27 | return $data_point[1]; |
| 28 | }, $this->views->daily_views()); |
| 29 | |
| 30 | $this->daily_visitors = array_map(function ($data_point) { |
| 31 | return $data_point[1]; |
| 32 | }, $this->views->daily_visitors()); |
| 33 | |
| 34 | $this->x_labels = array_map(function ($data_point) { |
| 35 | return $data_point[0]->format('M j'); |
| 36 | }, $this->views->daily_views()); |
| 37 | |
| 38 | $this->y_labels = $this->get_y_labels(); |
| 39 | |
| 40 | $this->x_step = $this->width / count($this->daily_views); |
| 41 | $this->y_step = $this->height / $this->y_label_highest; |
| 42 | |
| 43 | $this->view_coords = $this->get_point_coordinates($this->daily_views); |
| 44 | $this->visitor_coords = $this->get_point_coordinates($this->daily_visitors); |
| 45 | } |
| 46 | |
| 47 | public function get_svg_data() |
| 48 | { |
| 49 | $svg_data = [ |
| 50 | 'width' => $this->width, |
| 51 | 'height' => $this->height, |
| 52 | 'xLabels' => $this->x_labels, |
| 53 | 'yLabels' => $this->y_labels, |
| 54 | 'yEnd' => $this->width - $this->x_step, |
| 55 | 'view_coords' => $this->view_coords, |
| 56 | 'view_coords_string' => $this->get_polyline_coords($this->view_coords), |
| 57 | 'visitor_coords' => $this->visitor_coords, |
| 58 | 'visitor_coords_string' => $this->get_polyline_coords($this->visitor_coords), |
| 59 | ]; |
| 60 | |
| 61 | return $svg_data; |
| 62 | } |
| 63 | |
| 64 | private function get_y_labels() |
| 65 | { |
| 66 | $most_views = max($this->daily_views); |
| 67 | |
| 68 | $y_label_step = round($most_views / 10); |
| 69 | // Prevent a step equal to zero |
| 70 | $y_label_step = $y_label_step == 0 ? 1 : $y_label_step; |
| 71 | $total_steps = round($most_views / $y_label_step) + 1; |
| 72 | $this->y_label_highest = $y_label_step * $total_steps; |
| 73 | |
| 74 | $y_labels[] = [ |
| 75 | 'tickY' => 0, |
| 76 | 'textY' => -3, |
| 77 | 'label' => 0, |
| 78 | ]; |
| 79 | for ($i = 1; $i <= $total_steps; $i++) { |
| 80 | $y = ($this->height / $total_steps) * $i; |
| 81 | $y_labels[] = [ |
| 82 | 'tickY' => $y, |
| 83 | 'textY' => $y - 3, |
| 84 | 'label' => $y_label_step * $i, |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | return $y_labels; |
| 89 | } |
| 90 | |
| 91 | private function get_point_coordinates(array $views) : array |
| 92 | { |
| 93 | $coordinates = []; |
| 94 | |
| 95 | for ($i = 0; $i < count($views); $i++) { |
| 96 | $x = $i * $this->x_step; |
| 97 | $y = $views[$i] * $this->y_step; |
| 98 | $coordinates[] = [ |
| 99 | 'x' => $x, |
| 100 | 'y' => $y, |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | return $coordinates; |
| 105 | } |
| 106 | |
| 107 | // The Y point is flipped because SVGs place (0,0) at the top-left, rather than the bottom-left |
| 108 | private function get_polyline_coords(array $coordinates) : string |
| 109 | { |
| 110 | $points = ''; |
| 111 | foreach ($coordinates as $coordinate) { |
| 112 | $points .= $coordinate['x'] . ',-' . $coordinate['y'] . ' '; |
| 113 | } |
| 114 | $points .= ($this->width - $this->x_step) . ',0 '; |
| 115 | $points .= '0,0'; |
| 116 | |
| 117 | return $points; |
| 118 | } |
| 119 | } |
| 120 |