pie-high 0.1.0
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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +35 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/lib/high_chart.rb +132 -0
- data/lib/high_chart_series.rb +121 -0
- data/pie-high.gemspec +62 -0
- data/spec/high_chart_series_spec.rb +107 -0
- data/spec/high_chart_spec.rb +80 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +115 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jeff Sutherland
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
= Easy as <b>Pie High</b> Charts
|
2
|
+
|
3
|
+
This gem gives you an easy-as-pie Ruby interface to High Charts.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
I really hate to say this, but at version 0.1.0, the documentation is the code comments. More to follow as I expand
|
7
|
+
this gem.
|
8
|
+
|
9
|
+
=== In the controller
|
10
|
+
<tt>
|
11
|
+
require "high_chart"
|
12
|
+
require "high_chart_series"
|
13
|
+
@data_set = DataSet.find(:all)
|
14
|
+
@series = HighChartSeries.multi_series("name", "data", @data_set)
|
15
|
+
@my_chart = HighChart.new(@series,
|
16
|
+
{:title => "The Best Chart Ever", :x_axis_labels => ["Like", "Dislike", "Indifferent"], :y_axis_title => "Comparison of Apples to Oranges"}))
|
17
|
+
</tt>
|
18
|
+
|
19
|
+
=== In the view
|
20
|
+
|
21
|
+
<tt>
|
22
|
+
<script type="text/javascript">
|
23
|
+
$(document).ready(function() {
|
24
|
+
chart1 = new Highcharts.Chart(
|
25
|
+
<%= @my_chart.to_json %>
|
26
|
+
);
|
27
|
+
});
|
28
|
+
</script>
|
29
|
+
|
30
|
+
<div id="high-chart-container" style="width: 100%; height: 400px"></div>
|
31
|
+
</tt>
|
32
|
+
|
33
|
+
== Copyright
|
34
|
+
|
35
|
+
Copyright (c) 2010 Jeff Sutherland. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "pie-high"
|
8
|
+
gem.summary = %Q{An easy-as-pie Ruby interface to the High Charts JavaScript library}
|
9
|
+
gem.description = %Q{High Charts is one of the best JavaScript charting libraries there is. Here is a straight-forward
|
10
|
+
Ruby API to harness all the power High Charts has to offer. Includes support for Ruport pivot tables.}
|
11
|
+
gem.email = "jefferey.sutherland@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/drpentode/pie-high"
|
13
|
+
gem.authors = ["Jeff Sutherland"]
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
gem.add_development_dependency "ruport", ">=1.6.1"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "pie-high #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/high_chart.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
class HighChart
|
2
|
+
attr_accessor :chart, :credits, :colors, :global, :labels, :lang, :legend, :loading, :plot_options, :point, :series,
|
3
|
+
:subtitle, :symbols, :title, :toolbar, :tooltip, :x_axis, :y_axis, :x_axis_labels, :y_axis_title,
|
4
|
+
:chart_div, :chart_type, :width
|
5
|
+
|
6
|
+
def initialize(series, options={})
|
7
|
+
unless series.kind_of?(Array)
|
8
|
+
raise HighChartError, "series must be an Array"
|
9
|
+
end
|
10
|
+
|
11
|
+
if options[:x_axis_labels] == nil
|
12
|
+
raise HighChartError, "X-Axis labels are required"
|
13
|
+
end
|
14
|
+
|
15
|
+
if options[:colors] != nil && !options[:colors].kind_of?(Array)
|
16
|
+
raise HighChartError, "Colors must be an Array"
|
17
|
+
end
|
18
|
+
|
19
|
+
if options[:config_file]
|
20
|
+
read_defaults(options[:config_file])
|
21
|
+
end
|
22
|
+
|
23
|
+
options.each do |k, v|
|
24
|
+
if self.respond_to?(k.to_sym)
|
25
|
+
merged_value = merge_defaults(k, v)
|
26
|
+
self.instance_variable_set(("@#{k.to_s}").to_sym, merged_value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
@series = series
|
31
|
+
end
|
32
|
+
|
33
|
+
def merge_defaults(key, value)
|
34
|
+
default_value = self.send(key.to_s.to_sym)
|
35
|
+
if default_value.kind_of?(Hash) && value.kind_of?(Hash)
|
36
|
+
default_value.stringify_keys!.merge!(value.stringify_keys!)
|
37
|
+
else
|
38
|
+
default_value = value
|
39
|
+
end
|
40
|
+
|
41
|
+
return default_value
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_defaults(config_file)
|
45
|
+
begin
|
46
|
+
defaults = YAML.load_file(config_file)
|
47
|
+
|
48
|
+
unless defaults == false
|
49
|
+
defaults.each do |k, v|
|
50
|
+
if self.respond_to?(k.to_sym)
|
51
|
+
self.instance_variable_set(("@" + k.to_s).to_sym, v)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
rescue Exception => e
|
56
|
+
raise e
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def chart
|
61
|
+
if @chart_div == nil
|
62
|
+
@chart_div = "high-chart-container"
|
63
|
+
end
|
64
|
+
|
65
|
+
if @chart_type == nil
|
66
|
+
@chart_type = "bar"
|
67
|
+
end
|
68
|
+
|
69
|
+
default_chart = {"renderTo" => @chart_div, "defaultSeriesType" => @chart_type}
|
70
|
+
default_chart.merge!({"width" => @width}) if width.present?
|
71
|
+
default_chart.merge!(@chart) if @chart
|
72
|
+
|
73
|
+
return default_chart
|
74
|
+
end
|
75
|
+
|
76
|
+
def colors
|
77
|
+
if @colors != nil
|
78
|
+
return @colors
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def title
|
83
|
+
return {"text" => @title}
|
84
|
+
end
|
85
|
+
|
86
|
+
def x_axis
|
87
|
+
x_axis_hash = {}
|
88
|
+
x_axis_hash["categories"] = @x_axis_labels
|
89
|
+
x_axis_hash.merge!(@x_axis) if @x_axis
|
90
|
+
return x_axis_hash
|
91
|
+
end
|
92
|
+
|
93
|
+
def y_axis
|
94
|
+
y_axis_hash = {}
|
95
|
+
y_axis_hash["title"] = @y_axis_title
|
96
|
+
y_axis_hash.merge!(@y_axis) if @y_axis
|
97
|
+
return y_axis_hash
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_json
|
101
|
+
json_hash = {
|
102
|
+
:chart => chart,
|
103
|
+
:colors => colors,
|
104
|
+
:credits => credits,
|
105
|
+
:global => global,
|
106
|
+
:labels => labels,
|
107
|
+
:lang => lang,
|
108
|
+
:legend => legend,
|
109
|
+
:loading => loading,
|
110
|
+
:plotOptions => plot_options,
|
111
|
+
:point => point,
|
112
|
+
:subtitle => subtitle,
|
113
|
+
:symbols => symbols,
|
114
|
+
:title => title,
|
115
|
+
:toolbar => toolbar,
|
116
|
+
:tooltip => tooltip,
|
117
|
+
:xAxis => x_axis,
|
118
|
+
:yAxis => y_axis,
|
119
|
+
:series => series,
|
120
|
+
:width => width
|
121
|
+
}
|
122
|
+
|
123
|
+
clean_json_hash = json_hash.reject do |key, value|
|
124
|
+
value == nil
|
125
|
+
end
|
126
|
+
|
127
|
+
return clean_json_hash.to_json
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class HighChartError < StandardError
|
132
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
class HighChartSeries
|
2
|
+
require "high_chart"
|
3
|
+
|
4
|
+
PIE_CHART_TYPE = "pie"
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# builds an array of series from the same data set
|
9
|
+
# series_name_attribute - string of method to call to get series name
|
10
|
+
# attribute_of_interest - method with the numeric data to build the chart
|
11
|
+
# chart_data - array of query results with which to build a series
|
12
|
+
def multi_series(series_name_attribute, attribute_of_interest, chart_data, series_options={})
|
13
|
+
series = []
|
14
|
+
|
15
|
+
for datum in chart_data
|
16
|
+
if datum.kind_of?(Hash)
|
17
|
+
series << self.single_series(datum.fetch(series_name_attribute), attribute_of_interest, datum.fetch(attribute_of_interest))
|
18
|
+
else
|
19
|
+
series << self.single_series(datum.send(series_name_attribute.to_sym), attribute_of_interest, datum.send(attribute_of_interest.to_sym))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
return series
|
24
|
+
end
|
25
|
+
|
26
|
+
# will generate a hash for an individual series
|
27
|
+
# {:name => "My Wonderful Series", :data => [0, 1, 2, 3]
|
28
|
+
# series_options => used to define individual options for series
|
29
|
+
def single_series(series_name, attribute_of_interest, chart_series_data, series_options={})
|
30
|
+
series_data = []
|
31
|
+
|
32
|
+
if chart_series_data.kind_of?(Array)
|
33
|
+
for datum in chart_series_data
|
34
|
+
series_data << datum.send(attribute_of_interest)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
series_data << chart_series_data
|
38
|
+
end
|
39
|
+
|
40
|
+
unless series_options == nil
|
41
|
+
final_series = {:name => series_name, :data => series_data}.merge(series_options)
|
42
|
+
else
|
43
|
+
final_series = {:name => series_name, :data => series_data}
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
return final_series
|
48
|
+
end
|
49
|
+
|
50
|
+
# generates a hash for a pie chart
|
51
|
+
# {:type => 'pie', :name => "MyChart", :data => [ [ "event1", 1], ["event2", 2] ] }
|
52
|
+
def pie_series(series_name, key, value, chart_series_data)
|
53
|
+
final_series = []
|
54
|
+
series_data = []
|
55
|
+
|
56
|
+
for datum in chart_series_data
|
57
|
+
series_data << [datum.fetch(key), datum.fetch(value)]
|
58
|
+
end
|
59
|
+
|
60
|
+
final_series << {:type => PIE_CHART_TYPE, :name => series_name, :data => series_data}
|
61
|
+
|
62
|
+
return final_series
|
63
|
+
end
|
64
|
+
|
65
|
+
# accommodates multi-dimensional pivot tables from Ruport
|
66
|
+
# also generates your x-axis labels
|
67
|
+
def pivot_series(series_name_attribute, attribute_of_interest, chart_data, options={})
|
68
|
+
series = []
|
69
|
+
x_labels = []
|
70
|
+
ignore = nil
|
71
|
+
|
72
|
+
if options && options[:ignore]
|
73
|
+
ignore = options[:ignore]
|
74
|
+
end
|
75
|
+
|
76
|
+
sample_data_hash = chart_data[0].send(attribute_of_interest.to_sym)
|
77
|
+
series_builder = {}
|
78
|
+
|
79
|
+
unless sample_data_hash.is_a?(Hash)
|
80
|
+
raise HighChartError, "Please use the multi_series method. This is not the appropriate chart type for you."
|
81
|
+
end
|
82
|
+
|
83
|
+
for datum in chart_data
|
84
|
+
data_hash = datum.send(attribute_of_interest)
|
85
|
+
|
86
|
+
data_hash.each do |k, v|
|
87
|
+
unless k.to_s == ignore || (ignore && ignore.include?(k.to_s))
|
88
|
+
series_builder[k] ||= []
|
89
|
+
series_builder[k] << v
|
90
|
+
end
|
91
|
+
|
92
|
+
unless x_labels.include?(datum.send(series_name_attribute))
|
93
|
+
x_labels << datum.send(series_name_attribute)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
series_builder.each do |k, v|
|
99
|
+
series << {:name => k, :data => v}
|
100
|
+
end
|
101
|
+
|
102
|
+
return x_labels, series
|
103
|
+
end
|
104
|
+
|
105
|
+
# for building multiple, non-identical series for combo line/bar charts from multiple data sets
|
106
|
+
# series_name_attributes - attributes to get "name" keys from
|
107
|
+
# attributes_of_interest - array of strings naming the attributes to examine
|
108
|
+
# chart_data - the data to build the chart from, an array of arrays, if you will
|
109
|
+
# series_options - array of hashes to be passed to single_series
|
110
|
+
def multi_axis_series(series_name_attributes, attributes_of_interest, chart_data_arr, series_options=[{}, {"type" => "spline"}, {"type" => "spline"}])
|
111
|
+
series = []
|
112
|
+
|
113
|
+
chart_data_arr.each_with_index do |chart_data, index|
|
114
|
+
inner_series = HighChartSeries.single_series(series_name_attributes[index], attributes_of_interest[index], chart_data, series_options[index])
|
115
|
+
series << inner_series
|
116
|
+
end
|
117
|
+
|
118
|
+
return series
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/pie-high.gemspec
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{pie-high}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jeff Sutherland"]
|
12
|
+
s.date = %q{2010-08-03}
|
13
|
+
s.description = %q{High Charts is one of the best JavaScript charting libraries there is. Here is a straight-forward
|
14
|
+
Ruby API to harness all the power High Charts has to offer. Includes support for Ruport pivot tables.}
|
15
|
+
s.email = %q{jefferey.sutherland@gmail.com}
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/high_chart.rb",
|
28
|
+
"lib/high_chart_series.rb",
|
29
|
+
"pie-high.gemspec",
|
30
|
+
"spec/high_chart_series_spec.rb",
|
31
|
+
"spec/high_chart_spec.rb",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/drpentode/pie-high}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{An easy-as-pie Ruby interface to the High Charts JavaScript library}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/high_chart_series_spec.rb",
|
42
|
+
"spec/high_chart_spec.rb",
|
43
|
+
"spec/spec_helper.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
s.add_development_dependency(%q<ruport>, [">= 1.6.1"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<ruport>, [">= 1.6.1"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
s.add_dependency(%q<ruport>, [">= 1.6.1"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require "high_chart"
|
3
|
+
require "high_chart_series"
|
4
|
+
include Ruport::Data
|
5
|
+
|
6
|
+
class ChartData
|
7
|
+
|
8
|
+
def initialize(name="", data=nil)
|
9
|
+
@name = name
|
10
|
+
@data = data
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_accessor :name, :data
|
14
|
+
end
|
15
|
+
|
16
|
+
describe HighChartSeries do
|
17
|
+
|
18
|
+
context "chart data" do
|
19
|
+
before(:each) do
|
20
|
+
@chart_data = []
|
21
|
+
(1..10).each do |i|
|
22
|
+
obj = ChartData.new()
|
23
|
+
obj.name = rand().to_s
|
24
|
+
obj.data = rand().to_s.split(".")[1].slice(0, 3).to_i
|
25
|
+
@chart_data << obj
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should build a single line of series output" do
|
30
|
+
series = HighChartSeries.single_series("my series", "data", @chart_data)
|
31
|
+
series.should have_key(:name)
|
32
|
+
series.should have_key(:data)
|
33
|
+
series.should have_value("my series")
|
34
|
+
series.should have_value(@chart_data.collect { |datum| datum.data })
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should build multiple lines of series output" do
|
38
|
+
series = HighChartSeries.multi_series("name", "data", @chart_data)
|
39
|
+
series.size.should == 10
|
40
|
+
series.first[:data].size.should == 1
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should build a multi-axis series based on multiple discrete data sets" do
|
44
|
+
chart_data_sets = []
|
45
|
+
(1..5).each do |i|
|
46
|
+
chart_data = []
|
47
|
+
(1..10).each do |j|
|
48
|
+
obj = ChartData.new()
|
49
|
+
obj.name = rand().to_s
|
50
|
+
obj.data = rand().to_s.split(".")[1].slice(0, 3).to_i
|
51
|
+
chart_data << obj
|
52
|
+
end
|
53
|
+
chart_data_sets << chart_data
|
54
|
+
end
|
55
|
+
|
56
|
+
series = HighChartSeries.multi_axis_series(["a", "b", "c", "d", "e"], ["data", "data", "data", "data", "data"], chart_data_sets)
|
57
|
+
series.size.should == 5
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "pivot chart data" do
|
62
|
+
|
63
|
+
let(:series) do
|
64
|
+
ChartData.new("Bob", {:bananas => 7, :apples => 13, :ignore_me => 1, :ignore_me_too => 2})
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should build a series from pivot table data" do
|
68
|
+
x_labels, pivot_series = HighChartSeries.pivot_series("name", "data", [series])
|
69
|
+
x_labels.should == [series.name]
|
70
|
+
pivot_series.size.should == 4
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should build a series from pivot table data, ignoring one of the attributes for the series" do
|
74
|
+
x_labels, pivot_series = HighChartSeries.pivot_series("name", "data", [series], :ignore => "ignore_me")
|
75
|
+
x_labels.should == [series.name]
|
76
|
+
pivot_series.size.should == 3
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should build a series from pivot table data, ignoring an array of attributes" do
|
80
|
+
x_labels, pivot_series = HighChartSeries.pivot_series("name", "data", [series], :ignore => ["ignore_me", "ignore_me_too"])
|
81
|
+
x_labels.should == [series.name]
|
82
|
+
pivot_series.size.should == 2
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
context "table data" do
|
88
|
+
let(:table) do
|
89
|
+
Table.new :data => [["Event 1", 2], ["Event 2", 4]], :column_names => %w[ event count ]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should build a multi series from a ruport table" do
|
93
|
+
series = HighChartSeries.multi_series("event", "count", table.data.collect(& :data))
|
94
|
+
series.size.should == 2
|
95
|
+
series.first[:data].first.should eql(2)
|
96
|
+
series.first[:name].should eql("Event 1")
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should build a series for a pie chart" do
|
100
|
+
series = HighChartSeries.pie_series("Event Compare", "event", "count", table.data.collect(& :data))
|
101
|
+
series.first.size.should == 3
|
102
|
+
series.first[:type].should eql("pie")
|
103
|
+
series.first[:name].should eql("Event Compare")
|
104
|
+
series.first[:data].first.first.should eql("Event 1")
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require "high_chart"
|
3
|
+
|
4
|
+
describe HighChart do
|
5
|
+
before(:each) do
|
6
|
+
@series = [{:name => "series1", :data => [0]}, {:name => "series2", :data => [1]}]
|
7
|
+
@x_labels = ["label1", "label2"]
|
8
|
+
@y_title = "a title"
|
9
|
+
@chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :y_axis_title => @y_title, :title => "chart title"})
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should read some defaults from a config file" do
|
13
|
+
chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :y_axis_title => @y_title, :title => "chart title", :config_file => File.dirname(__FILE__) + "/../assets/high_chart_defaults.yml"})
|
14
|
+
chart.legend.should == {"enabled" => true, "layout" => "vertical"}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be OK if there is no config file" do
|
18
|
+
@chart.should_not be(nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should merge options from the hash passed in with the values in the config file" do
|
22
|
+
chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :legend => {:enabled => false}, :config_file => File.dirname(__FILE__) + "/../assets/high_chart_defaults.yml"})
|
23
|
+
chart.legend.should == {"enabled" => false, "layout" => "vertical"}
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should set instance variables from the options hash" do
|
27
|
+
chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :y_axis_title => @y_title, :title => "chart title", :legend => {:enabled => false}, :config_file => File.dirname(__FILE__) + "/../assets/high_chart_defaults.yml"})
|
28
|
+
chart.title.should == {"text" => "chart title"}
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should build a chart" do
|
32
|
+
@chart.to_json.should_not be(nil)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return a colors hash" do
|
36
|
+
chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :y_axis_title => @y_title, :title => "chart title", :colors => ["red", "green", "blue"]})
|
37
|
+
chart.colors.should == ["red", "green", "blue"]
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise an error if colors are not an array" do
|
41
|
+
lambda {HighChart.new(@series, {:colors => "red", :x_axis_labels => @x_labels})}.should raise_error(HighChartError)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return a title hash" do
|
45
|
+
@chart.title.should == {"text" => "chart title"}
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return an xAxis hash" do
|
49
|
+
@chart.x_axis.should == {"categories" => ["label1", "label2"]}
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return a yAxis hash" do
|
53
|
+
@chart.y_axis.should == {"title" => "a title"}
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should fail if no x_axis_labels are provided" do
|
57
|
+
lambda {HighChart.new(@series)}.should raise_error(HighChartError)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should output JSON without null entries" do
|
61
|
+
@chart.to_json.should_not match(/null/)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should read colors from a config file" do
|
65
|
+
chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :y_axis_title => @y_title, :title => "chart title", :config_file => File.dirname(__FILE__) + "/../assets/high_chart_defaults.yml"})
|
66
|
+
chart.colors.should == ['#FFFFFF', '#000000']
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should merge default chart options from a config file" do
|
70
|
+
chart = HighChart.new(@series, {:x_axis_labels => @x_labels, :y_axis_title => @y_title, :title => "chart title", :config_file => File.dirname(__FILE__) + "/../assets/high_chart_defaults.yml"})
|
71
|
+
chart.chart.should == {"defaultSeriesType"=>"bar", "renderTo"=>"high-chart-container", "plotBackgroundColor" => "#000000"}
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should raise an error if series is not an array" do
|
75
|
+
series = {:name => "series1", :data => [0]}
|
76
|
+
x_labels = ["label1", "label2"]
|
77
|
+
y_title = "a title"
|
78
|
+
lambda {HighChart.new(series, {:x_axis_labels => x_labels, :y_axis_title => y_title, :title => "chart title"})}.should raise_error(HighChartError)
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pie-high
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeff Sutherland
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-03 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: ruport
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 13
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 6
|
49
|
+
- 1
|
50
|
+
version: 1.6.1
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: |-
|
54
|
+
High Charts is one of the best JavaScript charting libraries there is. Here is a straight-forward
|
55
|
+
Ruby API to harness all the power High Charts has to offer. Includes support for Ruport pivot tables.
|
56
|
+
email: jefferey.sutherland@gmail.com
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
61
|
+
extra_rdoc_files:
|
62
|
+
- LICENSE
|
63
|
+
- README.rdoc
|
64
|
+
files:
|
65
|
+
- .document
|
66
|
+
- .gitignore
|
67
|
+
- LICENSE
|
68
|
+
- README.rdoc
|
69
|
+
- Rakefile
|
70
|
+
- VERSION
|
71
|
+
- lib/high_chart.rb
|
72
|
+
- lib/high_chart_series.rb
|
73
|
+
- pie-high.gemspec
|
74
|
+
- spec/high_chart_series_spec.rb
|
75
|
+
- spec/high_chart_spec.rb
|
76
|
+
- spec/spec.opts
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: http://github.com/drpentode/pie-high
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --charset=UTF-8
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.3.7
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: An easy-as-pie Ruby interface to the High Charts JavaScript library
|
112
|
+
test_files:
|
113
|
+
- spec/high_chart_series_spec.rb
|
114
|
+
- spec/high_chart_spec.rb
|
115
|
+
- spec/spec_helper.rb
|