rubyvis 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/History.txt +9 -1
  2. data/Manifest.txt +9 -4
  3. data/README.txt +59 -8
  4. data/examples/antibiotics/antibiotics.rb +16 -8
  5. data/examples/area.rb +6 -49
  6. data/examples/bar_column_chart.rb +4 -0
  7. data/examples/crimea/{crimea.rb → crimea_data.rb} +0 -0
  8. data/examples/crimea/crimea_grouped_bar.rb +2 -2
  9. data/examples/crimea/crimea_line.rb +1 -1
  10. data/examples/dot.rb +7 -8
  11. data/examples/first.rb +4 -0
  12. data/examples/grouped_charts.rb +3 -0
  13. data/examples/line.rb +19 -61
  14. data/examples/line_and_step.rb +4 -0
  15. data/examples/nested_grid.rb +53 -0
  16. data/examples/scatterplot.rb +2 -0
  17. data/examples/second.rb +14 -2
  18. data/examples/third.rb +9 -6
  19. data/lib/rubyvis/color/color.rb +8 -6
  20. data/lib/rubyvis/color/colors.rb +4 -6
  21. data/lib/rubyvis/format/number.rb +2 -2
  22. data/lib/rubyvis/internals.rb +15 -3
  23. data/lib/rubyvis/javascript_behaviour.rb +20 -8
  24. data/lib/rubyvis/layout/stack.rb +13 -16
  25. data/lib/rubyvis/mark/anchor.rb +64 -2
  26. data/lib/rubyvis/mark/area.rb +4 -3
  27. data/lib/rubyvis/mark/bar.rb +51 -2
  28. data/lib/rubyvis/mark/dot.rb +88 -5
  29. data/lib/rubyvis/mark/label.rb +94 -3
  30. data/lib/rubyvis/mark/line.rb +3 -2
  31. data/lib/rubyvis/mark/panel.rb +1 -0
  32. data/lib/rubyvis/mark/rule.rb +2 -1
  33. data/lib/rubyvis/mark/wedge.rb +2 -1
  34. data/lib/rubyvis/mark.rb +419 -54
  35. data/lib/rubyvis/nest.rb +20 -20
  36. data/lib/rubyvis/scale/linear.rb +1 -1
  37. data/lib/rubyvis/scale/log.rb +1 -1
  38. data/lib/rubyvis/scale/ordinal.rb +120 -8
  39. data/lib/rubyvis/scale/quantitative.rb +159 -15
  40. data/lib/rubyvis/scale.rb +1 -1
  41. data/lib/rubyvis/scene/svg_area.rb +12 -12
  42. data/lib/rubyvis/scene/svg_line.rb +5 -5
  43. data/lib/rubyvis/scene/svg_panel.rb +1 -1
  44. data/lib/rubyvis/scene/svg_scene.rb +1 -1
  45. data/lib/rubyvis.rb +35 -6
  46. data/spec/anchor_spec.rb +15 -15
  47. data/web/Rakefile +34 -0
  48. data/web/build_site.rb +86 -0
  49. data/web/examples.haml +26 -0
  50. data/web/index.haml +97 -0
  51. data/web/style.css +82 -0
  52. data.tar.gz.sig +0 -0
  53. metadata +14 -9
  54. metadata.gz.sig +0 -0
  55. data/lib/rubyvis/label.rb +0 -1
  56. data/web/first.svg +0 -1
  57. data/web/index.html +0 -48
data/History.txt CHANGED
@@ -1,4 +1,12 @@
1
- === 0.1.5 / 2010-10-22
1
+ === 0.1.6 / 2010-10-30
2
+
3
+ * Improved documentation for examples. Added description from protovis site on the one borrowed from it and added commentary on new ones.
4
+ * Distribution includes 'web' directory, with script to recreate the web site. Use as example of massive creation of graphics
5
+ * Added explicitily "none" to svg "fill" property. This allows to render properly with librsv (rsgv), without affecting the browser output
6
+ * Added API documentation on many files.
7
+ * Replaced all pv instances on code for Rubyvis
8
+
9
+ === 0.1.5 / 2010-10-22
2
10
 
3
11
  * Bug fix: styles considered properly, so font attribute works right now.
4
12
  * Bug fix: anchors have proper parent assignation.
data/Manifest.txt CHANGED
@@ -8,7 +8,7 @@ examples/area.rb
8
8
  examples/bar_column_chart.rb
9
9
  examples/barley/barley.rb
10
10
  examples/barley/barley_data.rb
11
- examples/crimea/crimea.rb
11
+ examples/crimea/crimea_data.rb
12
12
  examples/crimea/crimea_grouped_bar.rb
13
13
  examples/crimea/crimea_line.rb
14
14
  examples/dot.rb
