<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Plant Wars &#187; graph</title>
	<atom:link href="http://blog.plantwars.com/index.php/tag/graph/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.plantwars.com</link>
	<description>Development of a PBBG</description>
	<lastBuildDate>Wed, 10 Mar 2010 01:23:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dynamically Generated Graphs With PHP</title>
		<link>http://blog.plantwars.com/index.php/2009/02/28/dynamically-generated-graphs-with-php</link>
		<comments>http://blog.plantwars.com/index.php/2009/02/28/dynamically-generated-graphs-with-php#comments</comments>
		<pubDate>Sun, 01 Mar 2009 02:55:29 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.plantwars.com/?p=8</guid>
		<description><![CDATA[A requested feature I recently implemented was the ability to view your character&#8217;s progress in a chart. For example, you can view how quickly you&#8217;ve leveled for the past month, or see the fluctuations in the amount of cash you have in the bank. There are several libraries for generating graphs with PHP, but I [...]]]></description>
			<content:encoded><![CDATA[<p>A requested feature I recently implemented was the ability to view your character&#8217;s progress in a chart. For example, you can view how quickly you&#8217;ve leveled for the past month, or see the fluctuations in the amount of cash you have in the bank.</p>
<p>There are several libraries for generating graphs with PHP, but I went with <a href="http://phplot.sourceforge.net/" target="_blank">PHPlot</a>. It is easy and highly customizable. Other graphing packages, such as <a href="http://www.aditus.nu/jpgraph/" target="_blank">JpGraph</a> look like they would make more attractive graphs, but are either more complicated and/or not free for commercial use. I was only concerned with making bar graphs, but others are supported by PHPlot and most of the other packages.</p>
<p>Here is the end result:</p>
<p><img src="/wp-content/images/statgraph.jpg" alt="" /></p>
<p>And here is the PHP that generates it:</p>
<pre><code class="php">
// This is to format the x values so that they decrement from left to right
function format_x_value($value, $numDays) {
	return $numDays - $value;
}

        // Every day at midnight, we insert a row in the table StatLog with the current
        // fighting stats, money, health, and level
	$query = "SELECT * FROM StatLog WHERE UserId='".$_SESSION["Id"]."' ORDER BY Date ASC";
	$result = mysql_query($query);

	if ($result) {
		$stat = "Level";
		if (isset($_GET["stat"])) {
			$stat = $_GET["stat"];
			if ($stat == "Potency" || $stat == "Girth" || $stat == "Resp" || $stat == "Level"
                            || $stat == "MoneyBank" || $stat == "TotalHealth" || $stat == "Total") {
				// no op
			} else {
				// not acceptable stat specified - use level by default
				$stat = "Level";
			}
		}

                // Use an array of arrays to make a bar graph with PHPlot
		$data = array();
		$index = 0;
		$numDays = mysql_num_rows($result);

		while ($row = mysql_fetch_assoc($result)) {
			if ($stat == "Total") {
				$data[$index] = array('', $index, $row["Potency"]+$row["Resp"]+$row["Girth"]);
			} else {
				$data[$index] = array('', $index, $row[$stat]);
			}
			$index++;
		}

		$plot = new PHPlot();
		// Only get one x tick per record
		$plot-&gt;SetNumXTicks($numDays-1);
		// Use formatting function to reverse number ordering
		$plot-&gt;SetXLabelType('custom', 'format_x_value', $numDays);
		// Set data to use for graph
		$plot-&gt;SetDataValues($data);
		// Set graph's title
		$plot-&gt;SetTitle($stat . " Log");
		// data-data is the type for a bar graph
		$plot-&gt;SetDataType('data-data');
		// Set x-axis title
		$plot-&gt;SetXTitle("Days Ago");
		// Set y-axis title
		$plot-&gt;SetYTitle($stat);
		$plot-&gt;SetDrawPlotAreaBackground("true");
		// Make bg transparent
		$plot-&gt;SetBackgroundColor('yellow');
		$plot-&gt;SetTransparentColor('yellow');
		// Set plot bg color
		$plot-&gt;SetPlotBgColor("DarkGreen");
		$plot-&gt;DrawGraph();
	} else {
		echo mysql_error();
	}
</code></pre>
<p>Then, from another page, the users can select the stat they would like a graph of and it is passed along like so:</p>
<pre>&lt;img src="viewgraph.php?stat=Potency" /></pre>
<p>I&#8217;d be interested in hearing about other PHP graphing packages, but for now, I&#8217;m quite happy with these results.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.plantwars.com/index.php/2009/02/28/dynamically-generated-graphs-with-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
