我使用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);
是的,這是可能的。 – Mischa