@@ -16,6 +16,7 @@ examples/first.rb
16
16
  examples/grouped_charts.rb
17
17
  examples/line.rb
18
18
  examples/line_and_step.rb
19
+ examples/nested_grid.rb
19
20
  examples/pie_and_donut.rb
20
21
  examples/scatterplot.rb
21
22
  examples/second.rb
@@ -30,7 +31,6 @@ lib/rubyvis/format/date.rb
30
31
  lib/rubyvis/format/number.rb
31
32
  lib/rubyvis/internals.rb
32
33
  lib/rubyvis/javascript_behaviour.rb
33
- lib/rubyvis/label.rb
34
34
  lib/rubyvis/layout.rb
35
35
  lib/rubyvis/layout/stack.rb
36
36
  lib/rubyvis/mark.rb
@@ -61,6 +61,7 @@ lib/rubyvis/scene/svg_scene.rb
61
61
  lib/rubyvis/scene/svg_wedge.rb
62
62
  lib/rubyvis/sceneelement.rb
63
63
  lib/rubyvis/transform.rb
64
+ spec/anchor_spec.rb
64
65
  spec/bar_spec.rb
65
66
  spec/internal_spec.rb
66
67
  spec/javascript_behaviour_spec.rb
@@ -70,6 +71,7 @@ spec/nest_spec.rb
70
71
  spec/panel_spec.rb
71
72
  spec/scale_linear_datetime_spec.rb
72
73
  spec/scale_linear_spec.rb
74
+ spec/scale_log_spec.rb
73
75
  spec/scale_ordinal_spec.rb
74
76
  spec/scale_spec.rb
75
77
  spec/spec.opts
@@ -168,5 +170,8 @@ vendor/protovis/src/text/DateFormat.js
168
170
  vendor/protovis/src/text/Format.js
169
171
  vendor/protovis/src/text/NumberFormat.js
170
172
  vendor/protovis/src/text/TimeFormat.js
