iBrasten

My methods of calculating time are far superior to yours, in every way.

 

This is the blog of Brasten Sager, a software engineer, Mariners fan, guitarist, haphazard philosopher.

Scruffy 0.2.0

August 14, 2006 @ 03:56 PM

Well, I warned you.

Scruffy 0.2.0 has been released, and with it comes some significant changes. However, I said the majority of the changes would affect only the rendering system, and for the most part I stuck to that.

What’s new (Layout/Rendering) & what isn’t…

The result of these large changes is a pretty nifty rendering/layout engine that allows you to make your graphs look pretty much however you want. In addition, all elements on the graph have been made into components. You can move anything anywhere on the graph with ease.

To demonstate the capabilities of the renderers, I created a couple different layouts that are included with Scruffy. You can use them, or create your own.

Still no piecharts, but yes, those are coming, as are a few more chart types that I personally need.

Using Scruffy

The documentation is out of date at the moment. I will fix that in the next day or two to reflect the changes. The API for using the graph hasn’t changed much and is fairly simple. Below is a collection of code of resulting images.

Installing

1
2

    gem install scruffy

Using

1
2
3
4
5

    require 'scruffy'

    graph = Scruffy::Graph.new
             (...)

Examples

Split Graph

1
2
3
4
5
6
7
8
9
10
11
12
13
14

    graph = Scruffy::Graph.new
    graph.title = "Long-term Comparisons"
    graph.value_formatter = Scruffy::Formatters::Currency.new(:special_negatives => true, 
                                                              :negative_color => '#ff7777')
    graph.renderer = Scruffy::Renderers::Split.new(:split_label => 'Northeastern (Top) / Central (Bottom)')

    graph.add :area, 'Jeff', [20, -5, 100, 70, 30, 106, 203, 100, 50, 203, 289, 20], :category => :top    
    graph.add :area, 'Jerry', [-10, 70, 20, 102, 201, 26, 30, 106, 203, 100, 50, 39], :category => :top
    graph.add :bar,  'Jack', [30, 0, 49, 29, 100, 203, 70, 20, 102, 201, 26, 130], :category => :bottom
    graph.add :line, 'Brasten', [42, 10, 75, 150, 130, 70, -10, -20, 50, 92, -21, 19], :categories => [:top, :bottom]
    graph.add :line, 'Jim', [-10, -20, 50, 92, -21, 56, 92, 84, 82, 100, 39, 120], :categories => [:top, :bottom]
    graph.point_markers = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

… generates …

Stacking Graph Types

1
2
3
4
5
6
7
8
9
10

    graph = Scruffy::Graph.new
    graph.title = "Comparative Agent Performance"
    graph.value_formatter = Scruffy::Formatters::Percentage.new(:precision => 0)
    graph.add :stacked do |stacked|
      stacked.add :bar, 'Jack', [30, 60, 49, 29, 100, 120]
      stacked.add :bar, 'Jill', [120, 240, 0, 100, 140, 20]
      stacked.add :bar, 'Hill', [10, 10, 90, 20, 40, 10]
    end
    graph.point_markers = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

… generates …

Multi-viewport Multi-layered (This was Fun)

1
2
3
4
5
6
7
8
9
10
11

    graph = Scruffy::Graph.new
    graph.title = "Some Kind of Information"
    graph.renderer = Scruffy::Renderers::Cubed.new

    graph.add :area, 'Jeff', [20, -5, 100, 70, 30, 106], :categories => [:top_left, :bottom_right]    
    graph.add :area, 'Jerry', [-10, 70, 20, 102, 201, 26], :categories => [:bottom_left, :buttom_right]
    graph.add :bar,  'Jack', [30, 0, 49, 29, 100, 203], :categories => [:bottom_left, :top_right]
    graph.add :line, 'Brasten', [42, 10, 75, 150, 130, 70], :categories => [:top_right, :bottom_left]
    graph.add :line, 'Jim', [-10, -20, 50, 92, -21, 56], :categories => [:top_left, :bottom_right]
    graph.point_markers = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

… generates …

46 Responses to “Scruffy 0.2.0”

  1. Hi Brasten, I am looking at your graph engine and it looks wonderful. Can I save the graph as a pdf file or a jpeg? Joe Alotta

    jalotta@earthlink.net

  2. Looks great! Maybe we could combine our efforts? I was hoping to incorporate SVG support into Gruff, but haven't done it yet.

    topfunky

  3. Joe- graph.render(:width => 800, :as => 'JPG') The :as option is passed directly to rmagick, so it supports anything imagemagick does. I THINK that includes PDF. Your best bet it just to try :as => 'PDF' and see what happens. :) (And come back and let me know, too!) topfunky- Absolutely! Do you attend the Seattle.rb meetings? I haven't made it to one yet but I intend to. If so, we could chat then.

    brasten

  4. I definitely attend the Seattle.rb meetings. I'll see you there.

    topfunky

  5. It cant draw a pie chart ?

    broker

  6. topfunky and brasten... just don't call your combined effort "Sgruffy" :D

    Keith

  7. There ya go. :) As it is, 'Scruffy' is just a word that starts coming out of your mouth when you've been saying 'Gruff SVG' all weekend trying to explain what you're working on. broker- Soon. In fact I just got an email from A.J. Ostman who's working on the pie charts. Apparently basic pie charts are functional. He's working on some advanced pie charting capabilities now and we should be seeing that code soon. I'd guess we'll have pie charts in an official release sometime next week.

    brasten

  8. after doing a gem install scruffy i can't seem to use the library (crashes on require 'scruffy') happens on both my linux and mac box

    jserver

  9. jserver- Please send me any errors you're getting to my email address.. brasten -at- nagilum.com I'll take a look at it.

    brasten

  10. #7 great! Thanks a lot, I will wait for the pie charts.

    broker

  11. #7 great! Thanks a lot, I will wait for the pie charts.

    broker

  12. Great graphing library - thanks! I've noticed a small y-axis labelling bug: for graphs that cover only a very small range, the y-axis labels will repeat the same integer multiple times. http://dib.deefa.com/line.svg has an example of the ouput.

    James

  13. Thanks - great stuff. I have one query though: Would it be possible to position the legend labels at differing heights - values on the y-axis that correspond with the last x value? Essentially, this would eliminate the use of a legend in line graphs (if desired). Have been searching high and low for such a feature and would love to find it in my own backyard (RoR). Interested in your thoughts on this. The flexibility that scruffy provides has been much needed. Well done.

    Ned Jamieson

  14. In the stacked graph example I have found that if you remove one of the bars so there are just two it errors. For example:
       graph = Scruffy::Graph.new
        graph.title = "Comparative Agent Performance"
        graph.value_formatter = Scruffy::Formatters::Percentage.new(:precision => 0)
        graph.add :stacked do |stacked|
          stacked.add :bar, 'Jack', [30, 60, 49, 29, 100, 120]
          stacked.add :bar, 'Hill', [10, 10, 90, 20, 40, 10]
        end
        graph.point_markers = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
    graph.render(:width => 700, :as => 'PNG', :to => "#{RAILS_ROOT}/public/graphs/test.png")
    Results in:
    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.size
    
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/layers/stacked.rb:71:in `points'
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/layers/stacked.rb:63:in `top_value'
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/helpers/layer_container.rb:64:in `top_value'
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/helpers/layer_container.rb:41:in `inject'
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/helpers/layer_container.rb:64:in `top_value'
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/helpers/layer_container.rb:74:in `bottom_value'
    /usr/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy/graph.rb:137:in `render'
    Am I doing something stupid?

    Robert Hulme

  15. sorry, åtopfunky, sorry gruff sucks, please allow it to die and use scruffy instead

    carl

  16. You may want to note here that you may have to ‘require ‘rubygems” before require ‘scruffy’ thanks!

    roger pack

  17. from http://scruffy.rubyforge.org/doc/classes/Scruffy/Graph.html

    Here’s how to save it to a file… graph.render(:width => 700, :as => ‘PNG’, :to => ’<filename>’)

    This quite confused me. Thanks!

    roger pack

  18. Great stuff but I cannot seem to make it work. I have Macos X running Locomotive with RMagick bundle. I also have XTools and X11 libraries. I have created a controller such as;

    class GraphController < ApplicationController

    def index require 'rubygems' require 'scruffy'

    graph = Scruffy::Graph.new
        graph.title = "Comparative Agent Performance"
        graph.value_formatter = Scruffy::Formatters::Percentage.new(:precision => 0)
        graph.add :stacked do |stacked|
             stacked.add :bar, &#8216;Jack&#8217;, [30, 60, 49, 29, 100, 120]
             stacked.add :bar, &#8216;Jill&#8217;, [120, 240, 0, 100, 140, 20]
             stacked.add :bar, &#8216;Hill&#8217;, [10, 10, 90, 20, 40, 10]
        end
        graph.point_markers = [&#8216;Jan&#8217;, &#8216;Feb&#8217;, &#8216;Mar&#8217;, &#8216;Apr&#8217;, &#8216;May&#8217;, &#8216;Jun&#8217;]
    graph.render(:width => 800, :as => ‘JPG’)
    

    end end

    But it give me error. SyntaxError in GraphController#index

    /Users/opsitunes/scruffy/app/controllers/graph_controller.rb:17: syntax error, unexpected kEND, expecting ‘)’ RAILS_ROOT: /Users/opsitunes/scruffy/config/..

    Any ideas?

    BT

  19. brasten – looks awesome! I’m running into one small problem. Using the exact example in your post, the generated graph has all the labels (legend, x-axis, and y-axis labels, but NOT the title) offset down and to the right of where they should be. For example, “Feb” is under the second bar, and the y-axis label “10” is midway between 0 and the first horizontal line, as well as positioned to far to the right (similar horizontal offset to the “Feb” label).

    Any suggestions? Thanks!

    Michael

  20. Brasten, I really like the tool you’ve created, great job. One question: Is it possible to create a stacked bar graph with more than three bars? I have a project that requires me to have four data points per bar, and when I add the fouth I get an error regarding size.

    Jorge

  21. Great stuff! Enjoyed using so far… Just need to work out how to tweek appearance :)

    Kris

  22. Any word on the next revision or any updates for us? Pie charts?

    Marston A.

  23. C:\rally>ruby scruffy.rb scruffy.rb:1: uninitialized constant Scruffy (NameError)

    C:\rally>ruby scruffy.rb ./scruffy.rb:5: uninitialized constant Scruffy (NameError) from c:/ruby/lib/ruby/siteruby/1.8/rubygems/customrequire.rb:27:in ge m_original_require&#8217; from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inre quire’ from scruffy.rb:2

    stanislav

  24. Is it possible to rotate the labels on the x axis, I have quite a lot of dates and they are all overlapping… Many thanks in advance!

    Kris

  25. Hi,

    Scruffy looks like a nice tool. So far I have had trouble with a few things though:

    • Can I create a bar-graph with several series?
    • Can I add labels to the data-points in the graph area?
    • I guess you already know that bar-graphs have shifted labels on the x-axis

    Cheers,

    Burnce

    PS: It would be nice if you could add the “point_markers” line to the example in the rdoc (Scruffy::Graph). I spent some time figuring that one out.

    Burnce

  26. A nice toolset. I have two questions: Is it possible to decouple the point markers from the data being graphed? I need to graph time on the x-axis with a reasonable number of evenly-spaced tics for a large number of data points.

    milan

  27. Much nicer than gruff… however, i notice that you use relative sizes for stroke-width, font-size, etc… is there a way to override these settings w/ my own?

    Bob

  28. Hi, your tool is amazing. I’ve a little issue. I first copied one of the examples you showed here and I tried to render it but nothing was visualized on my web page (nothing appears in the page source code). Instead if I save the render on a file I can obtain the result I expect. Where I’m wrong? Thank you in advance

    stefano

  29. Hi,

    Looks like stefano and I are having the same problem! In my case I’m using the sample directly from a rails view. Will this work?

    Chris

    Chris

  30. Hi! I’m working on a Ruby on Rails application with Scruffy :) and I was wondering how to get the smiley faces that are in the third graph on th Scruffy site http://scruffy.rubyforge.org/? i need to submit this application very soon, so please help me out if you can :)

    Olga

  31. Two best practices:

    The y-axis often doesn’t display the values you would like to see there. e.g. if you create a line graph where the highest value is 10, you would see values on the y-axis of 0, 3, 5, 8, 10 (so a difference of 3, 2, 3, 2). Setting :max_value to a multiple of 4 which is just higher than the highest value of the array solves this problem. i.e.:

    def multiple_of_4(ar) (ar.max / 4.to_f).ceil * 4 end

    in which ar is the array with the values in the graph you try to add. When calling render set :max_value to the value this function returns. Now the y-axis would show something like: 0, 3, 6, 9, 12 and the highest point on the graph is still 10.

    The second best practice is to set :min_value to 0 when calling render if you have no negative values. If you want to display a value of 0 it will otherwise render a negative value on the y-axis (which you don’t want).

    Hope this helps for all.

    Roel van der Hoorn

  32. Scruffy doesn’t work on Mac with rmagick 2.x and ImageMagick 6.3.8. It fails on the following line in rmagickrasterizer: “image = Magick::Image::fromblob(svg)[0]” on line 16. I’m not sure why it fails, but after 12 hours of searching and thinking it was an ImageMagick problem, it clearly is a Scruffy/RMagick issue.

    Roel van der Hoorn

  33. Just wanted to let you know that I love scruffy! Getting it to work in a Rails environment took a little work, but it wasn’t very difficult at all. I love it and hope you add pi charts soon!

    Brad

  34. Hi all, I’ve just finished setting Scruffy up in one of my rails apps, and it looks great!! Tks Brasten for this nice tool :) I don’t master it and I’m missing lots of options; I’ve actually not found too much documentation. Any full guide of params, options,..? cheers!

    Monica

  35. Hello all, I am using scruffy for bar graphs on a soon-to-be public site on heroku. It works beautifully on localhost – thanks, Brasten! – but the deployment is to a headless server, requiring that certain public resources such as fonts be copied to a location available to the application. Where is the path to such resources obtained by gem scruffy? Thanks … I will post a link to our interesting energy-conservation site after I get it deployed!

    Milan

  36. what do include veiw file ?

    Kohei

  37. Hi, I’m using scruffy to generate graphs, but I’m a newbee to ruby. I got the graphs working, but I have a question about rendering the graph.

    Right now I’m rendering the graph as a PNG image (in the controller) to the public/images folder, and loading it with an IMG tag in the view.

    This could give problems when 2 users ask for their graph at the same time (yes, they are personal). But also people could just refresh the URL for the graph and look at other peoples values.

    I’d like to know how to render the graph directly to the browserscreen without saving it in a public place.

    I’ve tried to render the graph in the view, but this just gave the PNG in hex on my screen.

    Can anyone help me ?

    Thx

    Niels

  38. Hello, I’d like to know how to reder the graph directly to the browserscreen without saving it somewhere in a public folder.

    I used the reder function in the view, this gave me the HEX index of the PNG graph.

    Can anyone tell me how to change that HEX code to a jpg redered to the screen

    Niels

  39. Nice Site! http://google.com

    tcm visa

  40. wellsfargo bank

  41. tcm: if you don’t know, best not to comment.

    Niels:

    In the graph.render command, you need to write the image to a file (so that your browser has something to work with).

    graph.render(:width => 600, :as => ‘JPG’,:to => “public/images/yourfile.jpg”)

    Clearly, replace the :width value with your desired width, JPG with the mime type you want to use, and yourfile.jpg with a file name of your choosing.

    Once render has been called, you will have a (permanent, until you overwrite it) file called “yourfile.jpg” in your images directory. As such, this code can be used in a controller, partial or view (even a model, though I wouldn’t recommend that!)…. so long as the code gets executed before you need to display it on the page. To show it in your view, just use an image tag:

    <%= image_tag(“yourfile.jpg”) %>

    Hope that helped!

    Damien

  42. Hi Brasten, all. first of all , thanks for all the work done in Scruffy. I am using the version 0.2.2 and discovered that there is no way to put in the same bar graph multiple series of data by side and not stacked. I just developed a working extension of the bar to add support for multiple series in a bar graph, I called it multibar :-) Working with the code I was able to identify a couple of very little changes to add how can I submit this work to the community? is the development on the rubyforge project stopped? BR Giuseppe

    pinoatrome

  43. This is really cool. I installed scruffy, however, I have not been able to find version 0.2.3 for download. Where is it available? Rubyforge only has 0.2.2.

    Amrita

  44. LOL that doesn’t even make any sense!

    Andrew

  45. Is there ability to create a chart with 2 or more y-axis on different unit, for example: I have wireless data: call attempts: 100000, 200000, the drop calls rate: 2%, 1%, when I show it in the chart with double y-axis: all the drop call rate line chart is not readable.

    Shawn

  46. I love the plugin. It looks amazing and is really using to use for a lot of different varieties of graphs.

    However, I’m having a problem. I’m trying to add x and y axis labels, but I can’t seem to figure out how. Am I just missing something or is this even part of the functionality?

    sean