public
Created

  • Download Gist
chart.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
<?php
function ch_archive_graph($args = ''){
global $wpdb, $wp_locale;
//template tag defaults
$defaults = array(
'graphYear' => '2007',
'limit' => '13',
'graphHeight' => '200',
'xIncrement' => '55'
);
$maxHeight = 1;
$scale = 1;
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint($limit + 1);
$limit = ' LIMIT '.$limit;
}
$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'ch_archive_graph' , 'general');
$output = '<dl class="barGraph '.$graphYear.'">';
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_add( 'ch_archive_graph', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
//Loop through to find the highest number of posts
foreach ( (array) $arcresults as $arcresult ) {
//number of posts in a month
$total = $arcresult->posts;
if($maxHeight < $total) $maxHeight = $total;
}
//Reverse the months
$arcresults = array_reverse($arcresults);
foreach ( (array) $arcresults as $arcresult ) {
//Limit to one year
if($arcresult->year == $graphYear){
//Get month name, then appreviate, delete the second line if you want full month names
$month = sprintf(__('%1$s'), $wp_locale->get_month($arcresult->month));
$month = sprintf(__('%1$s'), $wp_locale->get_month_abbrev($month));
//Get archive link
$url = get_month_link( $arcresult->year, $arcresult->month );
//Number of posts in the month
$num_posts = $arcresult->posts;
//Determine the scale and the height of the bar
$scale = $graphHeight / $maxHeight;
$height = ($arcresult->posts * $scale);
//Put it all together
$output .= "<dt class='label' style='left: ".$xOffset."px;'><a href=".$url.">".$month."</a></dt>";
$output .= "<dd class='bar ".$month."' style='height: ".$height."px; left: ".$xOffset."px;' title=".$month.">".$num_posts."</dd>";
//Increase the offset for the next bar
$xOffset = $xOffset + $xIncrement;
}
}
}
$output .= "</dl>";
echo $output;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.