plasticine 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +8 -0
  6. data/README.md +18 -0
  7. data/Rakefile +11 -0
  8. data/app/assets/javascripts/plasticine/base.coffee +13 -0
  9. data/app/assets/javascripts/plasticine/column.coffee +243 -0
  10. data/app/assets/javascripts/plasticine/index.js +3 -0
  11. data/app/assets/stylesheets/plasticine/base.scss +1 -0
  12. data/app/assets/stylesheets/plasticine/column.scss +44 -0
  13. data/app/assets/stylesheets/plasticine/index.css +4 -0
  14. data/app/controllers/plasticine_controller.rb +53 -0
  15. data/config/locales/en.yml +26 -0
  16. data/config/locales/fr.yml +26 -0
  17. data/config/routes.rb +3 -0
  18. data/lib/plasticine.rb +23 -0
  19. data/lib/plasticine/authentication.rb +43 -0
  20. data/lib/plasticine/base_visual.rb +11 -0
  21. data/lib/plasticine/builder.rb +43 -0
  22. data/lib/plasticine/builder/base.rb +58 -0
  23. data/lib/plasticine/builder/column.rb +41 -0
  24. data/lib/plasticine/engine.rb +7 -0
  25. data/lib/plasticine/helpers.rb +81 -0
  26. data/lib/plasticine/railtie.rb +15 -0
  27. data/plasticine.gemspec +22 -0
  28. data/spec/controllers/application_controller_spec.rb +18 -0
  29. data/spec/dummy/Rakefile +6 -0
  30. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  31. data/spec/dummy/app/controllers/examples_controller.rb +11 -0
  32. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  33. data/spec/dummy/app/models/anonymou.rb +2 -0
  34. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/spec/dummy/bin/bundle +3 -0
  36. data/spec/dummy/bin/rails +4 -0
  37. data/spec/dummy/bin/rake +4 -0
  38. data/spec/dummy/config.ru +4 -0
  39. data/spec/dummy/config/application.rb +18 -0
  40. data/spec/dummy/config/boot.rb +5 -0
  41. data/spec/dummy/config/database.yml +5 -0
  42. data/spec/dummy/config/environment.rb +5 -0
  43. data/spec/dummy/config/environments/development.rb +29 -0
  44. data/spec/dummy/config/environments/production.rb +80 -0
  45. data/spec/dummy/config/environments/test.rb +36 -0
  46. data/spec/dummy/config/initializers/standard_rails_initializers.rb +9 -0
  47. data/spec/dummy/config/routes.rb +3 -0
  48. data/spec/dummy/db/test.sqlite3 +0 -0
  49. data/spec/plasticine/base_spec.rb +7 -0
  50. data/spec/plasticine_spec.rb +4 -0
  51. data/spec/spec_helper.rb +25 -0
  52. data/spec/support/views_helper.rb +12 -0
  53. data/vendor/assets/javascripts/d3.js +6 -0
  54. metadata +191 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2041b85898391b69adc6c941713bfde0dd46d8a5fa55c95387770c5621f0403c
