volt-highcharts 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +133 -0
- data/Rakefile +1 -0
- data/app/highcharts/config/dependencies.rb +2 -0
- data/app/highcharts/config/initializers/boot.rb +14 -0
- data/app/highcharts/config/routes.rb +1 -0
- data/app/highcharts/controllers/main_controller.rb +72 -0
- data/app/highcharts/views/main/index.html +2 -0
- data/lib/volt/highcharts/version.rb +5 -0
- data/lib/volt/highcharts.rb +18 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/volt/highcharts_spec.rb +11 -0
- data/volt-highcharts.gemspec +23 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c3d80171d3265558b6ae56c93561728293fbaf8e
|
4
|
+
data.tar.gz: 625467100a4c7cd3aa655cb3674ee42d0a825a64
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e56df08f4f33341d83895f5cb9973393c73079149f4432a95ff8ebe67493e96d9ce6ab0311e2fc3c00f879d8455ca9f914c6e165575cf5f73a20d19825a6f5fd
|
7
|
+
data.tar.gz: c1911fe995e81aef5efc4b1281eb63f44ed5e82295f6c4c51125d0b8b3d9ed4cc1165574a35d290e533132787767576646a22f8319584553124b77bb9fb34191
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 balmoral
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# Volt::Highcharts
|
2
|
+
|
3
|
+
A Volt component wrapping the Highcharts javascript charting tool.
|
4
|
+
|
5
|
+
Highcharts is free for non-commercial use.
|
6
|
+
|
7
|
+
http://www.highcharts.com/products/highcharts
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'volt-highcharts'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install volt-highcharts
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
First include the gem in the project's Gemfile:
|
26
|
+
|
27
|
+
```gem 'volt-highcharts'```
|
28
|
+
|
29
|
+
Next add volt-highcharts to the dependencies.rb file:
|
30
|
+
|
31
|
+
```component 'highcharts'```
|
32
|
+
|
33
|
+
Pass a Ruby hash containing chart options in the appropriate view html file:
|
34
|
+
|
35
|
+
```html
|
36
|
+
<:highcharts chart="{{ chart_options }}" />
|
37
|
+
```
|
38
|
+
|
39
|
+
where `chart_options` is provided by your controller or model. Any object which responds to #to_h may be used, including of course a Volt::Model.
|
40
|
+
|
41
|
+
Documentation for Highcharts options can be found at: http://api.highcharts.com/highcharts#chart.
|
42
|
+
|
43
|
+
At present only a copy of the chart options are passed to Highcharts so the binding will not update the chart automatically.
|
44
|
+
|
45
|
+
For convenience, the last chart added can simply be accessed as ```page._chart```, wrapped by Opal's Native().
|
46
|
+
|
47
|
+
To query or modify multiple chart(s) on the page a unique :id should be set in each chart's options.
|
48
|
+
|
49
|
+
For example:
|
50
|
+
```
|
51
|
+
def fruit_chart_options
|
52
|
+
{
|
53
|
+
# to identity the chart in volt
|
54
|
+
id: 'fruit_chart',
|
55
|
+
|
56
|
+
# highcharts options
|
57
|
+
chart: {
|
58
|
+
type: 'bar'
|
59
|
+
},
|
60
|
+
title: {
|
61
|
+
text: 'Fruit Consumption'
|
62
|
+
},
|
63
|
+
xAxis: {
|
64
|
+
categories: %w(Apples Bananas Oranges)
|
65
|
+
},
|
66
|
+
yAxis: {
|
67
|
+
title: {
|
68
|
+
text: 'Fruit eaten'
|
69
|
+
}
|
70
|
+
},
|
71
|
+
series: [
|
72
|
+
{
|
73
|
+
name: 'Jane',
|
74
|
+
data: [1, 0, 4]
|
75
|
+
},
|
76
|
+
{
|
77
|
+
name: 'John',
|
78
|
+
data: [5, 7, 3]
|
79
|
+
},
|
80
|
+
...
|
81
|
+
]
|
82
|
+
}
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
You can later find the chart in page._charts, the elements of which are Volt::Model's each having an _id and a _chart attribute.
|
87
|
+
|
88
|
+
For example, in your controller you might have a method to return the native chart:
|
89
|
+
```
|
90
|
+
def find_chart(id)
|
91
|
+
# NB use detect, not find
|
92
|
+
e = page._charts.detect { |e| e._id == id }
|
93
|
+
e ? e._chart : nil
|
94
|
+
end
|
95
|
+
```
|
96
|
+
If you only have one chart on the page use ```page._chart```.
|
97
|
+
|
98
|
+
You can dynamically query or modify the chart(s) using Opal's Native() or inline scripting.
|
99
|
+
|
100
|
+
The chart object(s) found in ```page._chart``` and ```page._charts``` have been wrapped in Opal's Native(). This is because Volt::Model and Volt::ArrayModel can only hold Ruby objects.
|
101
|
+
|
102
|
+
Opal's Native() wraps a JS object to provide access to properties and functions in the JS object via Ruby method calls. As of writing (July 30, 2015) Native has not yet been documented. If you prefer to use backticks or %x{} to inline JS code you can get the JS object using #to_n.
|
103
|
+
|
104
|
+
For example, to change a series in a chart using Native(), you might do:
|
105
|
+
```
|
106
|
+
def update_sales
|
107
|
+
e = page._charts.find { |e| e._id == 'sales' }
|
108
|
+
series = Native(e._chart.series)
|
109
|
+
Native(series[0]).setData(sales_data.to_n)
|
110
|
+
end
|
111
|
+
```
|
112
|
+
The equivalent using backticks is:
|
113
|
+
```
|
114
|
+
def update_sales
|
115
|
+
native_chart = page._chart.to_n # get the native JS chart
|
116
|
+
native_data = sales_data.to_n # get native sales data
|
117
|
+
`native_chart.series[0].setData(native_data)`
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
Always use #to_n to convert Ruby data to JS when passing to Highcharts.
|
122
|
+
|
123
|
+
In the future we hope to provide a fully wrapped Ruby implementation of Highcharts.
|
124
|
+
|
125
|
+
## Contributing
|
126
|
+
|
127
|
+
The author is new to things javascript, opal and volt, so contributions and suggestions are very welcome.
|
128
|
+
|
129
|
+
1. Fork it ( http://github.com/balmoral/volt-highcharts/fork )
|
130
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
131
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
132
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
133
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Place any code you want to run when the component is included on the client
|
2
|
+
# or server.
|
3
|
+
|
4
|
+
# To include code only on the client use:
|
5
|
+
# if RUBY_PLATFORM == 'opal'
|
6
|
+
#
|
7
|
+
# To include code only on the server, use:
|
8
|
+
# unless RUBY_PLATFORM == 'opal'
|
9
|
+
# ^^ this will not send compile in code in the conditional to the client.
|
10
|
+
# ^^ this include code required in the conditional.
|
11
|
+
|
12
|
+
if RUBY_PLATFORM == 'opal'
|
13
|
+
require 'native'
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# Component routes
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Highcharts
|
2
|
+
class MainController < Volt::ModelController
|
3
|
+
|
4
|
+
def index_ready
|
5
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : #{Time.now}")
|
6
|
+
|
7
|
+
# Get the containing html element.
|
8
|
+
container = self.container
|
9
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : container='#{container}'")
|
10
|
+
|
11
|
+
opts = attrs.chart ? attrs.chart.to_h : missing_chart
|
12
|
+
|
13
|
+
# If the view hasn't set a container id then
|
14
|
+
# use the id in the chart options if present
|
15
|
+
# otherwise to a random id.
|
16
|
+
id = `$(container).prop("id")`
|
17
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : initial container id is '#{id}'")
|
18
|
+
unless id == nil || id == 'undefined'
|
19
|
+
id = opts[:id] || random_id
|
20
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : char container id is undefined - setting to '#{id}'")
|
21
|
+
`$(container).prop("id", id)`
|
22
|
+
end
|
23
|
+
|
24
|
+
# If :renderTo has been set in the options then it
|
25
|
+
# should match the id of the container, otherwise
|
26
|
+
# a Highcharts error #13 will occur. It should be
|
27
|
+
# unique in the page. We do not check here.
|
28
|
+
#
|
29
|
+
# If :renderTo has not been set in the options then
|
30
|
+
# it will be set here to the container id.
|
31
|
+
unless opts[:chart] && opts[:chart][:renderTo]
|
32
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : setting opts[:chart][:renderTo]='#{id}'")
|
33
|
+
(opts[:chart] ||= {})[:renderTo] = id
|
34
|
+
end
|
35
|
+
|
36
|
+
# Render the chart and add it to the page._charts.
|
37
|
+
# _charts ia an array of Volt::Models with an id and a chart attribute.
|
38
|
+
@id = opts[:id] || id
|
39
|
+
chart = Native(`new Highcharts.Chart( #{opts.to_n} )`) # needs to wrapped for Volt::Model
|
40
|
+
page._charts << Volt::Model.new({id: @id, chart: chart})
|
41
|
+
page._chart = chart # a simple way for later access
|
42
|
+
page._chart_id = id # so we know whether it's me
|
43
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : page._charts='#{page._charts}' page._charts.size=#{page._charts.size}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def before_index_remove
|
47
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} #{Time.now}")
|
48
|
+
# clear all references to this chart
|
49
|
+
i = page._charts.find_index { |e| e._id == @id }
|
50
|
+
if i
|
51
|
+
deleted = page._charts.delete_at(i)
|
52
|
+
Volt.logger.debug("#{self.class.name}##{__method__}:#{__LINE__} : deleted='#{deleted}' page._charts.size=#{page._charts.size}")
|
53
|
+
deleted._chart.destroy
|
54
|
+
deleted._chart = nil
|
55
|
+
end
|
56
|
+
page._chart = nil if page._chart_id == @id
|
57
|
+
@id = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# generate a unique id for chart container
|
63
|
+
def random_id
|
64
|
+
"highcharts#{(rand * 1000000000).to_i}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def missing_chart
|
68
|
+
{ chart: { title: 'no chart attribute set for :highcharts component' } }
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# If you need to require in code in the gem's app folder, keep in mind that
|
2
|
+
# the app is not on the load path when the gem is required. Use
|
3
|
+
# app/{gemname}/config/initializers/boot.rb to require in client or server
|
4
|
+
# code.
|
5
|
+
#
|
6
|
+
# Also, in volt apps, you typically use the lib folder in the
|
7
|
+
# app/{componentname} folder instead of this lib folder. This lib folder is
|
8
|
+
# for setting up gem code when Bundler.require is called. (or the gem is
|
9
|
+
# required.)
|
10
|
+
#
|
11
|
+
# If you need to configure volt in some way, you can add a Volt.configure block
|
12
|
+
# in this file.
|
13
|
+
|
14
|
+
module Volt
|
15
|
+
module Highcharts
|
16
|
+
# Your code goes here...
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Volt sets up rspec and capybara for testing.
|
2
|
+
require 'volt/spec/setup'
|
3
|
+
Volt.spec_setup
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
|
9
|
+
# Run specs in random order to surface order dependencies. If you find an
|
10
|
+
# order dependency and want to debug it, you can fix the order by providing
|
11
|
+
# the seed, which is printed after each run.
|
12
|
+
# --seed 1234
|
13
|
+
config.order = 'random'
|
14
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'volt/highcharts/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "volt-highcharts"
|
8
|
+
spec.version = Volt::Highcharts::VERSION
|
9
|
+
spec.authors = ["Colin Gunn"]
|
10
|
+
spec.email = ["colgunn@icloud.com"]
|
11
|
+
spec.summary = %q{Volt component wrapping Highcharts JavaScript library.}
|
12
|
+
spec.homepage = "https://github.com/balmoral/volt-highcharts"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# spec.add_development_dependency "volt", "~> 0.9.5.pre3"
|
21
|
+
# spec.add_development_dependency 'rspec', '~> 3.2.0'
|
22
|
+
# spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: volt-highcharts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Gunn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- colgunn@icloud.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- app/highcharts/config/dependencies.rb
|
26
|
+
- app/highcharts/config/initializers/boot.rb
|
27
|
+
- app/highcharts/config/routes.rb
|
28
|
+
- app/highcharts/controllers/main_controller.rb
|
29
|
+
- app/highcharts/views/main/index.html
|
30
|
+
- lib/volt/highcharts.rb
|
31
|
+
- lib/volt/highcharts/version.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
- spec/volt/highcharts_spec.rb
|
34
|
+
- volt-highcharts.gemspec
|
35
|
+
homepage: https://github.com/balmoral/volt-highcharts
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.4.6
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Volt component wrapping Highcharts JavaScript library.
|
59
|
+
test_files:
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- spec/volt/highcharts_spec.rb
|
62
|
+
has_rdoc:
|