a_la_chart 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -49,37 +49,35 @@ ALC logic is in two parts: controller and view. To activate a controller add the
49
49
 
50
50
  === Data and Mapping
51
51
 
52
- To declare the style of data returned, you call 'chart' with the style of chart (you can have more than one, eg. 'chart :pie, :line'). Within the chart directive, you return the list of data objects (Objects or Hashes), and finally a 'meta', which describes how to map each object to the relevant chart data. For example, we have a Person model, tied to a city name. We want to build a pie chart which counts the people in each city.
52
+ To declare the style of data returned, you call 'chart' with the style of chart (you can have more than one, eg. 'chart :pie, :line'). Within the chart directive, you return the list of 'data' objects (Objects or Hashes). You pass in a Hash which describes how to map each object to the relevant chart data. For example, we have a Person model, tied to a city name. We want to build a pie chart which counts the people in each city.
53
53
 
54
54
  class PeopleController < ApplicationController
55
55
  a_la_chart
56
56
 
57
57
  chart :pie do
58
- data do
58
+ data(:label => :city, :value => :total) do
59
59
  Person.all(:select => 'city, COUNT(people.*) as total', :group => 'city')
60
60
  end
61
- meta :label => :city, :value => :total
62
61
  end
63
62
  end
64
63
 
65
64
  http://chart.apis.google.com/chart?cht=p&chd=t:6,5,3&chs=300x180&chl=Chicago|Indy|New%20York&.jpg
66
65
 
67
- In a pie chart, there are two kinds of required data per slice: A value (how many people live in each city) and a label (what is the name of the city). meta points the two generic requirements (label and value) to specific methods in the returned objects (or key names, if an array of Hash objects are returned). ALC will effectively call "item.city" for each label.
66
+ In a pie chart, there are two kinds of required data per slice: A value (how many people live in each city) and a label (what is the name of the city). The data Hash points the two generic requirements (:label and :value) to specific methods in the returned objects (or key names, if an array of Hash objects are returned). When generating the chart data, ALC will effectively call "item.city" for each label, and "item.total" for each value.
68
67
 
69
- If your query does not return data which maps to a specific method or key, meta can also map requirements to procedure blocks (Proc.new, proc, or lambda) which procedurally extracts values from each item in the returned list of data values.
68
+ If your query does not return data which maps to a specific method or key, you can also map requirements to procedure blocks (Proc.new, proc, or lambda) which procedurally extracts values from each item in the returned list of data values, or integers which point to Array positions.
70
69
 
71
70
  chart :pie do
72
- data do
71
+ data(:label => proc{|item| item[0].blank? ? 'Unknown' : item[0]}, :value => 1) do
73
72
  Person.count(:group => :gender)
74
73
  end
75
- meta :label => proc{|item| item[0].blank? ? 'Unknown' : item[0] }, :value => 1
76
74
  end
77
75
 
78
- ALC will effectively call "item[0].blank? ? 'Unknown' : item[0]" for each label.
76
+ ALC will effectively call "item[0].blank? ? 'Unknown' : item[0]" to create each label, and "item[1]" for each value.
79
77
 
80
78
  === View Chart
81
79
 
82
- Note that we have yet to define the type of charting engine we wish to use. Since that is a view-level decision, we will place that in the view. Before continuing, let us assume you wish to display a report page with a table of people information - alongside the pie chart which displays the count of people per city.
80
+ We have yet to define the type of charting engine we wish to use. Since that is a view-level decision, we will place that in the view. Before continuing, let us assume you wish to display a report page with a table of people information - alongside two pie charts. One which displays the count of people per city, and another which displays their genders.
83
81
 
84
82
  To start, create a "def index" in the PeopleController which returns the list of people, as you normally would.
85
83
 
@@ -87,10 +85,12 @@ To start, create a "def index" in the PeopleController which returns the list of
87
85
  a_la_chart
88
86
 
89
87
  chart :pie do
90
- data do
88
+ data :label => :city, :value => :total do
91
89
  Person.all(:select => 'city, COUNT(people.*) as total', :group => 'city')
92
90
  end
93
- meta :label => :city, :value => :total
91
+ data :gender, :label => proc{|item| item[0].blank? ? 'Unknown' : item[0]}, :value => 1 do
92
+ Person.count(:group => :gender)
93
+ end
94
94
  end
95
95
 
96
96
  def index
@@ -101,6 +101,7 @@ To start, create a "def index" in the PeopleController which returns the list of
101
101
  The corresponding index.html.haml (or index.html.erb, or whatever) will look something like this
102
102
 
103
103
  = chart_tag :google, :pie
104
+ = chart_tag :google, :pie, :case => :gender
104
105
  %table
105
106
  %tr
106
107
  %th Name
@@ -110,7 +111,50 @@ The corresponding index.html.haml (or index.html.erb, or whatever) will look som
110
111
  %td&= person.name
111
112
  %td&= person.city
112
113
 
