Class: JSONToChart
- Inherits:
-
Object
- Object
- JSONToChart
- Defined in:
- lib/jsontochart.rb
Instance Method Summary collapse
-
#columns ⇒ Object
Gets titles for columns and type of data then formats for table.
-
#columntitles ⇒ Object
Gets a list of all the column titles without formatting.
-
#initialize(file, id) ⇒ JSONToChart
constructor
A new instance of JSONToChart.
-
#rows(keylist) ⇒ Object
Converts data in JSON to format for table.
-
#table ⇒ Object
Outputs html for table and calls methods to get column titles and data.
Constructor Details
#initialize(file, id) ⇒ JSONToChart
Returns a new instance of JSONToChart.
4 5 6 7 |
# File 'lib/jsontochart.rb', line 4 def initialize(file, id) @input = file @id = id end |
Instance Method Details
#columns ⇒ Object
Gets titles for columns and type of data then formats for table
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsontochart.rb', line 10 def columns data = JSON.parse(@input) keylist = Array.new columnstring = "\n" data.each do |l| dhash = Hash[*l.flatten] dhash.each_key do |key| # Check if the key is in the key list and if not add it (and check the type) if keylist.include? key else keylist.push(key) if dhash[key].is_a? Integer columnstring = columnstring + "data.addColumn('number', '" + key + "');\n" elsif dhash[key].is_a? String columnstring = columnstring + "data.addColumn('string', '" + key + "');\n" elsif dhash[key] == true || dhash[key] == false columnstring = columnstring + "data.addColumn('boolean', '" + key + "');\n" else columnstring = columnstring + "data.addColumn('string', '" + key + "');\n" end end end end return columnstring end |
#columntitles ⇒ Object
Gets a list of all the column titles without formatting
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jsontochart.rb', line 39 def columntitles data = JSON.parse(@input) keylist = Array.new data.each do |l| dhash = Hash[*l.flatten] dhash.each_key do |key| if keylist.include? key else keylist.push(key) end end end return keylist end |
#rows(keylist) ⇒ Object
Converts data in JSON to format for table
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jsontochart.rb', line 56 def rows(keylist) data = JSON.parse(@input) savestring = "data.addRows([\n" j = 0 data.each do |l| dhash = Hash[*l.flatten] tmpstring = "[" i = 0 j += 1 keylist.each do |key| i += 1 # Add data correctly for the type if dhash[key] != nil if dhash[key].is_a? String tmpstring = tmpstring + "'" + dhash[key].delete("'") + "'" elsif dhash[key].is_a? Integer tmpstring = tmpstring + dhash[key].to_s elsif dhash[key] == true || dhash[key] == false tmpstring = tmpstring + dhash[key].to_s elsif dhash[key].is_a? Array hold = "'" z = 0 dhash[key].each do |i| z += 1 hold = hold + (i.to_s).delete("'") if j < dhash[key].length hold = hold + "," end end hold = hold + "'" tmpstring = tmpstring + hold else tmpstring = tmpstring + "'" + dhash[key].to_s + "'" end else tmpstring = tmpstring + "null" end # Check if it is the end of the line and append correct characters if i == keylist.length then tmpstring = tmpstring + "]" else tmpstring = tmpstring + "," end end # Check if it is the end of the data or just the line and append correct characters if j == data.length then savestring = savestring + tmpstring + "\n]);\n" else savestring = savestring + tmpstring + ",\n" end end return savestring end |
#table ⇒ Object
Outputs html for table and calls methods to get column titles and data
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/jsontochart.rb', line 109 def table headerhtml = "<html> <head> <script type='text/javascript' src='https://www.google.com/jsapi'></script> <script type='text/javascript'> google.load('visualization', '1', {packages:['table']}); google.setOnLoadCallback(drawTable); function drawTable() { var data = new google.visualization.DataTable();" = "var table = new google.visualization.Table(document.getElementById('"+@id.to_s+"table_div')); table.draw(data, {showRowNumber: true}); } </script> </head> <body> <div id='"+@id.to_s+"table_div'></div> </body> </html>" return headerhtml + columns + rows(columntitles) + end |