2014-02-09 35 views
0

我使用Rails和有這個種子數據:這個字符串可以正確拆分嗎?

majors = 'business/marketing: 15%|social sciences: 14%|health professions: 11%|english: 10%|engineering: 9%|psychology: 8%|biology: 7%|history: 5%' 

在我看來文件,我有機會獲得@college.majors,然後我把它分解的|,並打印出來。不過,我正試圖將其製作成現在使用Google Charts的條形圖。它需要像這樣被格式化:

var popular_majors = google.visualization.arrayToDataTable([ 
      ['Major', 'Percentage'], 
      ['Psychology', 20], 
      ['Business', 15], 
      ['Engineering', 12], 
      ['Biology', 11], 
      ['Economics', 8], 
      ['History', 6], 
      ['Mathematics', 5] 
     ]); 

是否有可能爲正確分割字符串,然後通過它以這樣一種方式,我可以打印出多名成員的專業迭代,而偶數成員的百分比?

代碼:

var popular_majors = google.visualization.arrayToDataTable(<%= @college.majors.gsub('%', '').split('|').map{ |element| element.split(': ') }.unshift(['Major', 'Percentage']).to_s.html_safe %>) 
      var popular_majors_options = { 
       title: 'Most Popular Majors', 
       legend: 'none', 
       hAxis: {title: 'Percentage'} 
      }; 
      var chart = new google.visualization.BarChart(document.getElementById('popular_majors_chart')); 
      chart.draw(popular_majors, popular_majors_options); 

      var ethnicities = google.visualization.arrayToDataTable(<%= @college.ethnicity.gsub('%', '').split('|').map{ |element| element.split(': ') }.unshift(['Ethnicity', 'Percentage']).to_s.html_safe %>) 
      var ethnicities_options = { 
       title: 'Ethnicities', 
       legend: 'none', 
       hAxis: {title: 'Percentage'} 
      }; 
      var chart = new google.visualization.BarChart(document.getElementById('ethnicities_chart')); 
      chart.draw(ethnicities, ethnicities_options); 
+3

是的,這是可能的。 – Mischa

回答

1

事情是這樣的:

content = "business/marketing: 15%|social sciences: 14%|health professions: 11%|english: 10%|engineering: 9%|psychology: 8%|biology: 7%|history: 5%" 
content.split('|').map{ |element| result = element.split(': '); [result[0], result[1].to_i] }.unshift(['Major', 'Percentage']) 
# => [["Major", "Percentage"], ["business/marketing", 15], ["social sciences", 14], ["health professions", 11], ["english", 10], ["engineering", 9], ["psychology", 8], ["biology", 7], ["history", 5]] 

我錯過了你的觀點有它在JavaScript中的點。您可以通過簡單的結果數組上調用to_s做到這一點:

var popular_majors = google.visualization.arrayToDataTable(<%= @college.majors.split('|').map{ |element| result = element.split(': '); [result[0], result[1].to_i] }.unshift(['Major', 'Percentage']).to_s.html_safe %>) 
+0

「內容」實際上是「@ college.majors」,相當於「商業/營銷:15%|社會科學:14%|醫療職業:11%|英語:10%|工程:9%|心理學: 8%|生物學:7%|歷史:5%'。這會改變什麼嗎? –

+0

@AdamZerner,那就更簡單了。查看更新的答案。看看['Array#map'](http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-map)和['Array#unshift'](http ://www.ruby-doc.org/core-2.1.0/Array.html#method-i-unshift)如果你想了解發生了什麼。 – Mischa

+0

@AdamZerner:更新的答案顯示瞭如何在您的視圖中使用Google Charts的示例。 – Mischa

0

在管道split一次,然後再上的空間。

0
require 'json' 

majors.split('|').collect {|major| major.gsub('%', '').split(':').collect(&:strip) }.unshift(['Major', 'Percentage']).to_json 

你可以做一些其他的小把戲,比如拆分上「%|」和':'來避免gsub和strip,但對我來說這似乎有點脆弱。格式的細微變化會破壞它。