4
+ data.tar.gz: 4d3d890d23bd43f8c4e94e66c956126571cfc16fc5dfb0794536985ef608361a
5
+ SHA512:
6
+ metadata.gz: e252e948880b7a6ef76fc06ae953f6e34d51f314f246a2e2acd0d9105f10d76b5698c7d4cdff7ea034c3629511def2d49a2579adc879e2d79ceaa3e7202d22e9
7
+ data.tar.gz: 4dbaa2ec0511d9ac82557b9df5a3fd2e36e6b0fc96ddd46f5e671d0dae781a321c43198b41b8c4738cb42f42bc5a5790314c847db0eefdd38980d114a0c0e478
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ .bundle
3
+ .metrics
4
+ .rspec
5
+ Gemfile.lock
6
+ coverage/*
7
+ pkg/*
8
+ spec/dummy/log
9
+ tmp/*
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in plasticine.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Original work Copyright (c) 2012 De Marque inc.
2
+ Modified work Copyright (c) 2015 Alchimik inc.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ Plasticine
2
+ =======
3
+
4
+ [Description]
5
+
6
+ Install
7
+ -------
8
+
9
+ In your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'plasticine'
13
+ ```
14
+
15
+ Copyright
16
+ ---------
17
+
18
+ Copyright (c) 2015 Alchimik. See LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler"
2
+ require "rspec/core/rake_task"
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ desc "Run all specs in spec directory"
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = "spec/**/*_spec.rb"
9
+ end
10
+
11
+ task default: :spec
@@ -0,0 +1,13 @@
1
+ class Plasticine
2
+ constructor: () ->
3
+ @visuals = []
4
+
5
+ if @d3IsLoaded()
6
+ $('div.plasticine').each (i, visual) =>
7
+ switch $(visual).data('visual')
8
+ when 'column' then @visuals.push new PlasticineColumn $(visual)
9
+
10
+ d3IsLoaded: () -> if d3? then true else false
11
+
12
+
13
+ $ -> new Plasticine
@@ -0,0 +1,243 @@
1
+ class @PlasticineColumn
2
+ constructor: (@holder) ->
3
+ @holder = $('.plasticine-column')
4
+
5
+ @setLocale()
6
+ @drawTooltip()
7
+
8
+ width = @holderWidth()
9
+ height = @holder.height()
10
+
11
+ @xScale = d3.scale.ordinal()
12
+ @setXScaleRange()
13
+
14
+ @yScale = d3.scale.linear().range([height,0])
15
+
16
+ @setXAxis()
17
+ @setYAxis()
18
+
19
+ @svg = d3.select("##{@holder.attr('id')}").append('svg').style('width', width).style('height', height).append('g')
20
+
21
+ d3.json @holder.data('url'), (data) => @build(data)
22
+
23
+
24
+ build: (data) ->
25
+ columnIds = data.columns[0].y_values.map (d,i) -> "col#{i+1}"
26
+
27
+ @colGroup = d3.scale.ordinal().range(columnIds).domain(columnIds)
28
+
29
+ @xAxisFormat = data.axis_x_format
30
+ @yAxisFormat = data.axis_y_format
31
+ @yAxisTickCount = data.axis_y_tick_count
32
+ @quarterStartMonth = Number(data.quarter_start_month)
33
+
34
+ data.columns.forEach (d) =>
35
+ y0 = 0
36
+ d.yValues = @colGroup.domain().map (name,i) => { name: name, y0: y0, y1: y0 += @formatValue(d.y_values[i], @yAxisFormat) }
37
+ d.total = d.yValues[d.yValues.length - 1].y1
38
+ d.x = @formatValue(d.x_value, @xAxisFormat)
39
+
40
+ @xScale.domain data.columns.map( (d) -> d.x )
41
+ @yScale.domain [0, d3.max(data.columns, (d) => d.total * @holder.data('y-spacing-ratio'))]
42
+
43
+ @drawYAxis()
44
+
45
+ @columns = @svg.append('g').attr("class", "columns").selectAll('.column').data(data.columns)
46
+ @refreshColumns()
47
+
48
+ @drawXAxis()
49
+
50
+ d3.select(window).on("resize." + @holder.attr("id"), () => @resizeX())
51
+
52
+
53
+ drawTooltip: () -> @holder.append('<div class="tooltip"></div>')
54
+
55
+ drawXAxis: () ->
56
+ height = @holder.height()
57
+ @svg.append('g').attr('class', 'x axis')
58
+ .attr('transform', 'translate(0,' + height + ')')
59
+ .call @xAxis
60
+
61
+ @refreshXAxis()
62
+
63
+
64
+ drawYAxis: () ->
65
+ group = @svg.append("g").attr("class", "y axis").call @yAxis
66
+ group.selectAll("g").filter((d) -> d).classed("minor", true)
67
+
68
+ @refreshYAxis()
69
+
70
+
71
+ formatValue: (value, format) ->
72
+ switch format
73
+ when 'date' then new Date(value)
74
+ else value
75
+
76
+ holderWidth: () -> @holder.width()
77
+
78
+ refreshXAxis: () ->
79
+ @setXAxis()
80
+ @svg.select('.x.axis').call @xAxis
81
+
82
+
83
+ refreshYAxis: () ->
84
+ @setYAxis()
85
+ @svg.select('.y.axis').call @yAxis
86
+
87
+
88
+ setXAxis: () ->
89
+ @xAxis = d3.svg.axis().scale(@xScale).orient('bottom')
90
+ @xAxis.tickSize -@holder.height()
91
+
92
+ switch @xAxisFormat
93
+ when 'date' then @xAxis.tickFormat(d3.time.format('%b'))
94
+ when 'quarter' then @xAxis.tickFormat (d) => @toQuarter(d)
95
+ when 'year' then @xAxis.tickFormat (d) => @toYear(d)
96
+ when 'money' then @xAxis.tickFormat (d) => @toPrice(d)
97
+
98
+
99
+ setYAxis: () ->
100
+ @yAxis = d3.svg.axis().scale(@yScale).orient("left");
101
+ @yAxis.tickSize -@holderWidth()
102
+ @yAxis.ticks @yAxisTickCount
103
+
104
+ switch @yAxisFormat
105
+ when 'date' then @yAxis.tickFormat(d3.time.format('%b'))
106
+ when 'money' then @yAxis.tickFormat (d) => @toPrice(d)
107
+
108
+ hideTooltip: () -> @holder.find('.tooltip').hide()
109
+
110
+ refreshColumns: () ->
111
+ self = this
112
+
113
+ $('g.column').remove()
114
+
115
+ @columns.enter().append('g')
116
+ .attr('class', 'column')
117
+ .attr('data-x-val', (d) -> d.x_value)
118
+ .attr('transform', (d) => 'translate(' + @xScale(d.x) + ',0)')
119
+ .attr('x', (d) => @xScale(d.x))
120
+ .on("mouseover", (d) -> self.showTooltip(@, d))
121
+ .on("mouseout", (d) -> self.hideTooltip(@))
122
+ @columns.exit().remove()
123
+
124
+ @columns.selectAll('rect')
125
+ .data((d) -> d.yValues)
126
+ .enter()
127
+ .append('rect')
128
+ .attr('width', @xScale.rangeBand())
129
+ .attr('y', (d) => @yScale d.y1)
130
+ .attr('height', (d) => @yScale(d.y0) - @yScale(d.y1))
131
+ .attr('class', (d) => @colGroup d.name)
132
+
133
+
134
+ resizeX: () ->
135
+ d3.select(@svg.node().parentNode).style('width', @holderWidth() + 'px')
136
+
137
+ @setXScaleRange()
138
+
139
+ @refreshXAxis()
140
+ @refreshYAxis()
141
+ @hideTooltip()
142
+ @refreshColumns()
143
+
144
+
145
+ setLocale: () ->
146
+ french = d3.locale(
147
+ "decimal": ",",
148
+ "thousands": " ",
149
+ "grouping": [3],
150
+ "currency": ["$", ""],
151
+ "dateTime": "%a %b %e %X %Y",
152
+ "date": "%d-%m-%Y",
153
+ "time": "%H:%M:%S",
154
+ "periods": ["AM", "PM"],
155
+ "days": ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
156
+ "shortDays": ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
157
+ "months": ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"],
158
+ "shortMonths": ["Jan", "Fev", "Mar", "Avr", "Mai", "Jun", "Jul", "Aou", "Sep", "Oct", "Nov", "Dec"]
159
+ )
160
+
161
+ d3.time.format = french.timeFormat
162
+ d3.format = french.numberFormat
163
+
164
+
165
+ setXScaleRange: () ->
166
+ columnsMargin = @holder.data('columns-margin')
167
+ columnsLeftPadding = @holder.data('columns-left-padding')
168
+ columnsRightPadding = @holder.data('columns-right-padding')
169
+ width = @holderWidth()
170
+
171
+ @xScale = @xScale.rangeRoundBands([columnsLeftPadding, width - columnsRightPadding], columnsMargin, 0)
172
+
173
+
174
+ showTooltip: (bar, data) ->
175
+ barNode = $($(bar).find('rect:last-child')[0])
176
+ barX = $(bar).attr('x')
177
+ barY = barNode.attr('y')
178
+
179
+ xPosition = parseInt(barX) + @holder.offset().left
180
+ yPosition = parseInt(barY) + @holder.offset().top
181
+
182
+ tooltip = @holder.find('.tooltip')
183
+
184
+ tooltip.html(data.tooltip)
185
+ tooltip.fadeIn 200
186
+
187
+ tooltipX = xPosition - (tooltip.outerWidth() / 2) + (parseInt(barNode.attr("width")) / 2)
188
+
189
+ d3.select(".tooltip")
190
+ .style("left", tooltipX + "px")
191
+ .style("top", (yPosition - tooltip.outerHeight()) + "px")
192
+
193
+
194
+ toPrice: (amount) ->
195
+ if amount is 0
196
+ price = "0"
197
+ else
198
+ price = String(amount)[0..-3]
199
+ price = "0" if price is ""
200
+
201
+ price = switch price.length
202
+ when 4 then [price.slice(0, 1), ' ', price.slice(1)].join('')
203
+ when 5 then [price.slice(0, 2), ' ', price.slice(2)].join('')
204
+ when 6 then [price.slice(0, 3), ' ', price.slice(3)].join('')
205
+ when 7 then [price.slice(0, 4), ' ', price.slice(4)].join('')
206
+ else price
207
+
208
+ # Show decimals
209
+ #price += ',' + String(amount).slice(-2)
210
+
211
+ price += '$'
212
+
213
+
214
+ toQuarter: (strDate) ->
215
+ date = new Date(strDate + 'T12:00:00')
216
+
217
+ month = date.getMonth() + 1
218
+ monthEquiv = (month + (13 - @quarterStartMonth)) % 12
219
+
220
+ value = switch
221
+ when monthEquiv <= 3 then 'Q1'
222
+ when monthEquiv <= 6 then 'Q2'
223
+ when monthEquiv <= 9 then 'Q3'
224
+ else 'Q4'
225
+
226
+ year = Number(date.getFullYear())
227
+
228
+ if (value is "Q1" and @quarterStartMonth >= 10) or ((value is "Q1" or value is "Q2") and @quarterStartMonth >= 7 and @quarterStartMonth < 10)
229
+ year += 1
230
+
231
+ value += " #{year}"
232
+ value
233
+
234
+
235
+ toYear: (strDate) ->
236
+ date = new Date(strDate + 'T12:00:00')
237
+
238
+ year = Number(date.getFullYear())
239
+
240
+ if @quarterStartMonth >= 7 and (date.getMonth() + 1) >= 7
241
+ year += 1
242
+
243
+ year
@@ -0,0 +1,3 @@
1
+ //= require d3
2
+ //= require ./base
3
+ //= require ./column
@@ -0,0 +1 @@
1
+ .plasticine{border:3px solid #f00;}
@@ -0,0 +1,44 @@
1
+ div.plasticine-column{border:none;clear:both;height:500px;margin:0;padding:0;width:100%;
2
+ svg{overflow:visible;height:100%;width:100%;}
3
+
4
+ path{fill:none;opacity:0;}
5
+
6
+ g.axis{shape-rendering:crispEdges;
7
+ g.tick {
8
+ line{stroke:#efefef;}
9
+ .minor line {stroke:#efefef;}
10
+ text{text-transform:uppercase;fill:#999;font-size:1.4rem;font-weight:500;}
11
+ }
12
+
13
+ &.x g.tick{
14
+ line{opacity:0;}
15
+ text{
16
+ transform: translate(0px,10px);
17
+ -ms-transform: translate(0px,10px);
18
+ -webkit-transform: translate(0px,10px);
19
+ -moz-transform: translate(0px,10px);
20
+ }
21
+ }
22
+
23
+ &.y g.tick{
24
+ text{text-align:right;}
25
+ }
26
+ }
27
+
28
+ g.columns g.column{
29
+ rect{fill:#24b4ca;}
30
+ rect.col1{fill:#45c8dc;}
31
+ rect.col2{fill:#7fd7e5;}
32
+ rect.col3{fill:#a6e5ee;}
33
+ rect.col4{fill:#c2edf5;}
34
+
35
+ }
36
+
37
+ .tooltip{background:#fff;border:1px solid #eee;border-radius:1rem;box-shadow:0px 0px 4px 0px rgba(0,0,0,0.1);display:none;margin-top:-3px;padding:1rem 2rem;position:absolute;
38
+ p{font-size:1.5rem;margin:0;padding:0;}
39
+
40
+ &:after, &:before {border:solid transparent;content:" ";height:0;left:50%;pointer-events:none;position:absolute;top:100%;width:0;}
41
+ &:after {border-color:rgba(255, 255, 255, 0);border-top-color:#ffffff;border-width:12px;margin-left:-12px;}
42
+ &:before {border-color:rgba(238, 238, 238, 0);border-top-color:#eeeeee;border-width:13px;margin-left:-13px;}
43
+ }
44
+ }
@@ -0,0 +1,4 @@
1
+ /*
2
+ *= require ./base
3
+ *= require ./column
4
+ */
@@ -0,0 +1,53 @@
1
+ class PlasticineController < ApplicationController
2
+ before_action :authenticate
3
+
4
+ def show
5
+ if @granted
6
+ # set_default_to if params[:from] and not params[:to]
7
+
8
+ builder = "Plasticine::Builder::#{params[:nature].camelize}".constantize.new(params[:id], params)
9
+
10
+ visual_model.build builder
11
+
12
+ respond_to do |format|
13
+ format.json { render json: builder.to_json }
14
+ end
15
+ else
16
+ respond_to do |format|
17
+ format.json { render json: { 'state' => 'access_refused' } }
18
+ end
19
+ end
20
+ end
21
+
22
+
23
+ private
24
+
25
+ def authenticate
26
+ auth = Plasticine::Authentication.new(request.url, params)
27
+
28
+ @granted = (auth.valid_token? and not auth.expired?)
29
+ end
30
+
31
+ def visual_model
32
+ name = params[:id].underscore.camelize + 'Visual'
33
+
34
+ begin
35
+ name.constantize.new(params)
36
+ rescue
37
+ raise "Plasticine: You must defined #{name}"
38
+ end
39
+ end
40
+
41
+ def set_default_to
42
+ if params[:nature] == 'line'
43
+ params[:to] = case params[:step]
44
+ when 'day' then (Time.now.utc - 1.day).end_of_day.iso8601
45
+ when 'week' then (Time.now.utc - 1.week).end_of_week.iso8601
46
+ when 'month' then (Time.now.utc - 1.month).end_of_month.iso8601
47
+ else Time.now.utc.iso8601
48
+ end
49
+ else
50
+ params[:to] = Time.now.utc.iso8601
51
+ end
52
+ end
53
+ end