如何将整数数组转换为人力车的 x/y 坐标

How to convert an array of integers into x/y coordinates for rickshaw

本文关键字:坐标 人力车 整数 数组 转换      更新时间:2023-09-26

我正在尝试将很棒的JS图形库人力车集成到我的项目中。人力车可以在这里找到:http://code.shutterstock.com/rickshaw/

我要绘制的数据在一个简单的数组中:

data = [51929, 65932, 49119, 50379, 103501, 92430, 93107, 105710, 115200, 109283]

我想使用数组中每个元素的索引作为 x 坐标,将元素本身用作 y 坐标。我需要将其格式化为类似于以下格式的内容,以便 Richshaw 可以发挥它的魔力:

data = [ { x: 0, y: 40 }, { x: 1, y: 49 }, { x: 2, y: 17 }, { x: 3, y: 42 } ];

我已经尝试了各种方法,我最接近的是生成一个带有哈希的引号数组,如下所示:

clean_data = []
data.each_with_index do |value, index|
  clean_data << "{x: #{index}, y: #{value} }”
end 
clean_data.to_a

返回:

["{x: 0, y: 51929}", "{x: 1, y: 65932}", "{x: 2, y: 49119}" ... etc...]

所以我的问题是,如何删除上面的引号以获得一个哈希数组,或者,如果我走错了轨道:如何格式化数组以使其可呈现给人力车?我更喜欢在这里使用 Ruby,但 JavaScript 中的任何内容也很棒。

谢谢。

你很接近。试试这个:

data = [51929, 65932, 49119, 50379, 103501, 92430, 93107, 105710, 115200, 109283]
clean_data = []
data.each_with_index do |value, index|
  clean_data << {:x => index, :y => value}
end 
clean_data.to_json
p data.map.with_index{|val, idx|
  Hash[:x,idx,:y,val]
}
#=> [{:x=>0, :y=>51929}, {:x=>1, :y=>65932}, {:x=>2, :y=>49119} .....