Looking for a charting library in Ruby...

Discussion in 'Ruby' started by Tom Copeland, Oct 3, 2003.

  1. Tom Copeland

    Tom Copeland Guest

    Tom Copeland, Oct 3, 2003
    #1
    1. Advertising

  2. This is a multi-part message in MIME format.
    --------------040609040008020505060805
    Content-Type: text/plain; charset=us-ascii; format=flowed
    Content-Transfer-Encoding: 7bit

    Hi Tom,

    Tom Copeland wrote:

    >...searching the RAA came up with ruby-gdchart:
    >
    >http://raa.ruby-lang.org/list.rhtml?name=ruby-gdchart
    >
    >Is this what folks are using?
    >


    I have been using it for a while and it has fit all my requirements.
    I have created my own class to save some typing.(Attached).

    Then you can do something like this:

    require 'graph'
    def graph_txt(txtfile,labelcolumn,datacolumn,delimeter=" ")



    data=Array.new

    labels=Array.new

    File.new(txtfile).each do |lines|

    lines.split("\n").each do |line|

    data << line.split(delimeter)[datacolumn]

    labels << line.split(delimeter)[labelcolumn]

    end

    end

    data.map! {|x| x.to_i }



    g=Graphs.new

    options={

    "title" => "Time",

    "type" => "BAR3D",

    "name" => "1",

    "data"=> data,

    "labels"=> labels,

    "ytitle"=> "Y",

    "xtitle"=> "X",

    "dir" =>"/home/rbermejo",

    "xlabel_spacing" => 0

    }



    g.create(options)

    end



    graph_txt("txtfile",0,1)





    -rodrigo
    -----------------
    $ ls /usr/bin
    ls
    ruby
    $
    ----------------

    >
    >http://ai-app-prog.rubyforge.org/
    >
    >Thanks,
    >
    >tom
    >
    >







    --------------040609040008020505060805
    Content-Type: text/plain;
    name="graph.rb"
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline;
    filename="graph.rb"

    #!/usr/bin/ruby

    class Graphs

    require 'GDChart'

    def initialize
    @options=" "
    self.default
    end

    def default
    if File.exist?("graph_default.rb")
    require 'graph_default'
    else
    @graph_default=Hash.new
    @graph_default["bgcolor"] = 0x3a3a3a
    @graph_default["numset"] = 1
    @graph_default["bgimage"] = "/icons/graphbg.gif"
    @graph_default["dir"] = "./"
    @graph_default["file"] = "testing.png"
    @graph_default["extcolor"] = [ 0xc6c0c0, 0x466387 ]
    @graph_default["gridcolor"] = 0x466387
    @graph_default["labels"] = [ ]
    @graph_default["titlecolor"] = 0xc6c0c0
    @graph_default["linecolor"] = 0xefe413
    @graph_default["title"] = "graph"
    @graph_default["xlabel_spacing"]= 0
    @graph_default["xsize"] = 350
    @graph_default["xtitle"] = ""
    @graph_default["ysize"] = 350
    @graph_default["ytitlecolor"] = 0xc6c0c0
    @graph_default["xtitlecolor"] = 0xc6c0c0
    @graph_default["ytitle"] = "y"
    end
    end


    def setvalue(var)

    if not @options.has_key?(var)
    @options[var]=@graph_default[var]
    end

    return @options[var]

    end



    def create(options)

    @options=options
    @options.each {|k,v| eval " \@#{k}= v "}

    begin

    gdc = GDChart.new

    #############
    # TITLE #
    #############
    gdc.title_size = GDChart::MEDBOLD
    gdc.title = setvalue("title")
    gdc.ytitle = setvalue("ytitle")
    gdc.xtitle = setvalue("xtitle")
    gdc.TitleColor = setvalue("titlecolor")
    gdc.YTitleColor = setvalue("ytitlecolor")

    #############
    # BODY #
    #############
    gdc.ExtColor = setvalue("extcolor")
    gdc.LineColor = setvalue("linecolor")
    gdc.BGImage = setvalue("bgimage")
    gdc.BGColor = setvalue("bgcolor")
    gdc.GridColor = setvalue("gridcolor")
    gdc.xlabel_spacing = setvalue("xlabel_spacing")
    gdc.bar_width = setvalue("bar_width")
    gdc.border = setvalue("border")
    gdc.ylabel_fmt= setvalue("ylabel_fmt")

    #############
    # FILE #
    #############
    @ysize=setvalue("ysize")
    @xsize=setvalue("xsize")
    @name=setvalue("name")
    @dir=setvalue("dir")
    @file=File.open "#{@dir}/#{@name}.png" , "w"
    #############
    # DATA #
    #############
    @numset=setvalue("numset")

    ############
    # MAIN #
    ############
    if @type =~ /pie/i then
    eval " gdc.out_graph(\@ysize,\@xsize, \@file, GDChartPie::#{@type}, \@data.length, \@labels,\@numset, \@data) "
    else
    eval " gdc.out_graph(\@ysize,\@xsize, \@file, GDChart::#{@type}, \@data.length/@numset, \@labels,\@numset, \@data,\@data) "
    end

    rescue Exception => error
    print "create error:#{error}\n"

    end

    end



    end







    --------------040609040008020505060805--
    Bermejo, Rodrigo, Oct 3, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shanmuhanathan T
    Replies:
    1
    Views:
    550
    Oliver Wong
    Oct 4, 2005
  2. Vatsan
    Replies:
    1
    Views:
    2,613
    Fraser Ross
    Jan 20, 2005
  3. ameyas
    Replies:
    10
    Views:
    1,129
    ameyas
    Jan 23, 2007
  4. Helmut Jarausch

    Re: svg-chart 1.1 SVG Charting Library

    Helmut Jarausch, May 20, 2008, in forum: Python
    Replies:
    0
    Views:
    460
    Helmut Jarausch
    May 20, 2008
  5. mk
    Replies:
    3
    Views:
    270
    John Bokma
    Feb 3, 2010
Loading...

Share This Page