Generate pie chart with Gruff gem in Ruby - 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

Related

Kendo Chart Bar color is not changing

The above image is of my chart.
The default color of this kendo chart is not changing - I tried to set the color but it didn't work.
Here is my code:
#(Html.Kendo().Chart(item.UsageData)
.Name(item.Type)
.ChartArea(ca => ca.Background("transparent").Height(380))
.Legend(l => l.Visible(true).Position(ChartLegendPosition.Bottom))
.Series(s => s.Column("Cost", "lblUsage").Labels(l => l.Visible(true).Template("$ #= value #")))
.CategoryAxis(x => x.Categories(item.UsageData.Select(y => y.lblUsage)).Labels(l => l.Rotation(-25)).MajorGridLines(m => m.Visible(false)).MinorGridLines(minor => minor.Visible(false)))
.Tooltip(tooltip => tooltip.Visible(true).Template("#= dataItem.lblUsage #: #= value # $").Padding(5).Background("#3893ae"))
)

Kendo UI Bar Chart Labels Rotation

I have a Kendo UI (Telerik) bar chart with long label names. When I set the label rotation to anything outside of 0,180,90,360 the labels slant but they use the center of the text as the slant point instead of the start of the text. This causes all the labels to be off by a full bar.
http://snag.gy/m2XxJ.jpg
Is there a way to get the chart to use the start of the label as the rotation point instead of the center?
The only way I've gotten the labels to line up properly when using rotation, is to also set the padding.
Sample categoryAxis
categoryAxis: { field: 'name', labels: { rotation: -60, padding: { right: 10 }}}
JSbin sample http://jsbin.com/zoloc/1/edit
Kendo Documentation http://docs.telerik.com/kendo-ui/api/dataviz/chart#configuration-categoryAxis.labels.padding
You can use both rotation and margin to arrange the category axis text like this,
.CategoryAxis(axis => axis
.Categories(model => model.StudentName).Labels(labels => labels.Rotation(330).Margin(-5,45,0,0).Visible(true))
.MajorGridLines(lines => lines.Visible(false))
.Line(line => line.Visible(false))
)
Response from Telerik:
You have a valid point. Excel for example rotates the text around its left edge.
We'll look into this issue, but for the moment I can only suggest the multi-line option in the upcoming Q2 release.
You'll be able to split the labels by using a new-line character:
categories: ["J.R. SIMPLOT\nCOMPANY", ...]

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.

ruby spreadsheet row background color

I am trying to parse an excel spreadsheet using "spreadsheet". How could I get the background color of each row?
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => 'Name'
format = Spreadsheet::Format.new :color=> :blue, :pattern_fg_color => :yellow, :pattern => 1
sheet.row(0).set_format(0, format) #for first cell in first row
or
sheet.row(0).default_format = format #for entire first row
you can iterate over each row/cell and apply style exactly where you want
I was looking around for colors that you can use for the background color of a cell. For example:
Spreadsheet::Format.new({ :weight => :bold, :pattern => 1, :pattern_fg_color => :silver })
I couldn't find good info on which colors I could use for :pattern_fg_color. I decided to look for Excel help and found: http://dmcritchie.mvps.org/excel/colors.htm (at "The DOS assignments of the 16 colors").
It looks like the top 16 colors work:
0 Black, 1 Navy, 2 Green, 3 Teal, 4 Maroon, 5 Purple 6 Olive, 7 Silver,
8 Gray, 9 Blue, 10 Lime, 11 Aqua, 12 Red, 13 Fuschia, 14 Yellow, 15 White
I've just been trying to figure out the same, and seems like in the current version (0.6.5.9) of Spreadsheet gem the attribute of cell's background color is not supported in reader (you can only define background color in cells for writing).
Here's how to check all currently available cell attributes:
a = Spreadsheet.open('/folder/spreadsheet.xls')
puts a.worksheets[0].row(<rownumber>).format(<columnnumber>).inspect
After some experimentation however I figured out that not all of them are properly extracted. The good news is that the developers promise to implement better support for cell formats in future versions, so we just need to be patient :)

Ruby and Google Charts - using gchartrb

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 :)

Resources