我试图将一对数据结构中包含的数据转换为与HighCharts(A JS Chart Library)兼容的格式。通过解析JSON文件来填充初始数据结构。 (我不能改变这一步,而必须使用生成的数据结构)初始数据结构如下所示:
初始化:
@areaBreakdown = Hash.new { |hash, key| hash[key] = Hash.new }
@deptBreakdown = Hash.new { |hash, key| hash[key] = Hash.new }
示例数据@areaBreakdown:
{#<Area id: 1, area_code: "123", created_at: "2014-04-04 20:23:11",
updated_at: "2014-04-04 20:23:11", name: "testarea1",
org_code: "123456">=>{"areaScore"=>46.65},
#<Area id: 2, area_code: "456", created_at: "2014-04-04 20:23:11",
updated_at: "2014-04-04 20:23:11", name: "testarea2",
org_code: "123456">=>{"areaScore"=>52.98}}
示例数据@deptBreakdown:
{#<Dept id: 1, dept_code: "78910", created_at: "2014-04-04 20:23:11",
updated_at: "2014-04-04 20:23:11", name: "testdeptunderarea1",
area_id: 1>=>{"deptScore"=>46.65},
#<Dept id: 2, dept_code: "4567", created_at: "2014-04-04 20:23:11",
updated_at: "2014-04-04 20:23:11", name: "testdept1underarea2",
area_id: 2>=>{"deptScore"=>49.81},
#<Dept id: 3, dept_code: "99999", created_at: "2014-04-04 20:23:11",
updated_at: "2014-04-04 20:23:11", name: "testdept2underarea2",
area_id: 2>=>{"deptScore"=>56.15}}
Highchart为我正在尝试构建的图表采用两个独立的数据结构。 必须将 areaBreakdown 转换为哈希数组,每个索引对应不同的区域。数组名称是 areaScores 。
areaScores的硬编码版本:
@hardCodedAreaScores = Array.new
# Push a Hash onto the array
@hardCodedAreaScores.push Hash.new
# Access the newly pushed hash by giving it a key, value pair to add
@hardCodedAreaScores[0]["name"] = "area1"
# Do it again
@hardCodedAreaScores[0]["drilldown"] = "area1"
# And again
@hardCodedAreaScores[0]["y"] = 35.15
@hardCodedAreaScores.push Hash.new
@hardCodedAreaScores[1]["name"] = "area2"
@hardCodedAreaScores[1]["drilldown"] = "area2"
@hardCodedAreaScores[1]["y"] = 45.59
@deptBreakdown , deptScores 的转换应该是一个哈希数组。每个哈希应该有3个键:&#34; name&#34;,&#34; id&#34;和&#34; data&#34;。 &#34;名称&#34; =&#34; id&#34; =&#34;钻取&#34;。数据是一个数组数组,用于保存要显示的图表的数据点。例如,&#34;数据&#34; = [[x1,y1],[x2,y2]]。
deptScores的硬编码示例:
@hardCodedDepartmentScores = Array.new
@hardCodedDepartmentScores.push Hash.new
@hardCodedDepartmentScores[0]["name"] = "area1"
@hardCodedDepartmentScores[0]["id"] = "area1"
@hardCodedDepartmentScores[0]["data"] = Array.new
@hardCodedDepartmentScores[0]["data"][0] = Array.new
@hardCodedDepartmentScores[0]["data"][0].push "Department1UnderArea1"
@hardCodedDepartmentScores[0]["data"][0].push 20.15
@hardCodedDepartmentScores[0]["data"].push Array.new
@hardCodedDepartmentScores[0]["data"][1].push "Department2UnderArea1"
@hardCodedDepartmentScores[0]["data"][1].push 69.69
@hardCodedDepartmentScores.push Hash.new
@hardCodedDepartmentScores[1]["name"] = "area2"
@hardCodedDepartmentScores[1]["id"] = "area2"
@hardCodedDepartmentScores[1]["data"] = Array.new
@hardCodedDepartmentScores[1]["data"][0] = Array.new
@hardCodedDepartmentScores[1]["data"][0].push "Department1UnderArea2"
@hardCodedDepartmentScores[1]["data"][0].push 98.21
@hardCodedDepartmentScores[1]["data"].push Array.new
@hardCodedDepartmentScores[1]["data"][1].push "Department2UnderArea2"
@hardCodedDepartmentScores[1]["data"][1].push 12.34
我的尝试:
def cycleThroughProcessedDataAndCreateHighChartsDataSetsBreakdown(areaBreakdown, deptBreakdown, employBreakdown)
areaScores = Array.new
areaScores.push Hash.new
deptScores = Array.new
deptScores.push Hash.new
counter = 0
deptCounter = 0
areaCounter = 0
areaBreakdown.each_key do |area|
areaScores.push Hash.new
areaScores[counter]["name"] = areaBreakdown[area]["name"]
areaScores[counter]["drilldown"] = areaBreakdown[area]["name"]
areaScores[counter]["areaScore"] = areaBreakdown[area]["areaScore"]
deptScores.push Hash.new
deptScores[areaCounter]["name"] = areaBreakdown[area]["name"]
deptScores[areaCounter]["id"] = areaBreakdown[area]["name"]
deptBreakdown.each_key do |dept|
if deptScores[counter]["area_id"] == areaBreakdown[area]["area_id"]
deptScores[counter]["data"] = Array.new
deptScores[counter]["data"][deptCounter] = Array.new
deptScores[counter]["data"][deptCounter].push deptBreakdown[dept]["name"]
deptScores[counter]["data"][deptCounter].push deptBreakdown[dept]["deptScore"]
deptCounter += 1
end
#areaCounter += 1
end
counter += 1
end
debugger
return deptScores, areaScores
end
我的问题是如何将两个初始数据结构(或两个硬编码版本)正确转换为所需的数据结构。我对ruby有点新,我无法访问某些信息,如姓名和身份证。在尝试迭代并填写各种数据结构时,我也遇到了许多空方法错误。必须进行迭代,因为 @areBreakdown 和 @deptBreakdown 的大小是动态的。如何正确填充 @areaScores 和 @deptScores 两个数据结构?
谢谢, 马特
P.S。我为这篇长篇大论道歉。任何帮助表示赞赏。如果问题不清楚,将提供进一步的细节。
当前输出:
(rdb:1) areaScores
[{"name"=>nil, "drilldown"=>nil, "areaScore"=>46.65}, {"name"=>nil, "drilldown"=>nil, "areaScore"=>52.98}]
(rdb:1) deptScores
[{"name"=>nil, "id"=>nil, "data"=>[nil, nil, [nil, 56.15]]}, {"data"=>[nil, nil, nil, nil, nil, [nil, 56.15]]}, {}]
答案 0 :(得分:1)
我终于弄清楚了我的问题。在设置名称和ID时,我没有正确访问哈希。我在下面提供了我的解决方案:
<强>解决方案:强>
areaScores = Array.new
deptScores = Array.new
counter = 0
areaBreakdown.each_key do |area|
deptCounter = 0
areaScores.push Hash.new
areaScores[counter]["name"] = area["name"]
areaScores[counter]["drilldown"] = area["name"]
areaScores[counter]["y"] = areaBreakdown[area]["areaScore"]
deptScores.push Hash.new
deptScores[counter]["name"] = area["name"]
deptScores[counter]["id"] = area["name"]
deptScores[counter]["data"] = Array.new
deptBreakdown.each_key do |dept|
if dept["area_id"] == area["id"] #if department belongs to area
deptScores[counter]["data"][deptCounter] = Array.new
deptScores[counter]["data"][deptCounter].push dept["name"]
deptScores[counter]["data"][deptCounter].push deptBreakdown[dept]["deptScore"]
deptCounter += 1
end #if
end #deptBreakdown
counter += 1
end #areaBreakdown
#debugger
return deptScores, areaScores
感谢您的帮助!