[ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Echoes an image tag of Google Charts map from sorted array of 'country_code' => 'number of visits' (sort by DESC) 5 * 6 * @param array $countries Array of 'country_code' => 'number of visits' 7 * @param string $id Optional HTML element ID 8 * @return void 9 */ 10 function yourls_stats_countries_map($countries, $id = null) { 11 12 yourls_do_action( 'pre_stats_countries_map' ); 13 14 // if $id is null then assign a random string 15 if( $id === null ) 16 $id = uniqid ( 'yourls_stats_map_' ); 17 18 $data = array_merge( array( 'Country' => 'Hits' ), $countries ); 19 $data = yourls_google_array_to_data_table( $data ); 20 21 $options = array( 22 'backgroundColor' => "white", 23 'colorAxis' => "{colors:['A8D0ED','99C4E4','8AB8DB','7BACD2','6BA1C9','5C95C0','4D89B7','3E7DAE','2E72A5','1F669C']}", 24 'width' => "550", 25 'height' => "340", 26 'theme' => 'maximized' 27 ); 28 $options = yourls_apply_filter( 'stats_countries_map_options', $options ); 29 30 $map = yourls_google_viz_code( 'GeoChart', $data, $options, $id ); 31 32 echo yourls_apply_filter( 'stats_countries_map', $map, $countries, $options, $id ); 33 } 34 35 36 /** 37 * 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 38 * 39 * @param array $data Array of 'data' => 'value' 40 * @param int $limit Optional limit list of X first countries 41 * @param $size Optional size of the image 42 * @param $id Optional HTML element ID 43 * @return void 44 */ 45 function yourls_stats_pie($data, $limit = 10, $size = '340x220', $id = null) { 46 47 yourls_do_action( 'pre_stats_pie' ); 48 49 // if $id is null then assign a random string 50 if( $id === null ) 51 $id = uniqid ( 'yourls_stats_pie_' ); 52 53 // Trim array: $limit first item + the sum of all others 54 if ( count( $data ) > $limit ) { 55 $i= 0; 56 $trim_data = array( 'Others' => 0 ); 57 foreach( $data as $item=>$value ) { 58 $i++; 59 if( $i <= $limit ) { 60 $trim_data[$item] = $value; 61 } else { 62 $trim_data['Others'] += $value; 63 } 64 } 65 $data = $trim_data; 66 } 67 68 // Scale items 69 $_data = yourls_scale_data( $data ); 70 71 list($width, $height) = explode( 'x', $size ); 72 73 $options = array( 74 'theme' => 'maximized', 75 'width' => $width, 76 'height' => $height, 77 'colors' => "['A8D0ED','99C4E4','8AB8DB','7BACD2','6BA1C9','5C95C0','4D89B7','3E7DAE','2E72A5','1F669C']", 78 'legend' => 'none', 79 'chartArea' => '{top: "5%", height: "90%"}', 80 'pieSliceText' => 'label', 81 ); 82 $options = yourls_apply_filter( 'stats_pie_options', $options ); 83 84 $script_data = array_merge( array( 'Country' => 'Value' ), $_data ); 85 $script_data = yourls_google_array_to_data_table( $script_data ); 86 87 $pie = yourls_google_viz_code( 'PieChart', $script_data, $options, $id ); 88 89 echo yourls_apply_filter( 'stats_pie', $pie, $data, $limit, $size, $options, $id ); 90 } 91 92 93 /** 94 * Build a list of all daily values between d1/m1/y1 to d2/m2/y2. 95 * 96 * @param array $dates 97 * @return array[] Array of arrays of days, months, years 98 */ 99 function yourls_build_list_of_days($dates) { 100 /* Say we have an array like: 101 $dates = array ( 102 2009 => array ( 103 '08' => array ( 104 29 => 15, 105 30 => 5, 106 ), 107 '09' => array ( 108 '02' => 3, 109 '03' => 5, 110 '04' => 2, 111 '05' => 99, 112 ), 113 ), 114 ) 115 */ 116 117 if( !$dates ) 118 return array(); 119 120 // Get first & last years from our range. In our example: 2009 & 2009 121 $first_year = key( $dates ); 122 $_keys = array_keys( $dates ); 123 $last_year = end( $_keys ); 124 reset( $dates ); 125 126 // Get first & last months from our range. In our example: 08 & 09 127 $first_month = key( $dates[ $first_year ] ); 128 $_keys = array_keys( $dates[ $last_year ] ); 129 $last_month = end( $_keys ); 130 reset( $dates ); 131 132 // Get first & last days from our range. In our example: 29 & 05 133 $first_day = key( $dates[ $first_year ][ $first_month ] ); 134 $_keys = array_keys( $dates[ $last_year ][ $last_month ] ); 135 $last_day = end( $_keys ); 136 137 unset( $_keys ); 138 139 // Extend to today 140 $today = new DateTime(); 141 $today->setTime( 0, 0, 0 ); // Start of today 142 $today_year = $today->format( 'Y' ); 143 $today_month = $today->format( 'm' ); 144 $today_day = $today->format( 'd' ); 145 146 // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05) 147 $list_of_years = array(); 148 $list_of_months = array(); 149 $list_of_days = array(); 150 for ( $year = $first_year; $year <= $today_year; $year++ ) { 151 $_year = sprintf( '%04d', $year ); 152 $list_of_years[ $_year ] = $_year; 153 $current_first_month = ( $year == $first_year ? $first_month : '01' ); 154 $current_last_month = ( $year == $today_year ? $today_month : '12' ); 155 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) { 156 $_month = sprintf( '%02d', $month ); 157 $list_of_months[ $_month ] = $_month; 158 $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' ); 159 $current_last_day = ( $year == $today_year && $month == $today_month ? $today_day : yourls_days_in_month( $month, $year ) ); 160 for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) { 161 $day = sprintf( '%02d', $day ); 162 $key = date( 'M d, Y', mktime( 0, 0, 0, $_month, $day, $_year ) ); 163 $list_of_days[ $key ] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0; 164 } 165 } 166 } 167 168 return array( 169 'list_of_days' => $list_of_days, 170 'list_of_months' => $list_of_months, 171 'list_of_years' => $list_of_years, 172 ); 173 } 174 175 176 /** 177 * Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks'). 178 * 179 * $legend1_list & legend2_list are values used for the 2 x-axis labels. $id is an HTML/JS id 180 * 181 * @param array $values Array of values (eg 'number of clicks') 182 * @param string $id HTML element id 183 * @return void 184 */ 185 function yourls_stats_line($values, $id = null) { 186 187 yourls_do_action( 'pre_stats_line' ); 188 189 // if $id is null then assign a random string 190 if( $id === null ) 191 $id = uniqid ( 'yourls_stats_line_' ); 192 193 // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph 194 if ( count( $values ) == 1 ) 195 array_unshift( $values, 0 ); 196 197 // Keep only a subset of values to keep graph smooth 198 $values = yourls_array_granularity( $values, 30 ); 199 200 $data = array_merge( array( 'Time' => 'Hits' ), $values ); 201 $data = yourls_google_array_to_data_table( $data ); 202 203 $options = array( 204 "legend" => "none", 205 "pointSize" => "3", 206 "theme" => "maximized", 207 "curveType" => "function", 208 "width" => 430, 209 "height" => 220, 210 "hAxis" => "{minTextSpacing: 80, maxTextLines: 1, maxAlternation: 1}", 211 "vAxis" => "{minValue: 0, format: '#'}", 212 "colors" => "['#2a85b3']", 213 ); 214 $options = yourls_apply_filter( 'stats_line_options', $options ); 215 216 $lineChart = yourls_google_viz_code( 'LineChart', $data, $options, $id ); 217 218 echo yourls_apply_filter( 'stats_line', $lineChart, $values, $options, $id ); 219 } 220 221 222 /** 223 * Return the number of days in a month. From php.net. 224 * 225 * @param int $month 226 * @param int $year 227 * @return int 228 */ 229 function yourls_days_in_month($month, $year) { 230 // calculate number of days in a month 231 return $month == 2 ? ( $year % 4 ? 28 : ( $year % 100 ? 29 : ( $year % 400 ? 28 : 29 ) ) ) : ( ( $month - 1 ) % 7 % 2 ? 30 : 31 ); 232 } 233 234 235 /** 236 * Get max value from date array of 'Aug 12, 2012' = '1337' 237 * 238 * @param array $list_of_days 239 * @return array 240 */ 241 function yourls_stats_get_best_day($list_of_days) { 242 $max = max( $list_of_days ); 243 foreach( $list_of_days as $k=>$v ) { 244 if ( $v == $max ) 245 return array( 'day' => $k, 'max' => $max ); 246 } 247 } 248 249 /** 250 * Return domain of a URL 251 * 252 * @param string $url 253 * @param bool $include_scheme 254 * @return string 255 */ 256 function yourls_get_domain($url, $include_scheme = false) { 257 $parse = @parse_url( $url ); // Hiding ugly stuff coming from malformed referrer URLs 258 259 // Get host & scheme. Fall back to path if not found. 260 $host = isset( $parse['host'] ) ? $parse['host'] : ''; 261 $scheme = isset( $parse['scheme'] ) ? $parse['scheme'] : ''; 262 $path = isset( $parse['path'] ) ? $parse['path'] : ''; 263 if( !$host ) 264 $host = $path; 265 266 if ( $include_scheme && $scheme ) 267 $host = $scheme.'://'.$host; 268 269 return $host; 270 } 271 272 273 /** 274 * Return favicon URL 275 * 276 * @param string $url 277 * @return string 278 */ 279 function yourls_get_favicon_url($url) { 280 return yourls_match_current_protocol( '//www.google.com/s2/favicons?domain=' . yourls_get_domain( $url, false ) ); 281 } 282 283 /** 284 * Scale array of data from 0 to 100 max 285 * 286 * @param array $data 287 * @return array 288 */ 289 function yourls_scale_data($data ) { 290 $max = max( $data ); 291 if( $max > 100 ) { 292 foreach( $data as $k=>$v ) { 293 $data[$k] = intval( $v / $max * 100 ); 294 } 295 } 296 return $data; 297 } 298 299 300 /** 301 * Tweak granularity of array $array: keep only $grain values. 302 * 303 * This make less accurate but less messy graphs when too much values. 304 * See https://developers.google.com/chart/image/docs/gallery/line_charts?hl=en#data-granularity 305 * 306 * @param array $array 307 * @param int $grain 308 * @param bool $preserve_max 309 * @return array 310 */ 311 function yourls_array_granularity($array, $grain = 100, $preserve_max = true) { 312 if ( count( $array ) > $grain ) { 313 $max = max( $array ); 314 $step = intval( count( $array ) / $grain ); 315 $i = 0; 316 // Loop through each item and unset except every $step (optional preserve the max value) 317 foreach( $array as $k=>$v ) { 318 $i++; 319 if ( $i % $step != 0 ) { 320 if ( $preserve_max == false ) { 321 unset( $array[$k] ); 322 } else { 323 if ( $v < $max ) 324 unset( $array[$k] ); 325 } 326 } 327 } 328 } 329 return $array; 330 } 331 332 /** 333 * Transform data array to data table for Google API 334 * 335 * @param array $data 336 * @return string 337 */ 338 function yourls_google_array_to_data_table($data){ 339 $str = "var data = google.visualization.arrayToDataTable([\n"; 340 foreach( $data as $label => $values ){ 341 if( !is_array( $values ) ) { 342 $values = array( $values ); 343 } 344 $str .= "\t['$label',"; 345 foreach( $values as $value ){ 346 if( !is_numeric( $value ) && strpos( $value, '[' ) !== 0 && strpos( $value, '{' ) !== 0 ) { 347 $value = "'$value'"; 348 } 349 $str .= "$value"; 350 } 351 $str .= "],\n"; 352 } 353 $str = substr( $str, 0, -2 ) . "\n"; // remove the trailing comma/return, reappend the return 354 $str .= "]);\n"; // wrap it up 355 return $str; 356 } 357 358 /** 359 * Return javascript code that will display the Google Chart 360 * 361 * @param string $graph_type 362 * @param string $data 363 * @param var $options 364 * @param string $id 365 * @return string 366 */ 367 function yourls_google_viz_code($graph_type, $data, $options, $id ) { 368 $function_name = 'yourls_graph' . $id; 369 $code = "\n<script id=\"$function_name\" type=\"text/javascript\">\n"; 370 $code .= "function $function_name() { \n"; 371 372 $code .= "$data\n"; 373 374 $code .= "var options = {\n"; 375 foreach( $options as $field => $value ) { 376 if( !is_numeric( $value ) && strpos( $value, '[' ) !== 0 && strpos( $value, '{' ) !== 0 ) { 377 $value = "\"$value\""; 378 } 379 $code .= "\t'$field': $value,\n"; 380 } 381 $code = substr( $code, 0, -2 ) . "\n"; // remove the trailing comma/return, reappend the return 382 $code .= "\t}\n"; 383 384 $code .= "new google.visualization.$graph_type( document.getElementById('visualization_$id') ).draw( data, options );"; 385 $code .= "}\n"; 386 $code .= "google.setOnLoadCallback( $function_name );\n"; 387 $code .= "</script>\n"; 388 $code .= "<div id=\"visualization_$id\"></div>\n"; 389 390 return $code; 391 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed May 14 05:10:02 2025 | Cross-referenced by PHPXref 0.7.1 |