mysql,Linux,HighPerformance,ruby on Rails

2010年6月14日星期一

use jruby+sinatra+jfreechart build StackedBar graph! - Series 1

FYI,jruby is now 1.5.1, i want to build gpaph service.so i want to familiar with jruby and jfreechart api. this is blow doc is my practice notes.any feedback welcome.

first step:
use jruby+jetty+sinatra stack:


only git clone done.you will have done this stack in local

second step:

create your rb file:

charting.rb
==========
require 'rubygems'
require 'sinatra'
require '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 'removeValue'
    def []=(column, value)
      @dataset.addValue value.to_java(:int), @row, column.to_s
    end

    def populate(hash)
      hash.each_pair do |column, value|
        @dataset.addValue value.to_java(:int), @row, column.to_s
      end
    end
  end

  def [](row)
    Inner.new self, row
  end
end


module Graph
  class StackedBar
    include_class 'java.io.File'
    include_class 'org.jfree.chart.ChartUtilities'
    include_class 'org.jfree.chart.JFreeChart'
    include_class 'org.jfree.data.category.DefaultCategoryDataset'
    include_class 'org.jfree.chart.ChartFactory'
    include_class 'org.jfree.chart.plot.PlotOrientation'

    def initialize(width=600, height=400, data=[])
      @width = width
      @height = height
      dataset = create_sample_data() if data.empty?
      @chart = create_chart(dataset)
    end

    def render_to_file(filename, format="png")
      puts "Rendering graph to #{filename}"
      javafile = java.io.File.new(filename)
      ChartUtilities.saveChartAsPNG(javafile, @chart, @width, @height)
    end

    private
    def create_sample_data
      # This also allows symbols as well as strings
      dataset = DefaultCategoryDataset.new
      dataset[:Submitted].populate(:A => 1, :B => 2, :C => 3)
      dataset[:Assigned].populate(:A => 1, :C => 1)
      dataset["In-work"].populate(:A => 3, :C => 3)
      dataset[:InVerfication][:A] = 1
      dataset[:Delivered][:A] = 2
      dataset[:Rejected][:B] = 1
      dataset[:Closed][:B] = 1
      dataset["On-hold"][:C] = 2
      dataset
    end

    def create_chart(dataset)
      chart = ChartFactory.createStackedBarChart("XYZ's Development Projects",
                                                 "Project Name", 
                                                 "Hours", 
                                                 dataset, 
                                                 PlotOrientation::VERTICAL, 
                                                 true, 
                                                 true, 
                                                 false)
      return chart    
    end
  end # class StackedBar  
end # class Graph

get '/' do
  sb = Graph::StackedBar.new
  @pp = "stacked_bar.png"
  img_pub = "public/images/"
  sb.render_to_file(img_pub+@pp)
  erb :graph
end




--
tommy xiao
E-mail: xiaods(AT)gmail.com

没有评论:

发表评论