Example #1
0
/**
 * 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
 *
 */
function yourls_stats_pie($data, $limit = 10, $size = '340x220', $id = null)
{
    yourls_do_action('pre_stats_pie');
    // if $id is null then assign a random string
    if ($id === null) {
        $id = uniqid('yourls_stats_pie_');
    }
    // Trim array: $limit first item + the sum of all others
    if (count($data) > $limit) {
        $i = 0;
        $trim_data = array('Others' => 0);
        foreach ($data as $item => $value) {
            $i++;
            if ($i <= $limit) {
                $trim_data[$item] = $value;
            } else {
                $trim_data['Others'] += $value;
            }
        }
        $data = $trim_data;
    }
    // Scale items
    $_data = yourls_scale_data($data);
    list($width, $height) = explode('x', $size);
    $options = array('theme' => 'maximized', 'width' => $width, 'height' => $height, 'colors' => "['A8D0ED','99C4E4','8AB8DB','7BACD2','6BA1C9','5C95C0','4D89B7','3E7DAE','2E72A5','1F669C']", 'legend' => 'none', 'chartArea' => '{top: "5%", height: "90%"}', 'pieSliceText' => 'label');
    $options = yourls_apply_filter('stats_pie_options', $options);
    $script_data = array_merge(array('Country' => 'Value'), $_data);
    $script_data = yourls_google_array_to_data_table($script_data);
    $pie = yourls_google_viz_code('PieChart', $script_data, $options, $id);
    echo yourls_apply_filter('stats_pie', $pie, $data, $limit, $size, $options, $id);
}
Example #2
0
function yourls_stats_pie($data, $limit = 10, $size = '340x220', $colors = '202040,9090AA')
{
    // Trim array: $limit first item + the sum of all others
    if (count($data) > $limit) {
        $i = 0;
        $trim_data = array('Others' => 0);
        foreach ($data as $item => $value) {
            $i++;
            if ($i <= $limit) {
                $trim_data[$item] = $value;
            } else {
                $trim_data['Others'] += $value;
            }
        }
        $data = $trim_data;
    }
    // Scale items
    $_data = yourls_scale_data($data);
    // Hmmm, pie
    $pie = array('cht' => 'p', 'chs' => $size, 'chd' => 't:' . join(',', $_data), 'chco' => $colors, 'chl' => join('|', array_keys($data)));
    $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query($pie);
    echo "<img src='{$pie_src}' width='440' height='220' border='0' />";
}