<?php if (!defined('PmWiki')) exit();

$RecipeInfo['GoogleChart']['Version'] = '2017-12-31';

Markup('gchart', 'directives', '/\\(:gchart(\\s+.*?)?:\\)/i', 'gchart');

SDV($gchart_atypes, array("lc","lxy","bhs","bvs","bhg","bvg","p","p3","v","s"));
SDV($HTMLStylesFmt['gchart'], ".gchart_warning {font-weight: bold;} .gchart_warning span {color: red}");

function gchart($m) {
	global $gchart_atypes, $gchart_datasetpattern;
	$error = '';
	$out = '';
	$args = ParseArgs($m[1]);

	#check args

	#size (300000 pixel maximum)
	if ($args['xsize'] * $args['ysize'] > 300000) {
		$error .= '<div class="gchart_warning">';
		$error .= '<span>Warning:</span>The specified size (product of x and y) exceeds the maximum of 300 000 pixel.';
		$error .= '<div>';
	}
	#check if selected type is available
	if (!in_array($args['type'], $gchart_atypes)) {
		$error .= '<div class="gchart_warning">';
		$error .= '<span>Warning:</span>The specified type is not available. See the ';
		$error .= '<a href="http://code.google.com/apis/chart/#chart_type">documentation</a>';
		$error .= ' for alternatives.';
		$error .= '<div>';
	}
	if (strlen($error)>0){
		return $error;
	}
	$out .= '<img src="http://chart.apis.google.com/chart?';
	$out .= 'chs='.$args['xsize'].'x'.$args['ysize'];
	$out .= '&amp;';
	$out .= 'chd=s:'.$args['data'];
	$out .= '&amp;';
	$out .= 'cht='.$args['type'];
	$out .= '" />';
	
	return Keep($out);
}