[ 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 // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05) 140 $list_of_years = array(); 141 $list_of_months = array(); 142 $list_of_days = array(); 143 for ( $year = $first_year; $year <= $last_year; $year++ ) { 144 $_year = sprintf( '%04d', $year ); 145 $list_of_years[ $_year ] = $_year; 146 $current_first_month = ( $year == $first_year ? $first_month : '01' ); 147 $current_last_month = ( $year == $last_year ? $last_month : '12' ); 148 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) { 149 $_month = sprintf( '%02d', $month ); 150 $list_of_months[ $_month ] = $_month; 151 $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' ); 152 $current_last_day = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month( $month, $year) ); 153 for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) { 154 $day = sprintf( '%02d', $day ); 155 $key = date( 'M d, Y', mktime( 0, 0, 0, $_month, $day, $_year ) ); 156 $list_of_days[ $key ] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0; 157 } 158 } 159 } 160 161 return array( 162 'list_of_days' => $list_of_days, 163 'list_of_months' => $list_of_months, 164 'list_of_years' => $list_of_years, 165 ); 166 } 167 168 169 /** 170 * Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks'). 171 * 172 * $legend1_list & legend2_list are values used for the 2 x-axis labels. $id is an HTML/JS id 173 * 174 * @param array $values Array of values (eg 'number of clicks') 175 * @param string $id HTML element id 176 * @return void 177 */ 178 function yourls_stats_line($values, $id = null) { 179 180 yourls_do_action( 'pre_stats_line' ); 181 182 // if $id is null then assign a random string 183 if( $id === null ) 184 $id = uniqid ( 'yourls_stats_line_' ); 185 186 // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph 187 if ( count( $values ) == 1 ) 188 array_unshift( $values, 0 ); 189 190 // Keep only a subset of values to keep graph smooth 191 $values = yourls_array_granularity( $values, 30 ); 192 193 $data = array_merge( array( 'Time' => 'Hits' ), $values ); 194 $data = yourls_google_array_to_data_table( $data ); 195 196 $options = array( 197 "legend" => "none", 198 "pointSize" => "3", 199 "theme" => "maximized", 200 "curveType" => "function", 201 "width" => 430, 202 "height" => 220, 203 "hAxis" => "{minTextSpacing: 80, maxTextLines: 1, maxAlternation: 1}", 204 "vAxis" => "{minValue: 0, format: '#'}", 205 "colors" => "['#2a85b3']", 206 ); 207 $options = yourls_apply_filter( 'stats_line_options', $options ); 208 209 $lineChart = yourls_google_viz_code( 'LineChart', $data, $options, $id ); 210 211 echo yourls_apply_filter( 'stats_line', $lineChart, $values, $options, $id ); 212 } 213 214 215 /** 216 * Return the number of days in a month. From php.net. 217 * 218 * @param int $month 219 * @param int $year 220 * @return int 221 */ 222 function yourls_days_in_month($month, $year) { 223 // calculate number of days in a month 224 return $month == 2 ? ( $year % 4 ? 28 : ( $year % 100 ? 29 : ( $year % 400 ? 28 : 29 ) ) ) : ( ( $month - 1 ) % 7 % 2 ? 30 : 31 ); 225 } 226 227 228 /** 229 * Get max value from date array of 'Aug 12, 2012' = '1337' 230 * 231 * @param array $list_of_days 232 * @return array 233 */ 234 function yourls_stats_get_best_day($list_of_days) { 235 $max = max( $list_of_days ); 236 foreach( $list_of_days as $k=>$v ) { 237 if ( $v == $max ) 238 return array( 'day' => $k, 'max' => $max ); 239 } 240 } 241 242 /** 243 * Return domain of a URL 244 * 245 * @param string $url 246 * @param bool $include_scheme 247 * @return string 248 */ 249 function yourls_get_domain($url, $include_scheme = false) { 250 $parse = @parse_url( $url ); // Hiding ugly stuff coming from malformed referrer URLs 251 252 // Get host & scheme. Fall back to path if not found. 253 $host = isset( $parse['host'] ) ? $parse['host'] : ''; 254 $scheme = isset( $parse['scheme'] ) ? $parse['scheme'] : ''; 255 $path = isset( $parse['path'] ) ? $parse['path'] : ''; 256 if( !$host ) 257 $host = $path; 258 259 if ( $include_scheme && $scheme ) 260 $host = $scheme.'://'.$host; 261 262 return $host; 263 } 264 265 266 /** 267 * Return favicon URL 268 * 269 * @param string $url 270 * @return string 271 */ 272 function yourls_get_favicon_url($url) { 273 return yourls_match_current_protocol( '//www.google.com/s2/favicons?domain=' . yourls_get_domain( $url, false ) ); 274 } 275 276 /** 277 * Scale array of data from 0 to 100 max 278 * 279 * @param array $data 280 * @return array 281 */ 282 function yourls_scale_data($data ) { 283 $max = max( $data ); 284 if( $max > 100 ) { 285 foreach( $data as $k=>$v ) { 286 $data[$k] = intval( $v / $max * 100 ); 287 } 288 } 289 return $data; 290 } 291 292 293 /** 294 * Tweak granularity of array $array: keep only $grain values. 295 * 296 * This make less accurate but less messy graphs when too much values. 297 * See https://developers.google.com/chart/image/docs/gallery/line_charts?hl=en#data-granularity 298 * 299 * @param array $array 300 * @param int $grain 301 * @param bool $preserve_max 302 * @return array 303 */ 304 function yourls_array_granularity($array, $grain = 100, $preserve_max = true) { 305 if ( count( $array ) > $grain ) { 306 $max = max( $array ); 307 $step = intval( count( $array ) / $grain ); 308 $i = 0; 309 // Loop through each item and unset except every $step (optional preserve the max value) 310 foreach( $array as $k=>$v ) { 311 $i++; 312 if ( $i % $step != 0 ) { 313 if ( $preserve_max == false ) { 314 unset( $array[$k] ); 315 } else { 316 if ( $v < $max ) 317 unset( $array[$k] ); 318 } 319 } 320 } 321 } 322 return $array; 323 } 324 325 /** 326 * Transform data array to data table for Google API 327 * 328 * @param array $data 329 * @return string 330 */ 331 function yourls_google_array_to_data_table($data){ 332 $str = "var data = google.visualization.arrayToDataTable([\n"; 333 foreach( $data as $label => $values ){ 334 if( !is_array( $values ) ) { 335 $values = array( $values ); 336 } 337 $str .= "\t['$label',"; 338 foreach( $values as $value ){ 339 if( !is_numeric( $value ) && strpos( $value, '[' ) !== 0 && strpos( $value, '{' ) !== 0 ) { 340 $value = "'$value'"; 341 } 342 $str .= "$value"; 343 } 344 $str .= "],\n"; 345 } 346 $str = substr( $str, 0, -2 ) . "\n"; // remove the trailing comma/return, reappend the return 347 $str .= "]);\n"; // wrap it up 348 return $str; 349 } 350 351 /** 352 * Return javascript code that will display the Google Chart 353 * 354 * @param string $graph_type 355 * @param string $data 356 * @param var $options 357 * @param string $id 358 * @return string 359 */ 360 function yourls_google_viz_code($graph_type, $data, $options, $id ) { 361 $function_name = 'yourls_graph' . $id; 362 $code = "\n<script id=\"$function_name\" type=\"text/javascript\">\n"; 363 $code .= "function $function_name() { \n"; 364 365 $code .= "$data\n"; 366 367 $code .= "var options = {\n"; 368 foreach( $options as $field => $value ) { 369 if( !is_numeric( $value ) && strpos( $value, '[' ) !== 0 && strpos( $value, '{' ) !== 0 ) { 370 $value = "\"$value\""; 371 } 372 $code .= "\t'$field': $value,\n"; 373 } 374 $code = substr( $code, 0, -2 ) . "\n"; // remove the trailing comma/return, reappend the return 375 $code .= "\t}\n"; 376 377 $code .= "new google.visualization.$graph_type( document.getElementById('visualization_$id') ).draw( data, options );"; 378 $code .= "}\n"; 379 $code .= "google.setOnLoadCallback( $function_name );\n"; 380 $code .= "</script>\n"; 381 $code .= "<div id=\"visualization_$id\"></div>\n"; 382 383 return $code; 384 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Feb 22 05:10:06 2025 | Cross-referenced by PHPXref 0.7.1 |