jsontochart 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/jsontochart.rb +116 -0
  2. metadata +47 -0
@@ -0,0 +1,116 @@
1
+ require 'json'
2
+
3
+ class JSONToChart
4
+ def initialize(file)
5
+ @input = file
6
+ end
7
+
8
+ # Gets titles for columns and type of data then formats for table
9
+ def columns
10
+ data = JSON.parse(File.read(@input))
11
+ keylist = Array.new
12
+ columnstring = "\n"
13
+
14
+ data.each do |l|
15
+ dhash = Hash[*l.flatten]
16
+
17
+ dhash.each_key do |key|
18
+ # Check if the key is in the key list and if not add it (and check the type)
19
+ if keylist.include? key
20
+ else
21
+ keylist.push(key)
22
+ if dhash[key].is_a? Integer
23
+ columnstring = columnstring + "data.addColumn('number', '" + key + "');\n"
24
+ elsif dhash[key].is_a? String
25
+ columnstring = columnstring + "data.addColumn('string', '" + key + "');\n"
26
+ elsif dhash[key] == true || dhash[key] == false
27
+ columnstring = columnstring + "data.addColumn('boolean', '" + key + "');\n"
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ return columnstring
34
+ end
35
+
36
+ # Gets a list of all the column titles without formatting
37
+ def columntitles
38
+ data = JSON.parse(File.read(@input))
39
+ keylist = Array.new
40
+
41
+ data.each do |l|
42
+ dhash = Hash[*l.flatten]
43
+ dhash.each_key do |key|
44
+ if keylist.include? key
45
+ else keylist.push(key)
46
+ end
47
+ end
48
+ end
49
+
50
+ return keylist
51
+ end
52
+
53
+ # Converts data in JSON to format for table
54
+ def rows(keylist)
55
+ data = JSON.parse(File.read(@input))
56
+ savestring = "data.addRows([\n"
57
+ j = 0
58
+
59
+ data.each do |l|
60
+ dhash = Hash[*l.flatten]
61
+ tmpstring = "["
62
+ i = 0
63
+ j += 1
64
+ keylist.each do |key|
65
+ i += 1
66
+ # Add data correctly for the type
67
+ if dhash[key] != nil
68
+ if dhash[key].is_a? String
69
+ tmpstring = tmpstring + "'" + dhash[key] + "'"
70
+ elsif dhash[key].is_a? Integer
71
+ tmpstring = tmpstring + dhash[key].to_s
72
+ elsif dhash[key] == true || dhash[key] == false
73
+ tmpstring = tmpstring + dhash[key].to_s
74
+ end
75
+ end
76
+
77
+ # Check if it is the end of the line and append correct characters
78
+ if i == keylist.length then tmpstring = tmpstring + "]"
79
+ else tmpstring = tmpstring + ","
80
+ end
81
+ end
82
+
83
+ # Check if it is the end of the data or just the line and append correct characters
84
+ if j == data.length then savestring = savestring + tmpstring + "\n]);\n"
85
+ else savestring = savestring + tmpstring + ",\n"
86
+ end
87
+ end
88
+
89
+ return savestring
90
+ end
91
+
92
+ # Outputs html for table and calls methods to get column titles and data
93
+ def table
94
+ headerhtml = "<html>
95
+ <head>
96
+ <script type='text/javascript' src='https://www.google.com/jsapi'></script>
97
+ <script type='text/javascript'>
98
+ google.load('visualization', '1', {packages:['table']});
99
+ google.setOnLoadCallback(drawTable);
100
+ function drawTable() {
101
+ var data = new google.visualization.DataTable();"
102
+ footerhtml = "var table = new
103
+ google.visualization.Table(document.getElementById('table_div'));
104
+ table.draw(data, {showRowNumber: true});
105
+ }
106
+ </script>
107
+ </head>
108
+
109
+ <body>
110
+ <div id='table_div'></div>
111
+ </body>
112
+ </html>"
113
+
114
+ return headerhtml + columns + rows(columntitles) + footerhtml
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsontochart
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - M. C. McGrath
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Take JSON files and outputs html for various types of charts
15
+ email: shidash@shidash.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jsontochart.rb
21
+ homepage: https://github.com/Shidash/JSONToChart
22
+ licenses:
23
+ - GPL
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Turns JSONs into charts
46
+ test_files: []
47
+ has_rdoc: