Question

I am using open flash chart 2 (the plugin) in my rails application. Everything is looking smooth except for the range on my x axis. I am creating a line to represent cell phone plan cost over a specific amount of usage and I'm generate 8 values, 1-5 are below the allowed usage while 6-8 are demonstrations of the cost for usage over the limit.

The problem I'm encountering is how to set the range of the X axis in ruby on rails to something specific to the data. Right now the values being displayed are the indexes of the array that I'm giving. When I try to hand a hash to the values the chart doesn't even load at all.

So basically I need help getting a way to set the data for my line properly so that it displays correctly, right now it is treating every value as if it represents the x value of the index of the array.

Here is a screen shot which may be a better description than what I am saying: http://i163.photobucket.com/albums/t286/Xeno56/Screenshot.png Note that those values are correct just the range on the x-axis is incorrect, it should be something like 100, 200, 300, 400, 500, 600, 700

Code:

y = YAxis.new
y.set_range(0,100, 20)

x_legend = XLegend.new("Usage")
x_legend.set_style('{font-size: 20px; color: #778877}')

y_legend = YLegend.new("Cost")
y_legend.set_style('{font-size: 20px; color: #770077}')

chart =OpenFlashChart.new
chart.set_x_legend(x_legend)
chart.set_y_legend(y_legend)
chart.y_axis = y

line = Line.new
line.text = plan.name
line.width = 2
line.color = '#006633'
line.dot_size = 2
line.values = generate_data(plan)     
chart.add_element(line)

def generate_data(plan)
  values = []
  #generate below threshold numbers
  5.times do |x|
    usage = plan.usage / 5 * x 
    cost = plan.cost
    values << cost
  end
  #generate above threshold numbers
  3.times do |x|
    usage = plan.usage + ((plan.usage / 5) * x)
    cost = plan.cost + (usage * plan.overage)
    values << cost
  end
  return values
end

Also the other problem I'm having is I can't just add a few points as any values I give are taken as x being the elements position in the array and y being the value, whereas I need to be able to specify the x and y values for each point so I don't have to buffer my array with a bunch of null values

Was it helpful?

Solution 3

I ended up ditching open flash chart 2 and going with Flotr

OTHER TIPS

You have to create an XAxis Object to set the labels:

x = XAxis.new
x.labels =  [100, 200, 300, 400, 500, 600, 700].collect(&:to_s)
chart.x_axis = x

From my experience there are many ways to do things with Open Flash Chart 2. mikezter example above is almost correct; however, this is how I set the XAxis labels.

x = XAxis.new
x.set_labels([100, 200, 300, 400, etc]).collect(&:to_s)
chart.x_axis = x

Note the only difference between my example and mikezter's is I use:

x.set_labels([array values etc])

instead of

x.labels = [array values etc] 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top