0

I believe that I may be missing something here, so please bear with me as I explain two scenarios in hopes to reconcile my misunderstanding:

My end goal is to create a dataset that's acceptable by Highcharts via lazy_high_charts, however in this quest, I'm finding that it is rather particular about the format of data that it receives.

A) I have found that when data is formatted like this going into it, it draws the points just fine:

[0.0000001240,0.0000000267,0.0000000722, ..., 0.0000000512]

I'm able to generate an array like this simply with:

array = Array.new

data.each do |row|
  array.push row[:datapoint1].to_f
end

B) Yet, if I attempt to use the map function, I end up with a result like and Highcharts fails to render this data:

[[6.67e-09],[4.39e-09],[2.1e-09],[2.52e-09], ..., [3.79e-09]]

From code like:

array = data.map{|row| [(row.datapoint1.to_f)] }

Is there a way to coax the map function to produce results in B that more akin to the scenario A resultant data structure?


This get's more involved as I have to also add datetime into this, however that's another topic and I just want to understand this first and what can be done to perhaps further control where I'm going.

Ultimately, EVEN SCENARIO B SHOULD WORK according to the data in the example here: http://www.highcharts.com/demo/spline-irregular-time (press the "View options" button at bottom)

Heck, I'll send you a sucker in the mail if you can fill me in on that part! ;)

2
  • 1
    I wouldn't complain that the library is "rather particular". It works when you pass an array of floats. It doesn't work when you pass an array of arrays. That's totally reasonable. You should know the difference and know which one to use. Commented Jan 11, 2012 at 6:15
  • Unfortunately @DavidGrayson that's not exactly the case it seems. If you look at the example above I gave on the highcharts site the data structure is an array of arrays. Now, I can understand perhaps why it didn't like the float embedded into an array, however the real crux of my issue is still hanging out even though I posted this particularly limited question. I'd hoped to gain insight into why the other scenario of [[date_int,float],[date_int, float], ...] would not work, but when I line my output up precisely with that example, I'm still in a quandary. So yes, bittersweet ending so far Commented Jan 11, 2012 at 6:35

3 Answers 3

2

You can fix arrays like this

[[6.67e-09],[4.39e-09],[2.1e-09],[2.52e-09], ..., [3.79e-09]]

that have nested arrays inside them by using the flatten method on the array.

But you should be able to avoid generating nested arrays in the first place. Just remove the square brackets from your map line:

array = data.map{|row| row.datapoint1.to_f }
Sign up to request clarification or add additional context in comments.

Comments

1

Code

a = [[6.67e-09],[4.39e-09],[2.1e-09],[2.52e-09], [3.79e-09]]

b = a.flatten.map{|el| "%.10f" % el }
puts b.inspect

Output

["0.0000000067", "0.0000000044", "0.0000000021", "0.0000000025", "0.0000000038"]

Comments

1

Unless I, too, am missing something, your problem is that you're returning a single-element array from your block (thereby creating an array of arrays) instead of just the value. This should do you:

array = data.map {|row| row.datapoint1.to_f }
# => [ 6.67e-09, 4.39e-09, 2.1e-09, 2.52e-09, ..., 3.79e-09 ]

1 Comment

I left out a lot of additional logic that's actually in that block, but roughly I was lost in the logic and just needed to yank the brackets as per @DavidGrayson's first post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.