active_charts 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93c7d3297b3c8394585c573e9b4cea0a336fb88b
4
+ data.tar.gz: 89c376a47c453c945ad77d6ae14443c57aeadaa4
5
+ SHA512:
6
+ metadata.gz: 58b57ffca3337c294788d2e6b5cbfb155a649c64f8f49d14ec2a18535225e028963c8e2e38a26607638edecf8d8d5ec17edaafda5bb0f91110e137193ba033cd
7
+ data.tar.gz: 9d4a711a3d3856f356a464244c63768f7fd66ed937f595442bf2e856074a239621d15760602f1c341d7cadaee7b26c9cd515cdfa58821845d5592b4c88bd9e0c
@@ -0,0 +1,13 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ SimpleCov.start 'rails' do
2
+ # any custom configs like groups and filters can be here at a central place
3
+ end
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at http://stephsun.com/tickets/active_charts/. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in active_charts.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 saaineui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,186 @@
1
+ # ActiveCharts
2
+
3
+ Beautiful, easy charts for ActiveRecord and Rails.
4
+
5
+ [![View on Travis](https://travis-ci.org/saaineui/active_charts.svg?branch=master)](https://travis-ci.org/saaineui/active_charts)
6
+
7
+ ActiveCharts is a charting toolset that integrates into ActiveRecord and ActionView for fast, easy business intelligence and data visualization on any Rails app.
8
+
9
+ ActiveCharts uses inline SVG wherever possible for simplicity and best performance on the web. Javascript is used only when absolutely necessary.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'active_charts'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Generate the default stylesheet and script files:
24
+
25
+ $ bundle exec active_charts install
26
+
27
+ Follow the command line instructions to include the files in your assets pipeline.
28
+
29
+ ## Usage
30
+
31
+ ActiveCharts autoloads its view helpers into your Rails app. For every chart type available, there are helpers for raw data (array of arrays) and for ActiveRecord collections.
32
+
33
+ ### Chart Gallery
34
+
35
+ You can view sample charts created with the latest version of ActiveCharts at the Chart Gallery:
36
+ - [Chart Gallery](https://necker.herokuapp.com/active_charts "ActiveCharts Chart Gallery")
37
+
38
+ ### [Bar Charts](https://necker.herokuapp.com/active_charts#bar-charts)
39
+
40
+ #### `bar_chart(collection, options = {})`
41
+
42
+ ActiveCharts helpers all support multi-series data by default, so each 'row' of data should be wrapped in an array even if you are creating a single-series chart, e.g. `[[1], [2], [3]]` is a single-series data set with three datapoints.
43
+
44
+ If you do have multiple series, include the columns in the same order in each row. `[[1, 2], [2, 4], [3, 9]]` is a valid multi-series data set for use with the `bar_chart` helper.
45
+
46
+ _Note: ActiveCharts does not currently support negative values in its Bar Charts, but will in the future. Both Line Charts and Scatter Plots support all real numbers._
47
+
48
+ ```ruby
49
+ # controller:
50
+ @collection = [[a1, b1, c1], [a2, b2, c2], ... [a50, b50, c50]]
51
+ @rows = ['Alabama', 'Alaska', ... 'Wyoming']
52
+ @series_labels = ['Median Income', 'Percent White', 'GDP Per Capita']
53
+
54
+ # view:
55
+ <%= bar_chart(@collection, title: 'Census Data by State', rows: @rows, series_labels: @series_labels) %>
56
+ ```
57
+
58
+ #### `bar_chart_for(resource_collection, columns, options = {})`
59
+
60
+ If you have an ActiveRecord model named State with columns named `:median_income`, `:percent_white`, and `:gdp_per_capita` that correspond to the same series as in the above example, you will get a similar chart with the following code in your view. ActiveCharts will automatically use the `:name` column to populate the rows array. Manual methods for setting rows are described below.
61
+
62
+ ```ruby
63
+ # view:
64
+ <%= bar_chart_for(State.all, [:median_income, :percent_white, :gdp_per_capita], title: 'Census Data by State') %>
65
+ ```
66
+
67
+ #### Options
68
+
69
+ All charts have the following options unless indicated otherwise:
70
+
71
+ - `:title` - Specifies a string to include in the `<figcaption>` element as the chart title. Default is “” (i.e. nothing).
72
+ - `:class` - Specifies a string to append to the CSS class for the `<figure>` element which contains the chart title, svg data, and legend. Default is “” (i.e. nothing).
73
+ - `:data_formatters` - Specifies an array of symbols for special formatting of data columns. If not set, all data will be floating point numbers formatted using the `number_with_delimiter` helper.
74
+ - Available formatters are: `:percent`, `:date`, `:currency`, `:rounded`, `:none`.
75
+ - `:max_values` - Specifies an array of max y values for each data column, in column order (e.g. [300000, 1, 300000] might be passed for the above example, if we want Median Income and GDP Per Capita rendered using the same scale). Default will be the largest value in each column, as calculated by ActiveCharts.
76
+ - `:single_y_scale` - Set to true if you want to force a single y scale across all of your columns. (Note: if `:max_values` is set to valid data, it will override this setting.)
77
+ - `:series_labels` - Specifies an array of strings to use in the legend. (Default on basic helpers is []. Default on `_for` helpers is an array of column names.)
78
+ - `:label_height` - Specifies the number of pixels high the chart text labels should be. (Default is 10.)
79
+
80
+ All rectangular charts (Bar Charts, Scatter Plots, and Line Charts) have the following options unless indicated otherwise:
81
+
82
+ - `:height` - Specifies the number of pixels high the chart should be. (Default is 400.)
83
+ - `:rows` - Specifies an array of strings to use to label bars or datapoints. Default is [] (i.e. nothing).
84
+
85
+ Bar Charts have the following additional options:
86
+
87
+ - `:bar_width` - Specifies the number of pixels wide each bar should be. (Default is 40.) Width is automatically calculated from `:bar_width`.
88
+
89
+ ### XY Charts in ActiveCharts: Scatter Plots and Line Charts
90
+
91
+ ActiveCharts supports two types of charts that plot data on an XY grid: Scatter Plots and Line Charts. Each datapoint in these charts is represented by a two-item array, with the x value in the 0 position and the y value in the 1 position: `[x, y]`.
92
+
93
+ Use these charts to plot time-series data or to visualize the relationship between two variables.
94
+
95
+ `[[[1, 1.1]], [[2, 1.9]], [[3, 3.6]]]` is a valid single-series Scatter Plot or Line Chart data set with three datapoints (note the extra array wrapping each row is still required).
96
+
97
+ For multiple series, again include the columns in the same order in each row. `[[[1, 1.1], [3, 5]], [[2, 1.9], [0, 7]]]` is a valid multi-series data set for use with the `scatter_plot` and `line_chart` helpers.
98
+
99
+ ### [Scatter Plots](https://necker.herokuapp.com/active_charts#scatter-plots)
100
+
101
+ In the following examples, we plot `:median_income` vs. `:gdp_per_capita` for each state.
102
+
103
+ #### `scatter_plot(collection, options = {})`
104
+
105
+ ```ruby
106
+ # controller:
107
+ @collection = [[[x1, y1]], [[x2, y2]], ... [[x50, y50]]]
108
+ @rows = ['Alabama', 'Alaska', ... 'Wyoming']
109
+ @series_labels = ['Median Income', 'GDP Per Capita']
110
+
111
+ # view:
112
+ <%= scatter_plot(@collection, title: 'Census Data by State', rows: @rows, series_labels: @series_labels) %>
113
+ ```
114
+
115
+ #### `scatter_plot_for(resource_collection, columns, options = {})`
116
+
117
+ The ActiveRecord model helper `scatter_plot_for` works similarly to `bar_chart_for`. This code will produce a similar chart to the above example.
118
+
119
+ ```ruby
120
+ # view:
121
+ <%= scatter_plot_for(State.all, [:median_income, :gdp_per_capita], title: 'Census Data by State') %>
122
+ ```
123
+
124
+ A big difference to be aware of with `scatter_plot_for` and `line_chart_for` is that these XY helpers only support one column as the source for the x axis data (e.g. _a_ vs. _b_ and _a_ vs. _c_). If you wish to plot _a_ vs. _b_ and _c_ vs. _d_, you will need to use the generic helpers `scatter_plot` and `line_chart`.
125
+
126
+ The first column specified in the `columns` array is the x value for all data points for the row, and the remaining columns will be the y value(s).
127
+
128
+ The following will print each state's median income vs. the % of its population that's white in a series of red dots, and the median income vs. its GDP per capita in a series of orange dots.
129
+
130
+ ```ruby
131
+ # view:
132
+ <%= scatter_plot_for(State.all, [:median_income, :percent_white, :gdp_per_capita], title: 'Census Data by State') %>
133
+ ```
134
+
135
+ #### Options
136
+
137
+ Both XY Charts (Scatter Plots and Line Charts) have the following options unless indicated otherwise:
138
+
139
+ - `:width` - Specifies the number of pixels high the chart should be. (Default is 600.)
140
+
141
+ ### [Line Charts](https://necker.herokuapp.com/active_charts#line-charts)
142
+
143
+ Line Chart helpers work almost exactly the same as Scatter Plot helpers. Please review the previous section for detailed instructions.
144
+
145
+ _Note: The path for each series will be drawn in the given order, so please sort your collection in X order before passing it to `line_chart` or `line_chart_for` or your line will be crooked._
146
+
147
+ #### `line_chart(collection, options = {})`
148
+
149
+ ```ruby
150
+ # controller:
151
+ @collection = [[[x1, y1]], [[x2, y2]], ... [[x50, y50]]]
152
+ @rows = ['Alabama', 'Alaska', ... 'Wyoming']
153
+ @series_labels = ['Median Income', 'GDP Per Capita']
154
+
155
+ # view:
156
+ <%= line_chart(@collection, title: 'Census Data by State', rows: @rows, series_labels: @series_labels) %>
157
+ ```
158
+
159
+ #### `line_chart_for(resource_collection, columns, options = {})`
160
+
161
+ ```ruby
162
+ # view:
163
+ <%= line_chart_for(State.all.order(:name), [:median_income, :gdp_per_capita], title: 'Census Data by State') %>
164
+ ```
165
+
166
+ ## Development
167
+
168
+ After checking out the repo, run `bin/setup` to install dependencies.
169
+
170
+ Then, run the tests:
171
+
172
+ $ bundle exec rspec
173
+ $ bundle exec cucumber
174
+ $ bundle exec rake jasmine
175
+
176
+ ## Contributing
177
+
178
+ Bug reports and pull requests are welcome on GitHub at https://github.com/saaineui/active_charts. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
179
+
180
+ ## License
181
+
182
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
183
+
184
+ ## Code of Conduct
185
+
186
+ Everyone interacting in the ActiveCharts project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/saaineui/active_charts/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ require 'jasmine'
8
+ load 'jasmine/tasks/jasmine.rake'
@@ -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 "active_charts/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_charts"
8
+ spec.version = ActiveCharts::VERSION
9
+ spec.authors = ["saaineui"]
10
+
11
+ spec.summary = %q{Beautiful, easy charts for ActiveRecord and Rails.}
12
+ spec.description = %q{Charting toolset that integrates into ActiveRecord and ActionView for fast, easy business intelligence and data visualization on any Rails app. Uses inline SVG wherever possible for best performance on the web. Javascript is used only when absolutely necessary.}
13
+ spec.homepage = "https://github.com/saaineui/active_charts"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '>= 2.2.2'
24
+
25
+ spec.add_dependency "rails", ">= 5.0.1"
26
+ spec.add_dependency 'sass-rails', '~> 5.0'
27
+ spec.add_dependency "thor"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.15"
30
+ spec.add_development_dependency "rake", "~> 10.5"
31
+ spec.add_development_dependency "rspec", "~> 3.6"
32
+ spec.add_development_dependency "rspec-rails"
33
+ spec.add_development_dependency "cucumber"
34
+ spec.add_development_dependency "aruba"
35
+ spec.add_development_dependency "jasmine"
36
+ spec.add_development_dependency "simplecov"
37
+ end
File without changes
File without changes
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_charts"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'active_charts/cli'
3
+ ActiveCharts::CLI.start
@@ -0,0 +1,27 @@
1
+ require 'rails'
2
+
3
+ module ActiveCharts
4
+ class Engine < ::Rails::Engine
5
+ initializer 'active_charts.initialize' do
6
+ ActiveSupport.on_load(:action_view) do
7
+ include ActiveCharts::Helpers::BarChartHelper
8
+ include ActiveCharts::Helpers::ScatterPlotHelper
9
+ include ActiveCharts::Helpers::LineChartHelper
10
+ end
11
+ end
12
+
13
+ config.eager_load_namespaces << ActiveCharts
14
+ end
15
+
16
+ extend ActiveSupport::Autoload
17
+
18
+ eager_autoload do
19
+ autoload :Helpers
20
+ autoload :Chart
21
+ autoload :RectangularChart
22
+ autoload :XYChart
23
+ autoload :BarChart
24
+ autoload :ScatterPlot
25
+ autoload :LineChart
26
+ end
27
+ end
@@ -0,0 +1,94 @@
1
+ module ActiveCharts
2
+ class BarChart < RectangularChart
3
+ DEFAULT_BAR_WIDTH = 40
4
+
5
+ attr_reader :x_labels, :bar_width, :bars_count, :x_offset, :y_offset,
6
+ :section_width, :max_bar_height, :max_values, :y_multipliers
7
+
8
+ def chart_svg_tag
9
+ inner_html = [grid_rect_tag, bars, bottom_label_text_tags].flatten.join('
10
+ ')
11
+
12
+ tag.svg(inner_html.html_safe, svg_options)
13
+ end
14
+
15
+ def bars
16
+ whitelist = %w[width height x y class]
17
+
18
+ bars_specs.flatten.map do |bar|
19
+ label = formatted_val(bar[:val], bar[:formatter])
20
+
21
+ [%(<rect #{tag_options(bar.merge(width: bar_width), whitelist)} />),
22
+ tag.text(label, label_options(bar))]
23
+ end
24
+ end
25
+
26
+ def bars_specs
27
+ collection.map.with_index do |row, row_index|
28
+ row.map.with_index do |cell, col_index|
29
+ height = bar_height(cell, col_index)
30
+ x = bar_x(col_index, row_index)
31
+ y = grid_height - height
32
+
33
+ { height: height, x: x, y: y, class: bar_classes(col_index), val: cell,
34
+ formatter: data_formatters[col_index] }
35
+ end
36
+ end
37
+ end
38
+
39
+ def bottom_label_text_tags
40
+ x_labels.map.with_index do |label, index|
41
+ tag.text(label, x: section_width * (index + 0.5), y: grid_height + label_height * 1.5)
42
+ end.join
43
+ end
44
+
45
+ private
46
+
47
+ def process_options(options)
48
+ super
49
+
50
+ @bar_width = options[:bar_width] || DEFAULT_BAR_WIDTH
51
+ @x_labels = options[:rows] || []
52
+ end
53
+
54
+ def prereq_calcs
55
+ @bars_count = columns_count * rows_count
56
+ end
57
+
58
+ def width_calcs(_values)
59
+ @grid_width = @svg_width = compute_svg_width
60
+ @section_width = rows_count.zero? ? svg_width : svg_width / rows_count.to_d
61
+ @x_offset = bar_width / 2
62
+ end
63
+
64
+ def compute_svg_width
65
+ (bar_width * bars_count) + (rows_count * MARGIN * (1 + columns_count))
66
+ end
67
+
68
+ def height_calcs(_values)
69
+ @grid_height = svg_height - label_height * 2
70
+ @max_bar_height = grid_height - label_height * 3
71
+ @y_offset = label_height / 2
72
+ @y_multipliers = max_values.map { |max| Util.multiplier(max, max_bar_height) }
73
+ end
74
+
75
+ def label_options(bar)
76
+ {
77
+ x: bar[:x] + x_offset,
78
+ y: bar[:y] - y_offset
79
+ }
80
+ end
81
+
82
+ def bar_classes(col)
83
+ ['ac-bar-chart-bar', series_class(col)].join(' ')
84
+ end
85
+
86
+ def bar_height(cell, col_index)
87
+ Util.safe_to_dec(cell) * y_multipliers[col_index]
88
+ end
89
+
90
+ def bar_x(col, row)
91
+ ((bar_width + MARGIN) * Util.grid_index(columns_count, col, row)) + (MARGIN * (row + 1))
92
+ end
93
+ end
94
+ end