/** * 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. $id is an HTML/JS id * */ function yourls_stats_line($values, $id = null) { yourls_do_action('pre_stats_line'); // if $id is null then assign a random string if ($id === null) { $id = uniqid('yourls_stats_line_'); } // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph if (count($values) == 1) { array_unshift($values, 0); } // Keep only a subset of values to keep graph smooth $values = yourls_array_granularity($values, 30); $data = array_merge(array('Time' => 'Hits'), $values); $data = yourls_google_array_to_data_table($data); $options = array("legend" => "none", "pointSize" => "3", "theme" => "maximized", "curveType" => "function", "width" => 430, "height" => 220, "hAxis" => "{minTextSpacing: 80, maxTextLines: 1, maxAlternation: 1}", "vAxis" => "{minValue: -0.5, format: '#'}", "colors" => "['#2a85b3']"); $options = yourls_apply_filter('stats_line_options', $options); $lineChart = yourls_google_viz_code('LineChart', $data, $options, $id); echo yourls_apply_filter('stats_line', $lineChart, $values, $options, $id); }
unset($slice); break; case 'all': // get "yy-mm" foreach ($list_of_days as $k => $v) { $labels_1[] = preg_replace('/\\d\\d(\\d\\d)-(\\d\\d)-\\d\\d/', '$1-$2', $k); } // take out duplicates $labels_1 = array_unique($labels_1); // now get "mm" only so we have all different month foreach ($labels_1 as $k => $v) { $labels_1[$k] = preg_replace('/\\d\\d-(\\d\\d)/', '$1', $v); } echo "<h3>Number of hits : {$title}</h3>"; $labels_1 = yourls_array_granularity($labels_1, 30, false); $labels_2 = yourls_array_granularity($list_of_years, 30, false); yourls_stats_line($list_of_days, $labels_1, $labels_2); break; } echo "</div>\n"; } } ?> </td> <td valign="top"> <h3>Historical click count</h3> <?php $ago = round((date('U') - strtotime($timestamp)) / (24 * 60 * 60)); if ($ago <= 1) { $daysago = '';
function yourls_stats_line($values, $legend1_list, $legend2_list) { // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph if (count($values) == 1) { array_unshift($values, 0); } $values = yourls_array_granularity($values, 100); // If x-axis labels have only 1 value, double it for a nicer graph if (count($legend1_list) == 1) { $legend1_list[] = current($legend1_list); } if (count($legend2_list) == 1) { $legend2_list[] = current($legend2_list); } // Make the chart $legend1 = join('|', $legend1_list); $legend2 = join('|', $legend2_list); $max = max($values); if ($max >= 4) { $label_clicks = '0|' . intval($max / 4) . '|' . intval($max / 2) . '|' . intval($max / 1.5) . '|' . $max; } else { $label_clicks = array(); for ($i = 0; $i <= $max; $i++) { $label_clicks[] = $i; } $label_clicks = join('|', $label_clicks); } $line = array('cht' => 'lc', 'chs' => '440x220', 'chxt' => 'x,x,y', 'chd' => 't:' . join(',', $values), 'chds' => '0,' . $max, 'chxl' => '0:|' . $legend1 . '|1:|' . $legend2 . '|2:|' . $label_clicks); $line_src = 'http://chart.apis.google.com/chart?' . http_build_query($line); echo "<img src='{$line_src}' />"; }