how do I include sparkline in a Jasper report using JRuby?

Assuming that you have some way to get your source data, and that you already have followed instructions to do sparklines with JRuby and JFreeChart, you must first declare an image inside your report:

  <field name="Spark" class="net.sf.jasperreports.engine.JRRenderable"/>
[...]
  <image scaleImage="Clip" hAlign="Center" hyperlinkType="Reference">
    <reportElement x="200" y="0" width="150" height="30"/>
    <graphicElement/>
    <imageExpression class="net.sf.jasperreports.engine.JRRenderable"><![CDATA[$F{Spark}]]></imageExpression>
  </image>

And then, from JRuby, you must implement the JRDataSource interface (that is, a basic iterator over your data) to provide the corresponding field encapsulated in a JCommonDrawableRenderer:

class MySparkLine
  import "net.sf.jasperreports.engine.JRDataSource"
  include JRDataSource  
  def getFieldValue( field )
    case field.getName()
    when "Spark"
      return JCommonDrawableRenderer.new( chartObject )
    end
  end
end

And that’s as easy as it gets.

Comments are closed.