<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Stacked Bar Charts in JRuby using JFreeChart</title>
	<atom:link href="http://kfahlgren.com/blog/2007/07/12/stacked-bar-charts-in-jruby-using-jfreechart/feed/" rel="self" type="application/rss+xml" />
	<link>http://kfahlgren.com/blog/2007/07/12/stacked-bar-charts-in-jruby-using-jfreechart/</link>
	<description>Keith on XML, Publishing, Ruby, Birds, &#038; San Francisco</description>
	<lastBuildDate>Wed, 09 Sep 2009 14:05:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: enebo</title>
		<link>http://kfahlgren.com/blog/2007/07/12/stacked-bar-charts-in-jruby-using-jfreechart/comment-page-1/#comment-108</link>
		<dc:creator>enebo</dc:creator>
		<pubDate>Tue, 21 Aug 2007 18:33:07 +0000</pubDate>
		<guid isPermaLink="false">http://kfahlgren.com/blog/2007/07/12/stacked-bar-charts-in-jruby-using-jfreechart/#comment-108</guid>
		<description>I updated your blog entry code snippet to use the new more endorsed
Java integration methods:
1. Include Java
2. Import instead of include_class

In addition, I decided to play with the addValue method and see what I
could come up with besides raw arrays (which is actually probably the
best way of loading raw datasets) if you were handcoding a graph.

I would be fun if we could get some threads going on list to discuss
good rubification of Java API techniques...

-Tom

----- jfree.rb ----
include Java

import org.jfree.data.category.DefaultCategoryDataset

class DefaultCategoryDataset
 class Inner
   def initialize(dataset, row)
     @dataset, @row = dataset, row.to_s
   end

   # a value of nil can call &#039;removeValue&#039;
   def []=(column, value)
     @dataset.addValue value, @row, column.to_s
   end

   def populate(hash)
     hash.each_with_pair do &#124;column, value&#124;
       @dataset.addValue value, @row, column.to_s
     end
   end
 end

 def [](row)
   Inner.new self, row
 end
end

module Graph
 class StackedBar
   import org.jfree.chart.ChartUtilities
   import org.jfree.chart.JFreeChart
   import org.jfree.chart.ChartFactory
   import org.jfree.chart.plot.PlotOrientation

   def initialize(width=600, height=400, dataset=create_sample_data)
     @height, @width, @chart = height, width, create_chart(dataset)
   end

   def render_to_file(filename, format=&quot;png&quot;)
     puts &quot;Rendering graph to #{filename}&quot;
     javafile = java.io.File.new(filename)
     ChartUtilities.saveChartAsPNG(javafile, @chart, @width, @height)
   end

   private
   def create_sample_data
     # I thought about method_missing for dataset.submitted but since these
     # can be arbitrary strings with method-name unfriendly chars in it I
     # decided against it.

     # This also allows symbols as well as strings
     dataset = DefaultCategoryDataset.new
     dataset[:Submitted].populate(:A =&gt; 1, :B =&gt; 2, :C =&gt; 3)
     dataset[:Assigned].populate(:A =&gt; 1, :C =&gt; 1)
     dataset[&quot;In-work&quot;].populate(:A =&gt; 3, :C =&gt; 3)
     dataset[:InVerfication][:A] = 1
     dataset[:Delivered][:A] = 2
     dataset[:Rejected][:B] = 1
     dataset[:Closed][:B] = 1
     dataset[&quot;On-hold&quot;][:C] = 2
     dataset
   end

   def create_chart(dataset)
     ChartFactory.createStackedBarChart(&quot;XYZ&#039;s Development Projects&quot;,
                                        &quot;Project Name&quot;,
                                        &quot;Hours&quot;,
                                        dataset,
                                        PlotOrientation::VERTICAL,
                                        true,
                                        true,
                                        false)
   end
 end # class StackedBar
end # class Graph

sb = Graph::StackedBar.new
sb.render_to_file(&quot;stacked_bar.png&quot;)</description>
		<content:encoded><![CDATA[<p>I updated your blog entry code snippet to use the new more endorsed<br />
Java integration methods:<br />
1. Include Java<br />
2. Import instead of include_class</p>
<p>In addition, I decided to play with the addValue method and see what I<br />
could come up with besides raw arrays (which is actually probably the<br />
best way of loading raw datasets) if you were handcoding a graph.</p>
<p>I would be fun if we could get some threads going on list to discuss<br />
good rubification of Java API techniques&#8230;</p>
<p>-Tom</p>
<p>&#8212;&#8211; jfree.rb &#8212;-<br />
include Java</p>
<p>import org.jfree.data.category.DefaultCategoryDataset</p>
<p>class DefaultCategoryDataset<br />
 class Inner<br />
   def initialize(dataset, row)<br />
     @dataset, @row = dataset, row.to_s<br />
   end</p>
<p>   # a value of nil can call &#8216;removeValue&#8217;<br />
   def []=(column, value)<br />
     @dataset.addValue value, @row, column.to_s<br />
   end</p>
<p>   def populate(hash)<br />
     hash.each_with_pair do |column, value|<br />
       @dataset.addValue value, @row, column.to_s<br />
     end<br />
   end<br />
 end</p>
<p> def [](row)<br />
   Inner.new self, row<br />
 end<br />
end</p>
<p>module Graph<br />
 class StackedBar<br />
   import org.jfree.chart.ChartUtilities<br />
   import org.jfree.chart.JFreeChart<br />
   import org.jfree.chart.ChartFactory<br />
   import org.jfree.chart.plot.PlotOrientation</p>
<p>   def initialize(width=600, height=400, dataset=create_sample_data)<br />
     @height, @width, @chart = height, width, create_chart(dataset)<br />
   end</p>
<p>   def render_to_file(filename, format=&#8221;png&#8221;)<br />
     puts &#8220;Rendering graph to #{filename}&#8221;<br />
     javafile = java.io.File.new(filename)<br />
     ChartUtilities.saveChartAsPNG(javafile, @chart, @width, @height)<br />
   end</p>
<p>   private<br />
   def create_sample_data<br />
     # I thought about method_missing for dataset.submitted but since these<br />
     # can be arbitrary strings with method-name unfriendly chars in it I<br />
     # decided against it.</p>
<p>     # This also allows symbols as well as strings<br />
     dataset = DefaultCategoryDataset.new<br />
     dataset[:Submitted].populate(:A =&gt; 1, :B =&gt; 2, :C =&gt; 3)<br />
     dataset[:Assigned].populate(:A =&gt; 1, :C =&gt; 1)<br />
     dataset["In-work"].populate(:A =&gt; 3, :C =&gt; 3)<br />
     dataset[:InVerfication][:A] = 1<br />
     dataset[:Delivered][:A] = 2<br />
     dataset[:Rejected][:B] = 1<br />
     dataset[:Closed][:B] = 1<br />
     dataset["On-hold"][:C] = 2<br />
     dataset<br />
   end</p>
<p>   def create_chart(dataset)<br />
     ChartFactory.createStackedBarChart(&#8220;XYZ&#8217;s Development Projects&#8221;,<br />
                                        &#8220;Project Name&#8221;,<br />
                                        &#8220;Hours&#8221;,<br />
                                        dataset,<br />
                                        PlotOrientation::VERTICAL,<br />
                                        true,<br />
                                        true,<br />
                                        false)<br />
   end<br />
 end # class StackedBar<br />
end # class Graph</p>
<p>sb = Graph::StackedBar.new<br />
sb.render_to_file(&#8220;stacked_bar.png&#8221;)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
