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 );
48 list( $size_x, $size_y ) = split( 'x', $size );
49 echo "<img src='$pie_src' width='$size_x' height='$size_y' border='0' />";
52 // Build a list of all daily values between d1/m1/y1 to d2/m2/y2.
53 function yourls_build_list_of_days( $dates ) {
54 /* Say we have an array like:
74 // Get first & last years from our range. In our example: 2009 & 2009
75 $first_year = key( $dates );
76 $last_year = end( array_keys($dates) );
79 // Get first & last months from our range. In our example: 08 & 09
80 $first_month = key( $dates[$first_year] );
81 $last_month = end( array_keys($dates[$last_year]) );
84 // Get first & last days from our range. In our example: 29 & 05
85 $first_day = key( $dates[$first_year][$first_month] );
86 $last_day = end( array_keys($dates[$last_year][$last_month]) );
88 // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
89 $list_of_years = array();
90 $list_of_months = array();
91 $list_of_days = array();
92 for ( $year = $first_year; $year <= $last_year; $year++ ) {
93 $_year = sprintf('%04d', $year);
94 $list_of_years[$_year] = $_year;
95 $current_first_month = ( $year == $first_year ? $first_month : '01' );
96 $current_last_month = ( $year == $last_year ? $last_month : '12' );
97 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
98 $_month = sprintf('%02d', $month);
99 $list_of_months[$_month] = $_month;
100 $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
101 $current_last_day = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
102 for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
103 $day = sprintf('%02d', $day);
104 $list_of_days["$_year-$_month-$day"] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
110 'list_of_days' => $list_of_days,
111 'list_of_months' => $list_of_months,
112 'list_of_years' => $list_of_years,
116 // 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
117 function yourls_stats_line( $values, $legend1_list, $legend2_list ) {
119 // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph
120 if ( count( $values ) == 1 )
121 array_unshift( $values, 0 );
123 $values = yourls_array_granularity( $values, 100 );
125 // If x-axis labels have only 1 value, double it for a nicer graph
126 if( count( $legend1_list ) == 1 )
127 $legend1_list[] = current( $legend1_list );
128 if( count( $legend2_list ) == 1 )
129 $legend2_list[] = current( $legend2_list );
132 $legend1 = join('|', $legend1_list );
133 $legend2 = join('|', $legend2_list );
134 $max = max( $values );
136 $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
138 $label_clicks = array();
139 for ($i = 0; $i <= $max; $i++) {
140 $label_clicks[] = $i;
142 $label_clicks = join( '|', $label_clicks );
148 'chd' => 't:'.( join(',' , $values ) ),
150 'chm' => 'B,E3F3FF,0,0,0|o,5FA3C6,0,-1,6|o,FFFFFF,0,-1,4',
152 'chxl'=> '0:|'. $legend1 .'|1:|'. $legend2 .'|2:|'. $label_clicks
154 $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
155 echo "<img src='$line_src' />";
158 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
159 function yourls_days_in_month($month, $year) {
160 // calculate number of days in a month
161 return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
164 // Get max value from date array of 'year-month-day' = 'hits'
165 function yourls_stats_get_best_day( $list_of_days ) {
167 $max = max( $list_of_days );
168 foreach( $list_of_days as $k=>$v ) {
170 return array( 'day' => $k, 'max' => $max );
174 // Return domain of a URL
175 function yourls_get_domain( $url, $include_scheme = false ) {
176 $parse = parse_url( $url );
178 // Get host & scheme. Fall back to path if not found.
179 $host = isset( $parse['host'] ) ? $parse['host'] : '';
180 $scheme = isset( $parse['scheme'] ) ? $parse['scheme'] : '';
181 $path = isset( $parse['path'] ) ? $parse['path'] : '';
185 if ( $include_scheme && $scheme )
186 $host = $scheme.'://'.$host;
191 // Return favicon URL
192 function yourls_get_favicon_url( $url ) {
193 return 'http://www.google.com/s2/u/0/favicons?domain=' . yourls_get_domain( $url, false );
196 // Scale array of data from 0 to 100 max
197 function yourls_scale_data( $data ) {
200 foreach( $data as $k=>$v ) {
201 $data[$k] = intval( $v / $max * 100 );
207 // 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
208 function yourls_array_granularity( $array, $grain = 100, $preserve_max = true ) {
209 if ( count( $array ) > $grain ) {
210 $max = max( $array );
211 $step = intval( count( $array ) / $grain );
214 // Loop through each item and unset except every $step (optional preserver the max value)
215 foreach( $array as $k=>$v ) {
217 if ( $i % $step != 0 ) {
218 if ( $preserve_max == false ) {
222 if( $preserved == false )