30 Jul
Open Flash Chart II – Horizontal Bar Graph
Just got another comment asking about making a horizontal bar chart. This example is based on teethgrinder's horizontal bar example. So you will need to get the latest plugin since this required that I add a few things as of this article.
Here is the graph we are after in this example:
More Open Flash Chart II examples.
And here is the code (the controller):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class TestItController < ApplicationController def index @graph = open_flash_chart_object(600,300,"/test_it/graph_code") end def graph_code # based on this example - http://teethgrinder.co.uk/open-flash-chart-2/horizontal-bar-chart.php title = Title.new("HBar Graph") hbar = HBar.new # could also do it one at a time with hbar.append_value(...) or # hbar.values << ... hbar.values = [HBarValue.new(0,4), HBarValue.new(4,8), HBarValue.new(8,11)] chart = OpenFlashChart.new chart.set_title(title) chart.add_element(hbar) x = XAxis.new x.set_offset(false) x.set_labels(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']) chart.set_x_axis(x) y = YAxis.new y.set_offset(true) y.set_labels(["Make garden look sexy","Paint house","Move into house"]) chart.set_y_axis(y) render :text => chart.to_s end end |
And in your view (index.html.erb):
1 2 3 4 |
<script type="text/javascript" src="/javascripts/swfobject.js"></script> <%= @graph %> |
Good Luck!
13 Responses
to “Open Flash Chart II – Horizontal Bar Graph”
Sorry, comments for this entry are closed at this time.
This example wasn’t working for me. I checked the output of your graph by viewing the returned json with what my version was returning and your copy differed in how the "labels" were output.
I removed the set_labels and labels methods in the plugin and now I get the same results as your example.
Any reason why the label code is needed as it seems to "not work"?
Thanks
The month names on the x-axis also don’t seem to display. I tried a few things, but nothing seemed to work.
@Andrew K. – Hey thanks .. the label code should not be at the open flash chart object level, I think those changes are only needed for XAxis. I fixed this in the most recent code and committed. Also, it should not be x.set_labels_from_array, it should have just be x.set_labels. So the month names should show up now.
In the graphs of horizontal bars can they add lines on every bar? To do graphs Gantt? Or already there exists inside the plugin a way of doing graphs Gantt?
Also might it do that they are read the months, activities and the points to create the bars of the database?
Hello! Thank you for this. I was wondering if there’s a way to make HBar 3-D? Thanks!
Elijah
@Elijah – not at the moment – <a href="http://teethgrinder.co.uk/open-flash-chart-2/horizontal-bar-chart.php">official site</a>.
I have started looking in to the AS behind OFCII a little more and I am going to start implementing some of these suggestions, but no promises 🙂
Would there be any simple way to allow for stacked bars? I’m using this to graph a schedule; X-axis displays dates and y-axis displays names. Person A could be working from day1 to day3, AND day5 to day7. This would be two bars in the same row. Any tips would be great.
Mikyl: you can add more HBars to one chart, just try it and I think you’ll get it.
Horizontal stacked bar support is missing in official ofc2. For a patch, see
http://ofc2dz.com/OFC2/examples/HorizontalStackedBars.html
This Example worked as it is intended. However there are couple of things I need to clarify.
In the graph, I am getting the text like “Page Views”.. How to remove this?
And also when I add another 5 elements to the Y.axis, the Graph is broken. How to add as many elements to the Y.axis? Any way that we can manage the Bar width and color?
And also I am not getting the Tooltip! please help!
Munendra Chevuru (Muni)
1) “Page Views” text – not sure what you mean exactly, where is it showing up? Does the example you are using show it as well? Is it something that can be taken out in the code?
2) Adding 5 elements to Y Axis breaks Graph – do you have the same number of elements as you do labels? I’ve had thousands of entries, so we’d have to look at what you have.
3) No tooltip – this might be related to #2 above. Post a snippet of your code to gist or pastie.
Hi I need to plot horizontal groups as shown in this link
http://ofc2dz.com/OFC2/examples/HorizontalBars.html
but I see an improvement here to apply how to resolve this