171
- web/first.svg
172
- web/index.html
173
+ web/Rakefile
174
+ web/build_site.rb
175
+ web/examples.haml
176
+ web/index.haml
177
+ web/style.css
data/README.txt CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Ruby port of Protovis, a great visualization toolkit
7
+ Ruby port of Protovis[http://vis.stanford.edu/protovis/], a great visualization toolkit
8
8
 
9
9
  == FEATURES/PROBLEMS:
10
10
 
@@ -17,25 +17,23 @@ Basic protovis examples[http://vis.stanford.edu/protovis/ex/] works exactly like
17
17
  * Pie & Donut: Interaction with mouse not implemented
18
18
  * Line & Step Charts: Ok
19
19
  * Stacked Charts: Ok
20
- * Grouped Charts: Almost ok. Problems on data stack at third level.
20
+ * Grouped Charts: Ok.
21
21
 
22
22
  Complex examples requires more works:
23
23
 
24
- * antibiotics: Almost ok. Missing center labels
25
- * barley: ok
24
+ * antibiotics: Ok
25
+ * barley: Ok
26
26
  * crimea: line and grouped line ok.
27
27
 
28
28
 
29
- I try to maintain, when posible, complete compatibility with Javascript API, including camel case naming of functions. Johnson [http://github.com/jbarnette/johnson] - the lovely Javascript wrapper inside Ruby embrace - is our friend.
29
+ I try to maintain, when posible, complete compatibility with Javascript API, including camel case naming of functions. Johnson [http://github.com/jbarnette/johnson] - the lovely Javascript wrapper inside Ruby embrace - is our friend to test implementation of basic object.
30
30
 
31
31
  Until version 0.1.0, lambdas should always should created explicitly for method you may be temted to call it with a block.
32
32
 
33
- On a second stage, traditional block calling could be using maintaining backwards compatibily with Javascript API,
33
+ On a second stage, traditional block calling could be using maintaining backwards compatibily with Javascript API. See TODO section for proposal of new API.
34
34
 
35
35
  User could use +pv+ freely, cause is defined as a global method which call Rubyvis.
36
36
 
37
- +self.parent.index+ doesn't work always as expected. +self.index+ works ok. I'm working on it
38
-
39
37
  == CURRENT PROGRESS
40
38
 
41
39
  * pv.js
@@ -83,7 +81,60 @@ User could use +pv+ freely, cause is defined as a global method which call Rubyv
83
81
  puts vis.to_svg
84
82
 
85
83
  See examples directory for original protovis examples adaptations.
84
+
85
+ == TODO
86
+
87
+ Implement a ruby-like API, like ReportBuilder[http://ruby-statsample.rubyforge.org/reportbuilder/] or Prawn [http://prawn.majesticseacreature.com/docs/]
88
+
89
+ The examples/area.rb script should like somethink like that
90
+
91
+ require 'rubyvis'
92
+ vis = Rubyvis::Panel.new {
93
+ width w
94
+ height h
95
+ bottom 20
96
+ left 20
97
+ right 10
98
+ top 5
99
+ # Y-axis
100
+ rule {
101
+ data y.ticks(5)
102
+ bottom y
103
+ stroke_style {|d| d!=0 ? "#eee" : "#000"}
104
+ anchor("left") {
105
+ label {
106
+ text y.tick_format
107
+ }
108
+ }
109
+ }
110
+ # X-axis
111
+ rule {
112
+ data x.ticks
113
+ visible {|d| d!=0}
114
+ left x
115
+ bottom -5
116
+ height -5
117
+ label(:anchor=>'bottom') { # shortcut to create a mark inside an anchor
118
+ text x.tick_format
119
+ }
120
+ }
121
+
122
+ # The area with top line.
123
+ area {
124
+ data(data)
125
+ bottom 1
126
+ left {|d| x.scale(d.x)}
127
+ height {|d| y.scale(d.y)}
128
+ fill_style "rgb(121,173,210)"
129
+ line(:anchor=>"top") {
130
+ line_width 3
131
+ }
132
+ }
133
+ }
86
134
 
135
+ puts vis.to_svg
136
+
137
+
87
138
  == REQUIREMENTS:
88
139
 
89
140
  Ruby 1.8.7 or 1.9.1
@@ -74,20 +74,28 @@ ytick.anchor("left").add(pv.Label)
74
74
  .text_align("center");
75
75
 
76
76
  #/* Dot plot. */
77
- dot = plot.add(pv.Dot)
78
- .data($bacteria)
79
- .stroke_style(lambda {|d| color.scale(d[:gram])})
80
- .fill_style(lambda {|d| color.scale(d[:gram]).alpha(0.2)})
81
- .left(lambda {|d, x, y| z.scale(d[x.to_sym])})
82
- .bottom(lambda {|d, x, y| z.scale(d[y.to_sym])})
83
- .title(lambda {|d| d[:name]});
77
+ dot = plot.add(pv.Dot).
78
+ data($bacteria).
79
+ stroke_style(lambda { |d|
80
+ color.scale(d[:gram])
81
+ }).
82
+ fill_style(lambda {|d|
83
+ color.scale(d[:gram]).alpha(0.2)
84
+ }).
85
+ left(lambda {|d, x, y|
86
+ z.scale(d[x.to_sym])
87
+ }).
88
+ bottom(lambda {|d, x, y|
89
+ z.scale(d[y.to_sym])
90
+ }).
91
+ title(lambda {|d| d[:name]})
84
92
 
85
93
  #/* Legend. */
86
94
  vis.add(pv.Dot)
87
95
  .extend(dot)
88
96
  .data([{gram:"positive"}, {gram:"negative"}])
89
97
  .bottom(-30)
90
- .left(lambda { self.index * 100})
98
+ .left(lambda { index * 100})
91
99
  .title(nil)
92
100
  .anchor("right").add(pv.Label)
93
101
  .text(lambda {|d| "Gram-" + d[:gram]});
data/examples/area.rb CHANGED
@@ -1,8 +1,12 @@
1
+ # = Area Charts
2
+ # This simple area chart is constructed using an area mark, with an added line for emphasis on the top edge. Next, rules and labels are added for reference values.
3
+ # Although this example is basic, it provides a good starting point for adding more complex features. For instance, mouseover interaction can be added to allow precise reading of data values. Or multiple series of data can be added to produce a stacked area chart.
4
+ # Protovis version: http://vis.stanford.edu/protovis/ex/area.html
1
5
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
6
  require 'rubyvis'
3
7
 
4
- data = pv.range(0, 10, 1).map {|x|
5
- OpenStruct.new({:x=> x, :y=> Math.sin(x) + 2})
8
+ data = pv.range(0, 10, 0.1).map {|x|
9
+ OpenStruct.new({:x=> x, :y=> Math.sin(x) + 2+ rand()})
6
10
  }
7
11
 
8
12
 
@@ -54,50 +58,3 @@ vis.render();
54
58
 
55
59
 
56
60
  puts vis.to_svg
57
-
58
- =begin
59
- <svg font-size="10px" font-family="sans-serif" fill="none" stroke="none" stroke-width="1.5" width="430" height="225">
60
- <g transform="translate(20, 5)">
61
- <line shape-rendering="crispEdges" x1="0" y1="200" x2="400" y2="200" stroke="rgb(0,0,0)" stroke-width="1"/>
62
- <line shape-rendering="crispEdges" x1="0" y1="150" x2="400" y2="150" stroke="rgb(238,238,238)" stroke-width="1"/>
63
- <line shape-rendering="crispEdges" x1="0" y1="100" x2="400" y2="100" stroke="rgb(238,238,238)" stroke-width="1"/>
64
- <line shape-rendering="crispEdges" x1="0" y1="50" x2="400" y2="50" stroke="rgb(238,238,238)" stroke-width="1"/>
65
- <line shape-rendering="crispEdges" x1="0" y1="0" x2="400" y2="0" stroke="rgb(238,238,238)" stroke-width="1"/>
66
- </g>
67
- <g transform="translate(20, 5)">
68
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 200)" fill="rgb(0,0,0)" text-anchor="end">0</text>
69
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 150)" fill="rgb(0,0,0)" text-anchor="end">1</text>
70
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 100)" fill="rgb(0,0,0)" text-anchor="end">2</text>
71
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 50)" fill="rgb(0,0,0)" text-anchor="end">3</text>
72
- <text pointer-events="none" x="-3" dy="0.35em" fill="rgb(0,0,0)" text-anchor="end">4</text>
73
- </g>
74
- <g transform="translate(20, 5)">
75
- <line shape-rendering="crispEdges" x1="44.44444444444444" y1="200" x2="44.44444444444444" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
76
- <line shape-rendering="crispEdges" x1="88.88888888888889" y1="200" x2="88.88888888888889" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
77
- <line shape-rendering="crispEdges" x1="133.33333333333331" y1="200" x2="133.33333333333331" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
78
- <line shape-rendering="crispEdges" x1="177.77777777777777" y1="200" x2="177.77777777777777" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
79
- <line shape-rendering="crispEdges" x1="222.22222222222223" y1="200" x2="222.22222222222223" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
80
- <line shape-rendering="crispEdges" x1="266.66666666666663" y1="200" x2="266.66666666666663" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
81
- <line shape-rendering="crispEdges" x1="311.11111111111114" y1="200" x2="311.11111111111114" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
82
- <line shape-rendering="crispEdges" x1="355.55555555555554" y1="200" x2="355.55555555555554" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
83
- <line shape-rendering="crispEdges" x1="400" y1="200" x2="400" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
84
- </g>
85
- <g transform="translate(20, 5)">
86
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(44.4444, 205)" fill="rgb(0,0,0)" text-anchor="middle">1</text>
87
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(88.8889, 205)" fill="rgb(0,0,0)" text-anchor="middle">2</text>
88
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(133.333, 205)" fill="rgb(0,0,0)" text-anchor="middle">3</text>
89
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(177.778, 205)" fill="rgb(0,0,0)" text-anchor="middle">4</text>
90
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(222.222, 205)" fill="rgb(0,0,0)" text-anchor="middle">5</text>
91
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(266.667, 205)" fill="rgb(0,0,0)" text-anchor="middle">6</text>
92
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(311.111, 205)" fill="rgb(0,0,0)" text-anchor="middle">7</text>
93
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(355.556, 205)" fill="rgb(0,0,0)" text-anchor="middle">8</text>
94
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(400, 205)" fill="rgb(0,0,0)" text-anchor="middle">9</text>
95
- </g>
96
- <g transform="translate(20, 5)">
97
- <path d="M0,99L44.44444444444444,56.92645075960516L88.88888888888889,53.535128658715905L133.33333333333331,91.94399959700664L177.77777777777777,136.8401247653964L222.22222222222223,146.94621373315692L266.66666666666663,112.9707749099463L311.11111111111114,66.15067006406053L355.55555555555554,49.53208766883091L400,78.39407573791217L400,199L355.55555555555554,199L311.11111111111114,199L266.66666666666663,199L222.22222222222223,199L177.77777777777777,199L133.33333333333331,199L88.88888888888889,199L44.44444444444444,199L0,199Z" fill="rgb(121,173,210)"/>
98
- </g>
99
- <g transform="translate(20, 5)">
100
- <path d="M0,99L44.44444444444444,56.92645075960516L88.88888888888889,53.535128658715905L133.33333333333331,91.94399959700664L177.77777777777777,136.8401247653964L222.22222222222223,146.94621373315692L266.66666666666663,112.9707749099463L311.11111111111114,66.15067006406053L355.55555555555554,49.53208766883091L400,78.39407573791217" stroke="rgb(31,119,180)" stroke-width="3"/>
101
- </g>
102
- </svg>
103
- =end
@@ -1,3 +1,7 @@
1
+ # = Bar & Column Charts
2
+ # This simple bar chart is constructed using a bar mark. A linear scale is used to compute the width of the bar, while an ordinal scale sets the top position and height for the categorical dimension. Next, rules and labels are added for reference values.
3
+ # Bars can be used in a variety of ways. For instance, they can be stacked or grouped to show multiple data series, or arranged as vertical columns rather than bars.
4
+
1
5
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
6
  require 'rubyvis'
3
7
 
File without changes
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.dirname(__FILE__)+"/../../lib")
2
2
  require 'rubyvis'
3
3
  require 'ostruct'
4
- load(File.dirname(__FILE__)+"/crimea.rb")
4
+ load(File.dirname(__FILE__)+"/crimea_data.rb")
5
5
 
6
6
 
7
7
  w = 545
@@ -45,7 +45,7 @@ panel.anchor("bottom").add(pv.Label)
45
45
  .text_margin(10)
46
46
  .text(lambda {|d| format.format(d.date)})
47
47
 
48
-
48
+
49
49
  vis.add(pv.Rule)
50
50
  .data(y.ticks())
51
51
  .bottom(lambda {|d| y.scale(d)})
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.dirname(__FILE__)+"/../../lib")
2
2
  require 'rubyvis'
