Ruby and Google Charts - using gchartrb - ruby

I'm having my first go at using Google charts using a Ruby wrapper called gchartrb. My first attempt was to draw a bar chart, which works fairly well, except that the largest value (Trend 2 - 800) hits the top of the y axis. I'm guessing this is a Google Charts issue, but I was wondering if there was a way around this problem so all values scale correctly?
labels = [1, 2, 3, 4]
GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false) do |bc|
bc.axis :x, :labels => labels,
:color => '000000' # Months
bc.axis :y, :labels => [0, 100, 200, 300, 400, 500, 600, 700, 800, 900],
:color => '000000' # Number of books
bc.data "Trend 1", [100,200,300,400], '0000ff'
bc.data "Trend 2", [500,600,700,800], 'ff0000'
bc.width_spacing_options :bar_width => 50, :bar_spacing => 0, :group_spacing => 0
puts "\nBar Chart"
puts bc.to_url
end

Gave up on this gem and ended up using Google Charts directly.

for scaling issue, there is an option called 'range' for bc.axis attribute like,
bc.axis :y, :color => '000000', :range => [0,max_scale_value]
you can set max_scale_value to me maximum value of your data values.
1) If you are displaying bars side by side, you can set max_scale_value to 800, as per your data. i.e. max value from both of your arrays.
2) If you are displaying stacked bars(one above the other), then you should set max_scale_value to be maximum of [(100+500),(200+600),(300+700),(400+800)]
3) you need to remove labels property for y -axis,because it will automatically scale as per the values and give you labels.
thus, you wont have the scaling issue.
Hope it helps :)

Related

why ruby prawn draw from bottom

I am using prawn in ruby and draw a sample text, but why it render from bottom instead of from top?
require 'prawn'
require 'prawn/measurement_extensions'
def self.generate
pdf = Prawn::Document.new(page_size: 'A4', margin: [5.mm, 10.mm, 5.mm, 10.mm])
pdf.draw_text "Hello world", at: [0, 0]
pdf.dash 2, space: 3
pdf.vertical_line 0, 100, :at => 0
pdf.line_width = 0.5
pdf.stroke
pdf.render_file "#{__dir__}/test.pdf"
end
Result:
This is because, as stated in the manual - https://github.com/prawnpdf/prawn/blob/c504ae4e683017d7afadece084734a9190230cd8/manual/basic_concepts/origin.rb#L5, PDF documents have their origin (0,0) at the bottom left of the page. Therefore when you specifically tell something to draw at [0,0] it will draw at the bottom left of its encapsualating bounding box, which in your case is the page.

Getting custom colors and thresholds to work with D3 RelationshipGraph

Based on this example:
https://cdn.rawgit.com/hkelly93/d3-relationshipGraph/master/examples/index.html
D3 should allow me to create this chart and define colors and thresholds for when values change color. The function accepts some custom settings:
var graph = d3.select('#graph').relationshipGraph({
maxChildCount: 10,
valueKeyName: 'Story title',
thresholds: [6, 8, 10],
colors: ['red', 'yellow', 'green'],
showTooltips: true
})
But I'm not getting a graph with three colors when I load data fitting into all 3 ranges. I want 0-6 to appear red, 7-8 to appear yellow, and 9-10 to appear green. Here's the data loaded (excerpt):
[
{"parent": "2012-October", "organization": "WEWASAFO", "value": 10, "Story title": "NUTRITION"},
{"parent": "2012-April", "organization": "Jitegemee", "value": 5, "Story title": "Life in the street"},
{"parent": "2011-May", "organization": "KENYA YOUTH BUSINESS TRUST (KYBT)", "value": 2, "Story title": "BUSINESS"}
]
Everything else parses correctly, except combining custom colors an custom thresholds on the same chart. Either one alone works, but not both together.
The source repo is here with some docs:
https://github.com/hkelly93/d3-relationshipgraph
From that documentation:
thresholds: [100, 200, 300], // The thresholds for the color changes. If the values are strings, the colors are determined by the value of the child being equal to the threshold. If the thresholds are numbers, the color is determined by the value being less than the threshold.
colors: ['red', 'green', 'blue'], // The custom color set to use for the child blocks. These can be color names, HEX values, or RGBA values.
It doesn't explicitly state that the child colors correspond to the order that the thresholds appear. And all blocks appear red in this example.
I tested the code here: https://jsfiddle.net/cgrx3e9m/
This turned out to be a bug in the module itself. I notified the author and he fixed the way it sorts thresholds so it matches up with corresponding colors now.

How to store the barcode only as a .png instead of on a A4 page using rghost?

