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' width='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;
36 $_data = yourls_scale_data( $data );
42 'chd' => 't:'.( join(',' , $_data ) ),
44 'chl' => join('|' , array_keys( $data ) )
46 $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
47 echo "<img src='$pie_src' width='440' height='220' border='0' />";
50 // Build a list of all daily values between d1/m1/y1 to d2/m2/y2.
51 function yourls_build_list_of_days( $dates ) {
52 /* 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["$_year-$_month-$day"] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
108 'list_of_days' => $list_of_days,
109 'list_of_months' => $list_of_months,
110 'list_of_years' => $list_of_years,
114 // Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks'). $legend1_list & legend2_list are values used for the 2 x-axis labels
115 function yourls_stats_line( $values, $legend1_list, $legend2_list ) {
117 // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph
118 if ( count( $values ) == 1 )
119 array_unshift( $values, 0 );
121 $values = yourls_array_granularity( $values, 100 );
123 // If x-axis labels have only 1 value, double it for a nicer graph
124 if( count( $legend1_list ) == 1 )
125 $legend1_list[] = current( $legend1_list );
126 if( count( $legend2_list ) == 1 )
127 $legend2_list[] = current( $legend2_list );
130 $legend1 = join('|', $legend1_list );
131 $legend2 = join('|', $legend2_list );
132 $max = max( $values );
134 $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
136 $label_clicks = array();
137 for ($i = 0; $i <= $max; $i++) {
138 $label_clicks[] = $i;
140 $label_clicks = join( '|', $label_clicks );
146 'chd' => 't:'.( join(',' , $values ) ),
148 'chxl'=> '0:|'. $legend1 .'|1:|'. $legend2 .'|2:|'. $label_clicks
150 $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
151 echo "<img src='$line_src' />";
154 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
155 function yourls_days_in_month($month, $year) {
156 // calculate number of days in a month
157 return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
160 // Get max value from date array of 'year-month-day' = 'hits'
161 function yourls_stats_get_best_day( $list_of_days ) {
163 $max = max( $list_of_days );
164 foreach( $list_of_days as $k=>$v ) {
166 return array( 'day' => $k, 'max' => $max );
170 // Return domain of a URL
171 function yourls_get_domain( $url, $include_scheme = false ) {
172 $parse = parse_url( $url );
173 $host = $parse['host'];
174 $scheme = $parse['scheme'];
176 if ( $include_scheme )
177 $host = $scheme.'://'.$host;
182 // Scale array of data from 0 to 100 max
183 function yourls_scale_data( $data ) {
186 foreach( $data as $k=>$v ) {
187 $data[$k] = intval( $v / $max * 100 );
193 // Tweak granularity of array $array: keep only $grain values. This make less accurate but less messy graphs when too much values. See http://code.google.com/apis/chart/formats.html#granularity
194 function yourls_array_granularity( $array, $grain = 100, $preserve_max = true ) {
195 if ( count( $array ) > $grain ) {
196 $max = max( $array );
197 $step = intval( count( $array ) / $grain );
200 // Loop through each item and unset except every $step (optional preserver the max value)
201 foreach( $array as $k=>$v ) {
203 if ( $i % $step != 0 ) {
204 if ( $preserve_max == false ) {
208 if( $preserved == false )