Displaying WordPress categories via Google chart

İn this post i will give a small php code for wordpress which displays your wordpress categories in a nice pie-chart. It might be useful for every blog which owner is interested to share statictics of his(her) blog.
This is code solution of this small task:

//Author: Elvin Haci
//Author url: http://webania.net
$ch_cats = get_categories(array('orderby'=>'count','order'=>'desc') );
$sayy= count($ch_cats);
$chd=''; $chl='';
for ($i=1;$i<=$sayy;$i++)
{
$chd=$chd.','.$ch_cats[$i-1]->count;
$chl=$chl.'|'.$ch_cats[$i-1]->name.'('.$ch_cats[$i-1]->count.')';
}
$chd=substr($chd,1);
$chl=substr($chl,1);
$ch_title="My blog categories by post count"; 
$ch_size="450x200"; // You can change it feet to your post
echo '<img src="http://chart.googleapis.com/chart?chtt='.$ch_title.'
&cht=p3&chd=t:'.$chd.'&chs='.$ch_size.'&chl='.$chl.'">';

If you want to place pie chart into your post or page, you must install InlinePHP at first. After install you will be able to execute php codes inside posts. You can put this code into any post, with phpinline tag [ exec ] …code here… [/exec]
But if you have enough practise working with theme editor, you can put this php code to any place of your theme, for example to sidebar widget(sidebar.php in your theme folder).

At the end i show you the result of this code(i place inside blog post, it means that i have used phpinline plugin for it), of course here it shows categorie-chart for Webania.net :).

[exec]
$ch_cats = get_categories(array(‘orderby’=>’count’,’order’=>’desc’) );
$sayy= count($ch_cats);
$chd=”; $chl=”;
for ($i=1;$i<=$sayy;$i++) { $chd=$chd.','.$ch_cats[$i-1]->count;
$chl=$chl.’|’.$ch_cats[$i-1]->name.'(‘.$ch_cats[$i-1]->count.’)’;
}
$chd=substr($chd,1);
$chl=substr($chl,1);
$ch_title=”My blog categories by post count”;
$ch_size=”450×200″; // You can change it feet to your post
echo ‘‘;
[/exec]

I can easily turn this code to new wordpress plugin if users who don’t like any coding need it.
Good luck. If any question, please don’t hesitate to write comment.

6 comments on “Displaying WordPress categories via Google chart

  1. Hi Elvin,

    This would make a great plugin either for insertion into a page or as a widget.

    As the widget area is quite small a vertical bar would be better for those and pivotal question is:

    Can you make the categories links? So that the chart becomes “usable” to viewers?

    Matt

    1. Ok i will write small plugin for this solution. 🙂
      And it is very interesting idea that bars can be hyperlinks to their category, thank you, i will try to perform it. Nothing is impossible, i can do it with image mapping.

  2. Hi Elvin,

    Is there an option, we can have separate charts for separate (Parent) categories, showing the count of their sub categories respectively?

    I have two different categories: Geographies and Trends, with many sub-categories. Would like to see two different charts there.

    Thanks!

    1. Hello Abishek. Yeah, it is possible. Just add parent argument(parent category id) to this code

      $ch_cats = get_categories(array('orderby'=>'count','order'=>'desc','parent'=>'your_cat_parent_id_here') );
      

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.