I'm using rghost-barcode to generate an Aztec barcode. But every time I create a barcode it get's saved on a full A4 page with the barcode at the top left.
Why is this happening? How do I save the barcode only to the PNG with the dimensions I want (720px x 720px).
Here's what I'm currently trying:
doc = RGhost::Document.new
doc.barcode_azteccode('This is Aztec Code',{:text=>{:size=>8}, :format=>"full"})
doc.render :png, :resolution => 100, :filename => "my_barcode.png"
And this is the result:
The gem uses postscript/ghostsript as backend, so it is always producing documents, and then inserts barcodes into this document. And these document seem to have A4 as default.
According the rghost wiki, you can specify how big the document should be:
doc = RGhost::Document.new :paper => [15,10]
I'm afraid the dimension is in inches (the wiki does not state the dimension)
You have to set the paper size and position your barcode. Notice that the y positioning of the barcode is from the bottom of the page. After a bit of trial and error, this works fine for example:
#paper is x , y
doc=RGhost::Document.new :paper => [4.4, 3.2]
#y is from bottom of page
doc.barcode_ean13('2112345678900',
{:text=>{:size=>7},
:enable=>[:text],
:scale=>[1,1],
:x => 0.5, :y => 0.4})
doc.render :png, :resolution => 100, :filename => "ean13.png"
If you want to increase the resolution of the code, just doubling the scale, as well as all the others parameters (paper size, and barcode x, y) should work.

Generate pie chart with Gruff gem in Ruby

I am attempting to create a pie chart using the gruff gem, but my chart is a black abyss regardless of what I do. This is my code:
association_disposition_pie_chart = Gruff::Pie.new
association_disposition_pie_chart.title = "Visual Pie Graph Test"
association_disposition_pie_chart.data 'Solved', 10
association_disposition_pie_chart.data 'Action Required', 50
association_disposition_pie_chart.theme = {
:colors => ['#A5D8D8', '#EFAD1C'],
:font_color => 'black',
:background_colors => 'white'
}
association_disposition_pie_chart.write("association_disposition_pie_chart.jpg")
Why is this creating a black pie chart? The background is white, the font_color is black, but so is the entire chart. I want the chart pieces to be the colors specified in :colors.
EDIT
Screenshot:
http://i39.tinypic.com/33ne1r6.jpg
This is mentioned in the documentation:
You can set a theme manually. Assign a hash to this method before you send your data.
graph.theme = {
:colors => %w(orange purple green white red),
:marker_color => 'blue',
:background_colors => %w(black grey)
}
:background_image => 'squirrel.png' is also possible.
(Or hopefully something better looking than that.)
Although the source is more helpful:
# File 'lib/gruff/base.rb', line 300
def theme=(options)
reset_themes()
defaults = {
:colors => ['black', 'white'],
:additional_line_colors => [],
:marker_color => 'white',
:font_color => 'black',
:background_colors => nil,
:background_image => nil
}
#theme_options = defaults.merge options
#colors = #theme_options[:colors]
#marker_color = #theme_options[:marker_color]
#font_color = #theme_options[:font_color] || #marker_color
#additional_line_colors = #theme_options[:additional_line_colors]
render_background
end
I think maybe the problem is your colors attribute - :colors => ['#A5D8D8', '#EFAD1C'] - as Shaun Frost Duke Jackson mentioned, it looks like you need to use add_color('#c0e9d3') to do that, but the documentation isn't clear where you do that if you're defining the theme in line. It might be easier to add your own theme in the THEMES module:
LUIGIS_THEME = {
:colors => [
'#A5D8D8',
'#EFAD1C'
],
:marker_color => '#55ae36',
:font_color => 'black',
:background_colors => 'white'
}
which is then called with g.theme = Gruff::Themes::LUIGIS_THEME
While using imagemagick-no-hdri and the default rmagick gem the pie graphs would become black and white. I was able to fix this issue by doing the following
Install imagemagick
git clone git#github.com:rmagick/rmagick.git
gem build rmagick.gemspec
gem install ./rmagick-2.13.2.gem

Prawn: Is there a way for vertical alignment of all content in a box?

I'm trying to position some content vertically centered in a bounding_box. With a single text this is no problem:
bounding_box([0, bounds.top], :width => pdf.bounds.right, :height => pdf.bounds.top) do
text "vertically aligned in the surrounding box", :valign => :center
end
But what can I do if a have multiple elements in my bounding box:
bounding_box([0, bounds.top], :width => pdf.bounds.right, :height => pdf.bounds.top) do
text "vertically aligned in the surrounding box", :valign => :center
text "vertically aligned in the surrounding box", :valign => :center
end
That won't work, the text is overlaid when you try this...
I'm looking for a way to group the whole content of the bounding_box and then align that whole group vertically. Is there any way to do this with prawn??
Thanks a lot for your help!
Chris
If you only have text lines, you can still use formatted_text with \n in your text :
formatted_text [
{ text: "#{line1}\n" },
{ text: "#{line2}" }
],
valign: :center,
leading: 6
I'm still trying to figure out how to handle a picture/legend group, since even tables don't seem to do the trick.

Resources