3
3
  require 'ostruct'
4
- load(File.dirname(__FILE__)+"/crimea.rb")
4
+ load(File.dirname(__FILE__)+"/crimea_data.rb")
5
5
 
6
6
 
7
7
  w = 545
data/examples/dot.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # = Dot and anchors
2
+ # This example shows how looks differents positions of anchors on dots
1
3
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
4
  require 'rubyvis'
3
5
 
@@ -5,15 +7,12 @@ vis = pv.Panel.new().width(200).height(200);
5
7
 
6
8
  dot=vis.add(pv.Dot)
7
9
  .data([1,2,3,4,5,6])
8
- .bottom(lambda {|d| return d*30})
9
- .left(lambda { return 20+self.index*20} )
10
+ .bottom(lambda {|d| d*30})
11
+ .left(lambda { 20+index*40} )
10
12
  .shape_radius(10)
11
- dot.anchor('top').add(pv.Label).text('a')
12
- dot.anchor('bottom').add(pv.Label).text('b')
13
- dot.anchor('left').add(pv.Label).text('l')
14
- dot.anchor('right').add(pv.Label).text('r')
15
-
16
-
13
+ %w{top bottom left right center}.each do |dir|
14
+ dot.anchor(dir).add(pv.Label).text(dir[0,1])
15
+ end
17
16
  vis.render()
