charting_ui 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require charting_ui
3
+ */
@@ -0,0 +1,5 @@
1
+ module ChartingUi
2
+ class ApplicationController < ::ApplicationController #ActionController::Base
3
+ protect_from_forgery
4
+ end
5
+ end
@@ -0,0 +1,124 @@
1
+ require_dependency 'charting_ui/application_controller'
2
+
3
+ module ChartingUi
4
+ class ChartersController < ApplicationController
5
+ def fetch_data
6
+ model = charter_params[:model_name].classify.constantize
7
+ if model == nil then
8
+ respond_to do |format|
9
+ format.json { render :json => { :error => "#{t("charting_ui.Wrong_model")}: '#{model}'"}, :status => 422 }
10
+ end
11
+ return
12
+ end
13
+
14
+ key = JSON.parse(charter_params[:model_key])
15
+ if model.charting_ui_available_methods == nil
16
+ respond_to do |format|
17
+ format.json { render :json => { :error => "#{t('charting_ui.Need_to_define_acts_as_chartable')}: '#{key}'"}, :status => 422 }
18
+ end
19
+ return
20
+ end
21
+ key.each do |k|
22
+ if ! model.charting_ui_available_methods.include?(k)
23
+ respond_to do |format|
24
+ format.json { render :json => { :error => "#{t('charting_ui.Wrong_key')}: '#{k}'"}, :status => 422 }
25
+ end
26
+ return
27
+ end
28
+ end
29
+
30
+ global_result = {}
31
+ model.all.each do |item|
32
+ key.each do |k|
33
+ begin
34
+ key_result = item.send(k)
35
+ if is_date? key_result
36
+ key_result = modify_date(key_result, charter_params[:date_scope])
37
+ else
38
+ key_result = { value: key_result, display: key_result }
39
+ end
40
+
41
+ global_result = create_new_block(key_result, global_result, key)
42
+
43
+ global_result[key_result[:value]][:values][k][:value] += 1
44
+ global_result[key_result[:value]][:values][k][:ids].push(item.id)
45
+ global_result[key_result[:value]][:ids].push(item.id)
46
+ rescue Exception => e
47
+ respond_to do |format|
48
+ format.json { render :json => { :error => "#{t('charting_ui.Unknow_key')}: '#{key}' | Error: #{e}"}, :status => 422 }
49
+ end
50
+ return
51
+ end
52
+ end
53
+ end
54
+
55
+ global_result.delete(2) if global_result.has_key? 2
56
+
57
+ all_integer = true
58
+ global_result.each{|k,v| all_integer = false unless k.is_a? Integer}
59
+
60
+ if all_integer
61
+ tmp = global_result.map{|k,v| v[:key]}
62
+ min_value = tmp.min
63
+ max_value = tmp.max
64
+ min_value.upto(max_value) {|n| create_new_block({value:n , display: n}, global_result, key)}
65
+ end
66
+
67
+ global_result = global_result.map{|k, v| v }
68
+ global_result.each { |item| item[:values] = item[:values].map{|k,v| v} }
69
+ global_result = global_result.sort { |left, right| left[:key] <=> right[:key] }
70
+
71
+ respond_to do |format|
72
+ format.json { render :json => { :success => global_result}, :status => 200 }
73
+ end
74
+ return
75
+ end
76
+
77
+
78
+
79
+ private
80
+ def charter_params
81
+ params.permit(:date_scope, :model_name, :model_key)
82
+ params.require(:model_key)
83
+ params.require(:model_name)
84
+ return params
85
+ end
86
+
87
+ def modify_date(value, scope = "all")
88
+ case scope
89
+ when "year"
90
+ return { value: Time.mktime(value.year), display: "#{value.year}" }
91
+ when "month"
92
+ return { value: Time.mktime(value.year, value.month), display: "#{value.year}-#{value.month}" }
93
+ when "day"
94
+ return { value: Time.mktime(value.year, value.month, value.day), display: "#{value.year}-#{value.month}-#{value.day}" }
95
+ else
96
+ return { value: value, display: "#{value}" }
97
+ end
98
+ end
99
+
100
+ def is_date?(key)
101
+ return key.is_a?(DateTime) || key.is_a?(ActiveSupport::TimeWithZone)
102
+ end
103
+
104
+ def create_new_block(key, result, keys)
105
+ if ! result.has_key? key[:value]
106
+
107
+ type = ""
108
+ if key[:value].is_a? Integer
109
+ type = "integer"
110
+ elsif is_date? key[:value]
111
+ type = "date"
112
+ else
113
+ type = "string"
114
+ end
115
+
116
+ result[key[:value]] = {values: {}, ids: [], key: key[:display], key_type: type }
117
+ keys.each do |kk|
118
+ result[key[:value]][:values][kk] = {value: 0, ids: [], serie: kk}
119
+ end
120
+ end
121
+ return result
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,17 @@
1
+ <div class="charting_ui_area<%= _charting[:bordered] ? ' charting_ui_bordered_area' : '' %>">
2
+ <<%= _charting[:title][:tag] %> class="charting_ui_main_title <%= _charting[:title][:class] %>"><%= _charting[:title][:content] %></<%= _charting[:title][:tag] %>>
3
+
4
+ <div class="charting_ui_canvas_area">
5
+ <div class="charting_ui_info_block">
6
+
7
+ </div>
8
+
9
+ <canvas width="0" height="0" class="charting_ui_chart_interactive"></canvas>
10
+ <canvas width="0" height="0" class="charting_ui_chart_fix"></canvas>
11
+ <div class="charting_ui_data" data-data="<%= _charting[:data].to_json %>" ></div>
12
+
13
+ <script type="text/javascript">
14
+ create_new_canvas($(document.currentScript).parent()[0], <%= browser.device.mobile? ? true : false %>, '<%= _charting[:data][:type] %>')
15
+ </script>
16
+ </div>
17
+ </div>
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'charting_ui/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "charting_ui"
8
+ spec.version = ChartingUi::VERSION
9
+ spec.authors = ["Leo Benkel"]
10
+ spec.email = ["leo.benkel@gmail.com"]
11
+
12
+ spec.summary = "Will draw chart"
13
+ spec.description = "Will draw chart"
14
+ spec.homepage = "https://gitlab.com/wonay/charting_ui"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "byebug", ">= 9.0.4"
26
+ spec.add_development_dependency "sqlite3"
27
+ spec.add_development_dependency "factory_girl_rails"
28
+ spec.add_development_dependency "forgery"
29
+
30
+ spec.required_ruby_version = ">= 2.3.0"
31
+ spec.add_dependency "rails", ">= 4.2.4"
32
+ spec.add_dependency 'jquery-rails', '> 3.0.0'
33
+ end
34
+
35
+
36
+ # Useful links
37
+ # http://flowingdata.com/2010/01/07/11-ways-to-visualize-changes-over-time-a-guide/
@@ -0,0 +1,16 @@
1
+ en:
2
+ charting_ui:
3
+ Wrong_model: "Wrong model"
4
+ Wrong_key: "Wrong key"
5
+ Sum: "Sum"
6
+ Unknow_key: "Unknown key"
7
+ Need_to_define_acts_as_chartable: "Need to define 'acts_as_chartable' in the controller"
8
+
9
+
10
+ fr:
11
+ charting_ui:
12
+ Wrong_model: "Mauvais modele"
13
+ Wrong_key: "Mauvais champ"
14
+ Sum: "Somme"
15
+ Unknow_key: "Champ inconnu"
16
+ Need_to_define_acts_as_chartable: "Besoin de definir 'acts_as_chartable' dans le controller"
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ ChartingUi::Engine.routes.draw do
2
+ post '/fetch_data' => 'charters#fetch_data', as: :fetch_data
3
+ end
@@ -0,0 +1,19 @@
1
+
2
+
3
+ module ChartingUi
4
+ require "charting_ui/engine"
5
+ require "charting_ui/version"
6
+ require "charting_ui/configuration"
7
+ require "charting_ui/view_helpers"
8
+ require "charting_ui/chartable"
9
+
10
+ class << self
11
+ attr_accessor :configuration
12
+ end
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+ def self.configure
17
+ yield(configuration)
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module ChartingUi
2
+ module Chartable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ attr_accessor :charting_ui_available_methods
10
+
11
+ def acts_as_chartable(*queries)
12
+ cattr_accessor :charting_ui_available_methods
13
+ self.charting_ui_available_methods = queries.map(&:to_s)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveRecord::Base.send :include, ChartingUi::Chartable
@@ -0,0 +1,15 @@
1
+ module ChartingUi
2
+ class Configuration
3
+ attr_accessor :default_title_tag, :default_font_size, :default_font_family, :default_offset_left, :default_offset_right, :default_offset_top, :default_offset_bottom
4
+
5
+ def initialize
6
+ @default_title_tag = "h3"
7
+ @default_font_size = 12
8
+ @default_font_family = "Arial"
9
+ @default_offset_left = 50
10
+ @default_offset_right = 20
11
+ @default_offset_top = 30
12
+ @default_offset_bottom = 40
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module ChartingUi
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ChartingUi
4
+
5
+ initializer "charting_ui", before: :load_config_initializers do |app|
6
+ Rails.application.routes.append do
7
+ mount ChartingUi::Engine, at: "/charting_ui"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ChartingUi
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,113 @@
1
+ module ChartingUi
2
+ module ViewHelpers
3
+
4
+ def create_charting_area_hardcoded_scatter(collection, title: ,
5
+ title_tag: ChartingUi.configuration.default_title_tag,
6
+ title_class: "",
7
+ x_dimension: nil,
8
+ y_dimension: nil,
9
+ axe_x_title: ,
10
+ axe_y_title:,
11
+ offset_left: ChartingUi.configuration.default_offset_left,
12
+ offset_right: ChartingUi.configuration.default_offset_right,
13
+ offset_top: ChartingUi.configuration.default_offset_top,
14
+ offset_bottom: ChartingUi.configuration.default_offset_bottom,
15
+ font_family: ChartingUi.configuration.default_font_family,
16
+ font_size: ChartingUi.configuration.default_font_size,
17
+ bordered: true
18
+ )
19
+
20
+ render partial: 'charting_ui/chart_content', locals: {
21
+ _charting: {
22
+ bordered: bordered,
23
+ title:
24
+ {
25
+ tag: title_tag,
26
+ content: title,
27
+ class: title_class
28
+ },
29
+ data: {
30
+ type: "scatter",
31
+ collection: collection,
32
+ offsets: {
33
+ left: offset_left,
34
+ right: offset_right,
35
+ top: offset_top,
36
+ bottom: offset_bottom
37
+ },
38
+ font: {
39
+ family: font_family,
40
+ size: font_size
41
+ },
42
+ axes: {
43
+ x: {
44
+ title: axe_x_title
45
+ },
46
+ y:{
47
+ title: axe_y_title
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ end
54
+
55
+
56
+ def create_charting_model_histogram(
57
+ model_class_name: ,
58
+ name_of_key: ,
59
+ title: ,
60
+ title_tag: ChartingUi.configuration.default_title_tag,
61
+ title_class: "",
62
+ offset_left: ChartingUi.configuration.default_offset_left,
63
+ offset_right: ChartingUi.configuration.default_offset_right,
64
+ offset_top: ChartingUi.configuration.default_offset_top,
65
+ offset_bottom: ChartingUi.configuration.default_offset_bottom,
66
+ font_family: ChartingUi.configuration.default_font_family,
67
+ font_size: ChartingUi.configuration.default_font_size,
68
+ axe_x_title: Array(name_of_key).map{|n| n.capitalize() }.join(", "),
69
+ axe_y_title: t("charting_ui.Sum"),
70
+ bordered: true
71
+ )
72
+
73
+ render partial: 'charting_ui/chart_content', locals: {
74
+ _charting: {
75
+ bordered: bordered,
76
+ title:
77
+ {
78
+ tag: title_tag,
79
+ content: title,
80
+ class: title_class
81
+ },
82
+ data: {
83
+ type: "histogram_model",
84
+ model: {
85
+ name: model_class_name,
86
+ key: Array(name_of_key)
87
+ },
88
+ offsets: {
89
+ left: offset_left,
90
+ right: offset_right,
91
+ top: offset_top,
92
+ bottom: offset_bottom
93
+ },
94
+ font: {
95
+ family: font_family,
96
+ size: font_size
97
+ },
98
+ axes: {
99
+ x: {
100
+ title: axe_x_title
101
+ },
102
+ y:{
103
+ title: axe_y_title
104
+ }
105
+ }
106
+ }
107
+ }
108
+ }
109
+ end
110
+ end
111
+ end
112
+
113
+ ActionView::Base.send :include, ChartingUi::ViewHelpers
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: charting_ui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Leo Benkel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 9.0.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 9.0.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_girl_rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: forgery
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 4.2.4
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 4.2.4
125
+ - !ruby/object:Gem::Dependency
126
+ name: jquery-rails
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">"
130
+ - !ruby/object:Gem::Version
131
+ version: 3.0.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">"
137
+ - !ruby/object:Gem::Version
138
+ version: 3.0.0
139
+ description: Will draw chart
140
+ email:
141
+ - leo.benkel@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".travis.yml"
148
+ - CODE_OF_CONDUCT.md
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - app/assets/javascripts/charting_ui/charting_core.coffee
154
+ - app/assets/javascripts/charting_ui/charting_engine.coffee
155
+ - app/assets/javascripts/charting_ui/charting_histogram.coffee
156
+ - app/assets/javascripts/charting_ui/charting_point.coffee
157
+ - app/assets/javascripts/charting_ui/charting_scatter.coffee
158
+ - app/assets/javascripts/charting_ui/index.js
159
+ - app/assets/stylesheets/charting_ui.scss
160
+ - app/assets/stylesheets/index.scss
161
+ - app/controllers/charting_ui/application_controller.rb
162
+ - app/controllers/charting_ui/charters_controller.rb
163
+ - app/views/charting_ui/_chart_content.html.erb
164
+ - charting_ui.gemspec
165
+ - config/locales/charting_ui.yml
166
+ - config/routes.rb
167
+ - lib/charting_ui.rb
168
+ - lib/charting_ui/chartable.rb
169
+ - lib/charting_ui/configuration.rb
170
+ - lib/charting_ui/engine.rb
171
+ - lib/charting_ui/version.rb
172
+ - lib/charting_ui/view_helpers.rb
173
+ homepage: https://gitlab.com/wonay/charting_ui
174
+ licenses:
175
+ - MIT
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: 2.3.0
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.5.1
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: Will draw chart
197
+ test_files: []