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 ) {
9 'chco'=> 'FFFFFF,9090AA,202040',
10 'chld'=> join('' , array_keys( $countries ) ),
11 'chd' => 't:'. join(',' , $countries ),
12 'chf' => 'bg,s,EAF7FE'
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' />";
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 ) {
23 $trim_data = array('Others' => 0);
24 foreach( $data as $item=>$value ) {
27 $trim_data[$item] = $value;
29 $trim_data['Others'] += $value;
35 // Scale items (biggest = 100)
37 foreach( $data as $k=>$v ) {
38 $data[$k] = intval( $v / $max * 100 );
45 'chd' => 't:'.( join(',' , $data ) ),
47 'chl' => join('|' , array_keys( $data ) )
49 $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
50 echo "<img src='$pie_src' witdh='440' height='220' border='0' />";
53 // Echoes an image tag of Google Charts bar graph from list of chronologically sorted array of [year][month][day] => 'number of clicks'
54 function yourls_stats_clicks_line( $dates ) {
55 /* Say we have an array like:
72 // Get first & last years from our range. In our example: 2009 & 2009
73 $first_year = key( $dates );
74 $last_year = end( array_keys($dates) );
77 // Get first & last months from our range. In our example: 08 & 09
78 $first_month = key( $dates[$first_year] );
79 $last_month = end( array_keys($dates[$last_year]) );
82 // Get first & last days from our range. In our example: 29 & 05
83 $first_day = key( $dates[$first_year][$first_month] );
84 $last_day = end( array_keys($dates[$last_year][$last_month]) );
86 // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
87 $list_of_years = array();
88 $list_of_months = array();
89 $list_of_days = array();
90 for ( $year = $first_year; $year <= $last_year; $year++ ) {
91 $_year = sprintf('%04d', $year);
92 $list_of_years[$_year] = $_year;
93 $current_first_month = ( $year == $first_year ? $first_month : '01' );
94 $current_last_month = ( $year == $last_year ? $last_month : '12' );
95 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
96 $_month = sprintf('%02d', $month);
97 $list_of_months[$_month] = $_month;
98 $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
99 $current_last_day = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
100 for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
101 $day = sprintf('%02d', $day);
102 $list_of_days[] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
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_year;
110 $max = max( $list_of_days );
111 $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
117 'chd' => 't:'.( join(',' , $list_of_days ) ),
118 'chxl'=> '0:|'. $label_years .'|1:|'. $label_months .'|2:|'. $label_clicks
120 $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
121 echo "<img src='$line_src' />";
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);
130 // Get max value from date array of [year][month][day] = 'hits'
131 function yourls_stats_get_best_day( $dates ) {
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";
144 return array( 'day' => $bday, 'max' => $max );