X

Google charts shortcode, lines, xyline, sparkline, meter, scatter, venn, pie, pie2d

Adding this snippet to the functions.php of your wordpress theme will create a new shortcode that takes advantage of the Google charts API. This shortcode will let you display a range of charts "lines, xyline, sparkline, meter, scatter, venn, pie, pie2d" See the below shortcode example of a 3D pie chart. Change the type attribute in your shortcode to specify the type of graph you wish to display.


( example screenshot )


Code Snippet:


function google_chart_shortcode( $atts ) {
extract(shortcode_atts(array(
'data' => '',
'size' => '400×180',
'colors' => '',
'title' => '',
'labels' => '',
'type' => 'pie',
'advanced' => ''
), $atts));

switch ($type) {
case 'line' :
$charttype = 'lc'; break;
case 'xyline' :
$charttype = 'lxy'; break;
case 'sparkline' :
$charttype = 'ls'; break;
case 'meter' :
$charttype = 'gom'; break;
case 'scatter' :
$charttype = 's'; break;
case 'venn' :
$charttype = 'v'; break;
case 'pie' :
$charttype = 'p3'; break;
case 'pie2d' :
$charttype = 'p'; break;
default :
$charttype = $type;
break;
}

if ($title) $string .= '&chtt='.$title.'';
if ($labels) $string .= '&chl='.$labels.'';
if ($colors) $string .= '&chco='.$colors.'';
$string .= '&chs='.$size.'';
$string .= '&chd=t:'.$data.'';

return '<img title="'.$title.'" src="http://chart.apis.google.com/chart?cht='.$charttype.''.$string.$advanced.'" alt="'.$title.'" />';
}
add_shortcode('chart', 'google_chart_shortcode');


Code Snippet:


[chart data="41,37.89,10.67,2.03" labels="Reffering+sites|Search+Engines|Direct+traffic|Other" colors="ff0000,005599,fff000,000000" size="488×200" title="Traffic Sources" type="pie"]

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!