]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-infos.php
Scaling fix on line charts (f*ck this is complicated :)
[Github/YOURLS.git] / includes / functions-infos.php
1 <?php
2
3 // Echoes an image tag of Google Charts map from sorted array of 'country_code' => 'number of visits' (sort by DESC)
4 function yourls_stats_countries_map( $countries ) {
5         $map = array(
6                 'cht' => 't',
7                 'chs' => '440x220',
8                 'chtm'=> 'world',
9                 'chco'=> 'FFFFFF,9090AA,202040',
10                 'chld'=> join('' , array_keys( $countries ) ),
11                 'chd' => 't:'. join(',' ,  $countries ),
12                 'chf' => 'bg,s,EAF7FE'
13         );
14         $map_src = 'http://chart.apis.google.com/chart?' . http_build_query( $map );
15         echo "<img src='$map_src' witdh='440' height='220' border='0' />";
16 }
17
18 // Echoes an image tag of Google Charts pie from sorted array of 'data' => 'value' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits
19 function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $colors = '202040,9090AA' ) {
20         // Trim array: $limit first item + the sum of all others
21         if ( count( $data ) > $limit ) {
22                 $i= 0;
23                 $trim_data = array('Others' => 0);
24                 foreach( $data as $item=>$value ) {
25                         $i++;
26                         if( $i <= $limit ) {
27                                 $trim_data[$item] = $value;
28                         } else {
29                                 $trim_data['Others'] += $value;
30                         }
31                 }
32                 $data = $trim_data;
33         }
34         
35         // Scale items
36         $_data = yourls_scale_data( $data );
37         
38         // Hmmm, pie
39         $pie = array(
40                 'cht' => 'p',
41                 'chs' => $size,
42                 'chd' => 't:'.( join(',' ,  $_data ) ),
43                 'chco'=> $colors,
44                 'chl' => join('|' , array_keys( $data ) )
45         );
46         $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
47         echo "<img src='$pie_src' witdh='440' height='220' border='0' />";
48 }
49
50 // Echoes an image tag of Google Charts bar graph from list of chronologically sorted array of [year][month][day] => 'number of clicks'
51 function yourls_stats_clicks_line( $dates ) {
52         /* Say we have an array like:
53         $dates = array (
54                 2009 => array (
55                         '08' => array (
56                                 29 => 15,
57                                 30 => 5,
58                                 ),
59                     '09' => array (
60                                 '02' => 3,
61                                 '03' => 5,
62                                 '04' => 2,
63                                 '05' => 99,
64                                 ),
65                         ),
66                 )
67         */
68
69         // Get first & last years from our range. In our example: 2009 & 2009
70         $first_year = key( $dates );
71         $last_year  = end( array_keys($dates) );
72         reset( $dates );
73
74         // Get first & last months from our range. In our example: 08 & 09
75         $first_month = key( $dates[$first_year] );
76         $last_month  = end( array_keys($dates[$last_year]) );
77         reset( $dates );
78         
79         // Get first & last days from our range. In our example: 29 & 05
80         $first_day = key( $dates[$first_year][$first_month] );
81         $last_day  = end( array_keys($dates[$last_year][$last_month]) );
82
83         // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
84         $list_of_years = array();
85         $list_of_months = array();
86         $list_of_days = array();
87         for ( $year = $first_year; $year <= $last_year; $year++ ) {
88                 $_year = sprintf('%04d', $year);
89                 $list_of_years[$_year] = $_year;
90                 $current_first_month = ( $year == $first_year ? $first_month : '01' );
91                 $current_last_month  = ( $year == $last_year ? $last_month : '12' );
92                 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
93                         $_month = sprintf('%02d', $month);
94                         $list_of_months[$_month] = $_month;
95                         $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
96                         $current_last_day  = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
97                         for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
98                                 $day = sprintf('%02d', $day);
99                                 $list_of_days[] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
100                         }
101                 }
102         }
103         
104         if ( count( $list_of_days ) == 1 )
105                 array_unshift( $list_of_days, 0 );
106         
107         // Make the chart
108         $label_years = $first_year != $last_year ? join('|', $list_of_years ) : $first_year.'|'.$last_year;
109         $label_months = count( $list_of_months ) > 1 ? join('|', $list_of_months) : $first_month.'|'.$first_month;
110         $max = max( $list_of_days );
111         $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
112         $line = array(
113                 'cht' => 'lc',
114                 'chs' => '440x220',
115                 'chxt'=> 'x,x,y',
116                 'chd' => 't:'.( join(',' ,  $list_of_days ) ),
117                 'chds' => '0,'.$max,
118                 'chxl'=> '0:|'. $label_years .'|1:|'. $label_months .'|2:|'. $label_clicks
119         );
120         $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
121         echo "<img src='$line_src' />";
122 }
123
124 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
125 function yourls_days_in_month($month, $year) {
126         // calculate number of days in a month
127         return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
128 }
129
130 // Get max value from date array of [year][month][day] = 'hits'
131 function yourls_stats_get_best_day( $dates ) {
132         $max = 0; $day = 0;
133         foreach( $dates as $year=>$months ) {
134                 foreach( $months as $month=>$days ) {
135                         foreach( $days as $day=>$visits ) {
136                                 if( $visits > $max ) {
137                                         $max = intval($visits);
138                                         $bday = "$year-$month-$day";
139                                 }
140                         }
141                 }
142         }
143
144         return array( 'day' => $bday, 'max' => $max );
145 }
146
147 // Fetch remote page title
148 function yourls_get_page_title( $url, $default = '' ) {
149         if ( !class_exists('DOMDocument') )
150                 return $default;
151
152         $dom = new DOMDocument();
153
154         if( @$dom->loadHTMLFile('http://planetozh.com/') ) {
155                 $list = $dom->getElementsByTagName("title");
156                 if ($list->length > 0) {
157                         return( $list->item(0)->textContent );
158                 }
159         }
160         
161         return $default;
162 }
163
164 // Return domain of a URL
165 function yourls_get_domain( $url, $include_scheme = false ) {
166         $parse = parse_url( $url );
167         $host = $parse['host'];
168         $scheme = $parse['scheme'];
169         
170         if ( $include_scheme )
171                 $host = $scheme.'://'.$host;
172                 
173         return $host;
174 }
175
176 // Scale array of data from 0 to 100 max
177 function yourls_scale_data( $data ) {
178         $max = max( $data );
179         if( $max > 100 ) {
180                 foreach( $data as $k=>$v ) {
181                         $data[$k] = intval( $v / $max * 100 );
182                 }
183         }
184         return $data;
185 }