18
17
  #puts vis.children_inspect
19
18
  puts vis.to_svg
data/examples/first.rb CHANGED
@@ -1,3 +1,7 @@
1
+ # = First example
2
+ # This is the ruby version of "Getting Started" example of Protovis introduction.
3
+ # On this example we show you how can you build a bar chart using panel and bar marks.
4
+ # A mark represents a set of graphical elements that share data and visual encodings. Although marks are simple by themselves, you can combine them in interesting ways to make rich, interactive visualizations
1
5
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
6
  require 'rubyvis'
3
7
  vis = Rubyvis::Panel.new.width(150).height(150);
@@ -1,3 +1,6 @@
1
+ # = Grouped charts
2
+ # In this multi-series bar chart, we group bars together rather than stack them. A grouped chart allows accurate comparison of individual values thanks to an aligned baseline: a position, rather than length, judgment is used.
3
+ # An ordinal scale positions the groups vertically; the bars are then replicated inside a panel, a technique that is also used for small multiples.
1
4
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
5
  require 'rubyvis'
3
6
 
data/examples/line.rb CHANGED
@@ -1,8 +1,11 @@
1
+ # = Line Chart
2
+ # This line chart is constructed a Line mark.
3
+ # The second line, inside the first one, is created using an anchor.
1
4
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
5
  require 'rubyvis'
3
6
 