113
- Except for the chart_tag, it's pretty normal. Here we pass in the minimum required fields for chart_tag: the chart implementation (:google, :fusion, :raphael are the first to be supported, more to come), and the chart type (dependent on the implementation, see a_la_chart/configs/*/config.yml for the lists). Certain configs are supported by all implementations, such as :width and :height.
114
+ Except for chart_tag, it's pretty normal. In the first chart we pass in the minimum required fields for chart_tag: the chart implementation (:google, :fusion, :raphael are the first to be supported, more to come), and the chart type (dependent on the implementation, see a_la_chart/configs/*/config.yml for the lists). The second pie chart is populated with the :gender pie data. ":case" is how we can support several styles of data for a single type of chart.
115
+
116
+ == Multiple Chart Support
117
+
118
+ What if we want to display the city data as a :bar chart? There are two options. The first option, we can move the city data to a separate chart block. You can have as many chart blocks in a single controller as supported types. While we're at it, let's rename the query select names to be 'label' and 'value', to avoid the extra typing of remapping the data.
119
+
120
+ chart :bar do
121
+ data do
122
+ Person.all(:select => 'city as label, COUNT(people.*) as value', :group => 'city')
123
+ end
124
+ end
125
+
126
+ chart :pie do
127
+ data :label => proc{|item| item[0].blank? ? 'Unknown' : item[0]}, :value => 1 do
128
+ Person.count(:group => :gender)
129
+ end
130
+ end
131
+
132
+ Then the view simply requests it.
133
+
134
+ = chart_tag :google, :bar
135
+ = chart_tag :google, :pie
136
+
137
+ http://chart.apis.google.com/chart?chs=320x120&cht=bhs&chd=s:Zxz&chxr=0,0,61&chxt=y,x&chxl=0:|Indy|Chicago|New%20York|&.jpg
138
+
139
+ Now you have two chart types. But suppose you want to show the same data in two ways. Since chart data can sometimes be fundamentally the same, deciding how to display the data is purely a view-level decision. For this Second option, you can declare multiple chart types in a single block. We will merge our charts together again.
140
+
141
+ chart :pie, :bar do
142
+ data do
143
+ Person.all(:select => 'label, COUNT(people.*) as value', :group => 'city')
144
+ end
145
+ data :gender, :label => proc{|item| item[0].blank? ? 'Unknown' : item[0]}, :value => 1 do
146
+ Person.count(:group => :gender)
147
+ end
148
+ end
149
+
150
+ And in the view
151
+
152
+ = chart_tag :google, :bar
153
+ = chart_tag :google, :pie, :case => :gender
154
+
155
+ === More View Chart Config
156
+
157
+ The above example shows the minimum configs required. However, certain configs are supported by all implementations, such as :width and :height.
114
158
 
115
159
  = chart_tag :fusion, :angular, :height => 300, :width => 300, :title => 'My Angular Chart'
116
160
 
@@ -2,6 +2,6 @@ xml.instruct!
2
2
  xml.chart(chart_options.merge(:caption => params[:title])) do
3
3
  the_case = params[:case]
4
4
  data(the_case).each do |record|
5
- xml.set :value => value(record, :value, the_case), :label => value(record, :label, the_case), :color => next_color
5
+ xml.set :value => value(record, :value, the_case), :label => value(record, :label, the_case), :link => value(record, :link, the_case), :color => next_color
6
6
  end
7
7
  end
@@ -2,6 +2,6 @@ xml.instruct!
2
2
  xml.chart(chart_options.merge(:caption => params[:title])) do
3
3
  the_case = params[:case]
4
4
  data(the_case).each do |record|
5
- xml.set :value => value(record, :value, the_case), :label => value(record, :label, the_case), :color => next_color
5
+ xml.set :value => value(record, :value, the_case), :label => value(record, :label, the_case), :link => value(record, :link, the_case), :color => next_color
6
6
  end
7
7
  end
@@ -1,2 +1,2 @@
1
1
  <% set_chart(:pie) %>
2
- <img src="http://chart.apis.google.com/chart?cht=p&chd=t:<%= data.map{|r| value(r, :value) }.join(',') %>&chs=<%= width %>x<%= height %>&chl=<%= data.map{|r| value(r, :label) }.join('|') %>" width="<%= width %>" height="<%= height %>" />
2
+ <img src="http://chart.apis.google.com/chart?cht=p&chd=t:<%= data.map{|r| value(r, :value) }.join(',') %>&chs=<%= width %>x<%= height %>&chl=<%= data.map{|r| value(r, :label) }.join('|') %>&chtt=<%= args[:title] %>" width="<%= width %>" height="<%= height %>" />
data/lib/a_la_chart.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module ALaChart
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
4
4
 
5
5
  require File.join(File.dirname(__FILE__), 'a_la_chart', 'a_la_chart')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a_la_chart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Redmond
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-18 00:00:00 -05:00
12
+ date: 2009-12-23 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 2.3.3
23
+ version: 2.4.0
24
24
  version:
25
25
  description: "'a la Chart' (ALC) is a framework for managing various chart implementations - from grabbing the data, to declaring how those values are mapped to the desired type of chart (pie, line, bar, etc), and finally rendering them. Note: ALC is very much in alpha development and not recommended for public use."
26
26
  email: