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 ) {
10 'chco'=> 'FFFFFF,9090AA,202040',
11 'chld'=> join('' , array_keys( $countries ) ),
12 'chd' => 't:'. join(',' , $countries ),
13 'chf' => 'bg,s,EAF7FE'
15 $map_src = 'http://chart.apis.google.com/chart?' . http_build_query( $map );
16 echo "<img src='$map_src' witdh='440' height='220' border='0' />";
19 // Echoes an image tag of Google Charts pie from sorted array of 'country_code' => 'number of visits' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits
20 function yourls_stats_countries_pie( $countries, $limit = 10 ) {
21 if ( count( $countries ) > $limit ) {
23 $trim_countries = array('Others' => 0);
24 foreach( $countries as $country_code=>$visits ) {
27 $trim_countries[$country_code] = $visits;
29 $trim_countries['Others'] += $visits;
32 $countries = $trim_countries;
37 'chd' => 't:'.( join(',' , $countries ) ),
38 'chco'=> '202040,9090AA',
39 'chl' => join('|' , array_keys( $countries ) )
41 $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
42 echo "<img src='$pie_src' witdh='440' height='220' border='0' />";
45 // Echoes an image tag of Google Charts bar graph from list of chronologically sorted array of [year][month][day] => 'number of clicks'
46 function yourls_stats_clicks_bar( $dates ) {
47 /* Say we have an array like:
64 // Get first & last years from our range. In our example: 2009 & 2009
65 $first_year = key( $dates );
66 $last_year = end( array_keys($dates) );
69 // Get first & last months from our range. In our example: 08 & 09
70 $first_month = key( $dates[$first_year] );
71 $last_month = end( array_keys($dates[$last_year]) );
74 // Get first & last days from our range. In our example: 29 & 05
75 $first_day = key( $dates[$first_year][$first_month] );
76 $last_day = end( array_keys($dates[$last_year][$last_month]) );
78 // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
79 $list_of_years = array();
80 $list_of_months = array();
81 $list_of_days = array();
82 for ( $year = $first_year; $year <= $last_year; $year++ ) {
83 $_year = sprintf('%04d', $year);
84 $list_of_years[$_year] = $_year;
85 $current_first_month = ( $year == $first_year ? $first_month : '01' );
86 $current_last_month = ( $year == $last_year ? $last_month : '12' );
87 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
88 $_month = sprintf('%02d', $month);
89 $list_of_months[$_month] = $_month;
90 $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
91 $current_last_day = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
92 for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
93 $day = sprintf('%02d', $day);
94 $list_of_days[] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
100 $label_years = $first_year != $last_year ? join('|', $list_of_years ) : $first_year.'|'.$last_year;
101 $label_months = count( $list_of_months ) > 1 ? join('|', $list_of_months) : $first_month.'|'.$first_year;
102 $max = max( $list_of_days );
103 $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
109 'chd' => 't:'.( join(',' , $list_of_days ) ),
110 'chxl'=> '0:|'. $label_years .'|1:|'. $label_months .'|2:|'. $label_clicks
112 $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
113 echo "<img src='$line_src' />";
116 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
117 function yourls_days_in_month($month, $year) {
118 // calculate number of days in a month
119 return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
122 // Get max value from date array of [year][month][day] = 'hits'
123 function yourls_stats_get_best_day( $dates ) {
125 foreach( $dates as $year=>$months ) {
126 foreach( $months as $month=>$days ) {
127 foreach( $days as $day=>$visits ) {
128 if( $visits > $max ) {
129 $max = intval($visits);
130 $day = "$year-$month-$day";
136 return array( 'day' => $day, 'max' => $max );