4
- data = pv.range(0, 10, 1).map {|x|
5
- OpenStruct.new({:x=> x, :y=> Math.sin(x) + 2})
7
+ data = pv.range(0, 10, 0.1).map {|x|
8
+ OpenStruct.new({:x=> x, :y=> Math.sin(x) + 2+rand()})
6
9
  }
7
10
 
8
11
  #p data
@@ -15,70 +18,25 @@ y = pv.Scale.linear(data, lambda {|d| d.y}).range(0, h);
15
18
 
16
19
  #/* The root panel. */
17
20
  vis = pv.Panel.new()
18
- .width(w)
19
- .height(h)
20
- .bottom(20)
21
- .left(20)
22
- .right(10)
23
- .top(5)
21
+ .width(w)
22
+ .height(h)
23
+ .bottom(20)
24
+ .left(20)
25
+ .right(10)
26
+ .top(5)
24
27
 
25
28
  #/* The area with top line. */
26
- vis.add(pv.Line)
27
- .data(data)
28
- .line_width(5)
29
- .left(lambda {|d| x.scale(d.x)})
30
- .bottom(lambda {|d| y.scale(d.y)}).anchor("bottom").add(pv.Line)
31
- .stroke_style('red').line_width(1)
29
+ vis.add(pv.Line).
30
+ data(data).
31
+ line_width(5).
32
+ left(lambda {|d| x.scale(d.x)}).
33
+ bottom(lambda {|d| y.scale(d.y)}).
34
+ anchor("bottom").add(pv.Line).
35
+ stroke_style('red').
36
+ line_width(1)
32
37
 
33
38
 
34
39
  vis.render();
35
40
 
36
41
 
37
42
  puts vis.to_svg
38
-
39
- =begin
40
- <svg font-size="10px" font-family="sans-serif" fill="none" stroke="none" stroke-width="1.5" width="430" height="225">
41
- <g transform="translate(20, 5)">
42
- <line shape-rendering="crispEdges" x1="0" y1="200" x2="400" y2="200" stroke="rgb(0,0,0)" stroke-width="1"/>
43
- <line shape-rendering="crispEdges" x1="0" y1="150" x2="400" y2="150" stroke="rgb(238,238,238)" stroke-width="1"/>
44
- <line shape-rendering="crispEdges" x1="0" y1="100" x2="400" y2="100" stroke="rgb(238,238,238)" stroke-width="1"/>
45
- <line shape-rendering="crispEdges" x1="0" y1="50" x2="400" y2="50" stroke="rgb(238,238,238)" stroke-width="1"/>
46
- <line shape-rendering="crispEdges" x1="0" y1="0" x2="400" y2="0" stroke="rgb(238,238,238)" stroke-width="1"/>
47
- </g>
48
- <g transform="translate(20, 5)">
49
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 200)" fill="rgb(0,0,0)" text-anchor="end">0</text>
50
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 150)" fill="rgb(0,0,0)" text-anchor="end">1</text>
51
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 100)" fill="rgb(0,0,0)" text-anchor="end">2</text>
52
- <text pointer-events="none" x="-3" dy="0.35em" transform="translate(0, 50)" fill="rgb(0,0,0)" text-anchor="end">3</text>
53
- <text pointer-events="none" x="-3" dy="0.35em" fill="rgb(0,0,0)" text-anchor="end">4</text>
54
- </g>
55
- <g transform="translate(20, 5)">
56
- <line shape-rendering="crispEdges" x1="44.44444444444444" y1="200" x2="44.44444444444444" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
57
- <line shape-rendering="crispEdges" x1="88.88888888888889" y1="200" x2="88.88888888888889" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
58
- <line shape-rendering="crispEdges" x1="133.33333333333331" y1="200" x2="133.33333333333331" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
59
- <line shape-rendering="crispEdges" x1="177.77777777777777" y1="200" x2="177.77777777777777" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
60
- <line shape-rendering="crispEdges" x1="222.22222222222223" y1="200" x2="222.22222222222223" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
61
- <line shape-rendering="crispEdges" x1="266.66666666666663" y1="200" x2="266.66666666666663" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
62
- <line shape-rendering="crispEdges" x1="311.11111111111114" y1="200" x2="311.11111111111114" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
63
- <line shape-rendering="crispEdges" x1="355.55555555555554" y1="200" x2="355.55555555555554" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
64
- <line shape-rendering="crispEdges" x1="400" y1="200" x2="400" y2="205" stroke="rgb(0,0,0)" stroke-width="1"/>
65
- </g>
66
- <g transform="translate(20, 5)">
67
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(44.4444, 205)" fill="rgb(0,0,0)" text-anchor="middle">1</text>
68
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(88.8889, 205)" fill="rgb(0,0,0)" text-anchor="middle">2</text>
69
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(133.333, 205)" fill="rgb(0,0,0)" text-anchor="middle">3</text>
70
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(177.778, 205)" fill="rgb(0,0,0)" text-anchor="middle">4</text>
71
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(222.222, 205)" fill="rgb(0,0,0)" text-anchor="middle">5</text>
72
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(266.667, 205)" fill="rgb(0,0,0)" text-anchor="middle">6</text>
73
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(311.111, 205)" fill="rgb(0,0,0)" text-anchor="middle">7</text>
74
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(355.556, 205)" fill="rgb(0,0,0)" text-anchor="middle">8</text>
75
- <text pointer-events="none" y="3" dy="0.71em" transform="translate(400, 205)" fill="rgb(0,0,0)" text-anchor="middle">9</text>
76
- </g>
77
- <g transform="translate(20, 5)">
78
- <path d="M0,99L44.44444444444444,56.92645075960516L88.88888888888889,53.535128658715905L133.33333333333331,91.94399959700664L177.77777777777777,136.8401247653964L222.22222222222223,146.94621373315692L266.66666666666663,112.9707749099463L311.11111111111114,66.15067006406053L355.55555555555554,49.53208766883091L400,78.39407573791217L400,199L355.55555555555554,199L311.11111111111114,199L266.66666666666663,199L222.22222222222223,199L177.77777777777777,199L133.33333333333331,199L88.88888888888889,199L44.44444444444444,199L0,199Z" fill="rgb(121,173,210)"/>
79
- </g>
80
- <g transform="translate(20, 5)">
81
- <path d="M0,99L44.44444444444444,56.92645075960516L88.88888888888889,53.535128658715905L133.33333333333331,91.94399959700664L177.77777777777777,136.8401247653964L222.22222222222223,146.94621373315692L266.66666666666663,112.9707749099463L311.11111111111114,66.15067006406053L355.55555555555554,49.53208766883091L400,78.39407573791217" stroke="rgb(31,119,180)" stroke-width="3"/>
82
- </g>
83
- </svg>
84
- =end
@@ -1,3 +1,7 @@
1
+ # = Line and Step Chart
2
+ # Line charts are often used to visualize time series data, encoding the value of a variable over time using position. Typically, linear interpolation is used between samples. However, in some cases, the data does not vary smoothly, but instead changes in response to discrete events.
3
+ # Using the interpolate property, it is easy to change this behavior for lines and areas. Rubyvis also supports various nonlinear interpolation methods, including cardinal spline, Catmull-Rom spline, B-spline, and monotone cubic.
4
+
1
5
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
6
  require 'rubyvis'
3
7
 
@@ -0,0 +1,53 @@
1
+ # = Nested grid
2
+ # Two level nested grid.
3
+ # The first is created at random with n rows and m columns
4
+ # The second level is a 9x9 grid inside the cell of first level
5
+ $:.unshift(File.dirname(__FILE__)+"/../lib")
6
+ require 'rubyvis'
7
+
8
+
9
+ w = 600
10
+ h = 600
11
+ cs=pv.Colors.category20()
12
+ rows=2.0+rand(3)
13
+ cols=2.0+rand(3)
14
+ row_h=h/rows
15
+ col_w=w/cols
16
+ cel_h=row_h/3.0
17
+ cel_w=col_w/3.0
18
+
19
+ letters=%w{a b c d e f g h i j k}
20
+
21
+
22
+ vis = pv.Panel.new()
23
+ .width(w)
24
+ .height(h);
25
+
26
+ p1=vis.add(pv.Panel).data(letters[0,rows]).
27
+ top(lambda {index*(row_h)}).
28
+ height(row_h)
29
+
30
+ p2=p1.add(pv.Panel).data(letters[0,cols]).
31
+ width(col_w).
32
+ left(lambda {index*(col_w)})
33
+
34
+ p2.anchor("center").add(pv.Label).
35
+ text(lambda {|d,a,b| return "#{b}-#{a}"}).
36
+ font("bold large Arial")
37
+
38
+
39
+ p2.add(pv.Bar).data([1,2,3,4,5,6,7,8,9]).
40
+ width(-3+cel_w).
41
+ height(-3+cel_h).
42
+ visible(lambda {|d| d!=5}).
43
+ fillStyle(lambda {|d| cs.scale(d)}).
44
+ top(lambda { (index / 3.0).floor*cel_h}).
45
+ left(lambda { (index % 3)*cel_w}).
46
+ anchor('center').
47
+ add(pv.Label).
48
+ visible(lambda {|a,b,c| a!=5}).
49
+ text(lambda {|a,b,c| "#{c}-#{b}-#{a}"})
50
+
51
+ vis.render();
52
+
53
+ puts vis.to_svg
@@ -1,3 +1,5 @@
1
+ # = Scatterplot
2
+ # Scatterplots can facilitate visual analysis along multiple dimensions, though care should be taken to avoid interference. In this example, we encode three dimensions: two are encoded using position, while the third is redundantly encoded as both area and color.
1
3
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
4
  require 'rubyvis'
3
5
  data = pv.range(100).map {|x|
data/examples/second.rb CHANGED
@@ -1,3 +1,6 @@
1
+ # = Inheritance and labels
2
+ # Second example of Protovis "Getting Started section"
3
+ # The rule’s label inherits the data and bottom property, causing it to appear on the rule and render the value (datum) as text. The bar’s label uses the bottom anchor to tweak positioning, so that the label is centered at the bottom of the bar.
1
4
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
5
  require 'rubyvis'
3
6
 
@@ -5,10 +8,19 @@ vis = pv.Panel.new()
5
8
  .width(150)
6
9
  .height(200)
7
10
 
8
- vis.add(pv.Rule).data(pv.range(0, 2, 0.5)).bottom(lambda {|d| d * 80 + 0.5}).add(pv.Label).left(0)
11
+ vis.add(pv.Rule).
12
+ data(pv.range(0, 2, 0.5)).
13
+ bottom(lambda {|d| d * 80 + 0.5}).
14
+ add(pv.Label).left(0)
9
15
 
10
16
 
11
- vis.add(pv.Bar).data([1, 1.2, 1.7, 1.5, 0.7]).width(20).height(lambda {|d| d * 80}).bottom(0).left(lambda { self.index * 25 + 25}).anchor('bottom').add(pv.Label)
17
+ vis.add(pv.Bar).
18
+ data([1, 1.2, 1.7, 1.5, 0.7]).
19
+ width(20).height(lambda {|d| d * 80}).
20
+ bottom(0).
21
+ left(lambda { index * 25 + 25}).
22
+ anchor('bottom').
23
+ add(pv.Label)
12
24
 
13
25
 
14
26
  vis.render();
data/examples/third.rb CHANGED
@@ -1,18 +1,21 @@
1
+ # = Using parent
2
+ # This example shows how to group bars on groups and use the parent property to identify and color them
1
3
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
4
  require 'rubyvis'
3
5
 
4
6
  vis = pv.Panel.new().width(200).height(150);
5
7
 
6
- bar= vis.add(pv.Panel).data(["a","b"]).add(pv.Bar)
8
+ bar= vis.add(pv.Panel).data(["a","b","c","d"]).add(pv.Bar)
7
9
  .data([1,2])
8
10
  .width(20)
9
- .height(60)
11
+ .height(lambda {60+parent.index*20+index*5})
10
12
  .bottom(0)
11
- .left(lambda {|d,t| self.parent.index*60+self.index*30})
13
+ .left(lambda {|d,t| parent.index*60+index*25})
12
14
 
13
-
14
- bar.anchor("bottom").add(pv.Label).text(lambda {|d,t| "#{t}-#{d}"})
15
- bar.anchor("top").add(pv.Label).text(lambda {"#{self.parent.index}-#{index}"})
15
+ bar.anchor("bottom").add(pv.Label).
16
+ text(lambda {|d,t| "#{t}-#{d}"})
17
+ bar.anchor("top").add(pv.Label).
18
+ text(lambda {"#{parent.index}-#{index}"})
16
19
 
17
20
 
18
21
  vis.render()
@@ -1,4 +1,5 @@
1
1
  module Rubyvis
2
+
2
3
  def self.color(format)
3
4
  return format.rgb if format.respond_to? :rgb
4
5
  if (format =~/([a-z]+)\((.*)\)/)
@@ -31,7 +32,7 @@ module Rubyvis
31
32
  named = Rubyvis::Color.names[format.to_sym]
32
33
 
33
34
 
34
- return pv.color(named) if (named)
35
+ return Rubyvis.color(named) if (named)
35
36
 
36
37
  # Hexadecimal colors: #rgb and #rrggbb. */
37
38
  if (format[0,1]== "#")
@@ -50,6 +51,7 @@ module Rubyvis
50
51
  # Otherwise, pass-through unsupported colors. */
51
52
  return Rubyvis::Color.new(format, 1);
52
53
  end
54
+
53
55
  def self.rgb(r,g,b,a=1)
54
56
  Rubyvis::Color::Rgb.new(r,g,b,a)
55
57
  end
@@ -238,16 +240,16 @@ module Rubyvis
238
240
  self.class==v.class and @r==v.r and @b==v.b and @g=v.g and @a=v.a
239
241
  end
240
242
  def red(r1)
241
- pv.rgb(r1,g,b,a)
243
+ Rubyvis.rgb(r1,g,b,a)
242
244
  end
243
245
  def green(g1)
244
- pv.rgb(r,g1,b,a)
246
+ Rubyvis.rgb(r,g1,b,a)
245
247
  end
246
248
  def blue(b1)
247
- pv.rgb(r,g,b1,a)
249
+ Rubyvis.rgb(r,g,b1,a)
248
250
  end
249
251
  def alpha(a1)
250
- pv.rgb(r,g,b,a1)
252
+ Rubyvis.rgb(r,g,b,a1)
251
253
  end
252
254
 
253
255
  def rgb
@@ -256,7 +258,7 @@ module Rubyvis
256
258
  def darker(*arguments)
257
259
  k,dummy=arguments
258
260
  k = 0.7 ** ( arguments.size>0 ? k : 1)
259
- return pv.rgb(
261
+ return Rubyvis.rgb(
260
262
  [0, (k * self.r).floor].max,
261
263
  [0, (k * self.g).floor].max,
262
264
  [0, (k * self.b).floor].max,