charty 0.1.4.dev → 0.1.5.dev

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,16 @@
2
2
  "cells": [
3
3
  {
4
4
  "cell_type": "code",
5
- "execution_count": 1,
5
+ "execution_count": 15,
6
6
  "metadata": {},
7
7
  "outputs": [
8
8
  {
9
9
  "data": {
10
10
  "text/plain": [
11
- "[:inline, \"module://matplotlib.backends.backend_inline\"]"
11
+ "[:inline, \"module://ruby.matplotlib.backend_inline\"]"
12
12
  ]
13
13
  },
14
- "execution_count": 1,
14
+ "execution_count": 15,
15
15
  "metadata": {},
16
16
  "output_type": "execute_result"
17
17
  }
@@ -20,7 +20,7 @@
20
20
  "require 'charty'\n",
21
21
  "\n",
22
22
  "charty = Charty::Plotter.new(:pyplot)\n",
23
- "Charty::Matplot.activate_iruby_integration"
23
+ "Charty::PyPlot.activate_iruby_integration"
24
24
  ]
25
25
  },
26
26
  {
@@ -200,16 +200,16 @@
200
200
  },
201
201
  {
202
202
  "cell_type": "code",
203
- "execution_count": 8,
203
+ "execution_count": 16,
204
204
  "metadata": {},
205
205
  "outputs": [
206
206
  {
207
207
  "data": {
208
208
  "text/plain": [
209
- "#<Charty::Layout:0x00007f89d6291870 @frontend=#<Charty::Matplot:0x00007f89d713e580 @plot=<module 'matplotlib.pyplot' from '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/pyplot.py'>, @series=[#<struct Charty::Series xs=0..10, ys=#<Enumerator: 0..1:step(0.1)>, zs=nil, xerr=nil, yerr=nil, label=\"sample1\">, #<struct Charty::Series xs=0..5, ys=#<Enumerator: 0..1:step(0.2)>, zs=nil, xerr=nil, yerr=nil, label=\"sample2\">, #<struct Charty::Series xs=[0, 1, 2, 3, 4], ys=[0, -0.1, -0.5, -0.5, 0.1], zs=nil, xerr=nil, yerr=nil, label=nil>]>, @layout=#<Charty::ArrayLayout:0x00007f89d62917f8 @array=[], @direction=:horizontal>>"
209
+ "#<Charty::Layout:0x000055f440ba50d8 @frontend=#<Charty::PyPlot:0x000055f440b766c0 @plot=<module 'matplotlib.pyplot' from '/opt/conda/lib/python3.7/site-packages/matplotlib/pyplot.py'>>, @layout=#<Charty::ArrayLayout:0x000055f440ba50b0 @array=[], @direction=:horizontal>>"
210
210
  ]
211
211
  },
212
- "execution_count": 8,
212
+ "execution_count": 16,
213
213
  "metadata": {},
214
214
  "output_type": "execute_result"
215
215
  }
@@ -376,7 +376,7 @@
376
376
  ],
377
377
  "metadata": {
378
378
  "kernelspec": {
379
- "display_name": "Ruby 2.5.0",
379
+ "display_name": "Ruby 2.6.2",
380
380
  "language": "ruby",
381
381
  "name": "ruby"
382
382
  },
@@ -384,7 +384,7 @@
384
384
  "file_extension": ".rb",
385
385
  "mimetype": "application/x-ruby",
386
386
  "name": "ruby",
387
- "version": "2.5.0"
387
+ "version": "2.6.2"
388
388
  },
389
389
  "toc": {
390
390
  "nav_menu": {},
@@ -5,9 +5,3 @@ require_relative "charty/plotter_adapter"
5
5
  require_relative "charty/layout"
6
6
  require_relative "charty/linspace"
7
7
  require_relative "charty/table"
8
-
9
- module Charty
10
- def self.new(*args)
11
- Charty::Plotter.new(*args)
12
- end
13
- end
@@ -0,0 +1,74 @@
1
+ require 'pycall'
2
+
3
+ module Charty
4
+ class Bokeh < PlotterAdapter
5
+ Name = "bokeh"
6
+
7
+ def initialize
8
+ @plot = PyCall.import_module('bokeh.plotting')
9
+ end
10
+
11
+ def series=(series)
12
+ @series = series
13
+ end
14
+
15
+ def render(context, filename)
16
+ plot = plot(context)
17
+ save(plot, context, filename)
18
+ PyCall.import_module('bokeh.io').show(plot)
19
+ end
20
+
21
+ def save(plot, context, filename)
22
+ if filename
23
+ PyCall.import_module('bokeh.io').save(plot, filename)
24
+ end
25
+ end
26
+
27
+ def plot(context)
28
+ #TODO To implement boxplot, bublle, error_bar, hist.
29
+
30
+ plot = @plot.figure(title: context&.title)
31
+ plot.xaxis[0].axis_label = context&.xlabel
32
+ plot.yaxis[0].axis_label = context&.ylabel
33
+
34
+ case context.method
35
+ when :bar
36
+ context.series.each do |data|
37
+ diffs = data.xs.to_a.each_cons(2).map {|n, m| (n - m).abs }
38
+ width = diffs.min * 0.8
39
+ plot.vbar(data.xs.to_a, width, data.ys.to_a)
40
+ end
41
+
42
+ when :barh
43
+ context.series.each do |data|
44
+ diffs = data.xs.to_a.each_cons(2).map {|n, m| (n - m).abs }
45
+ height = diffs.min * 0.8
46
+ plot.hbar(data.xs.to_a, height, data.ys.to_a)
47
+ end
48
+
49
+ when :boxplot
50
+ raise NotImplementedError
51
+
52
+ when :bubble
53
+ raise NotImplementedError
54
+
55
+ when :curve
56
+ context.series.each do |data|
57
+ plot.line(data.xs.to_a, data.ys.to_a)
58
+ end
59
+
60
+ when :scatter
61
+ context.series.each do |data|
62
+ plot.scatter(data.xs.to_a, data.ys.to_a)
63
+ end
64
+
65
+ when :error_bar
66
+ raise NotImplementedError
67
+
68
+ when :hist
69
+ raise NotImplementedError
70
+ end
71
+ plot
72
+ end
73
+ end
74
+ end
@@ -45,8 +45,26 @@ module Charty
45
45
  end
46
46
  p
47
47
  when :barh
48
- # TODO: To implement
49
- raise NotImplementedError
48
+ p = plot::SideBar.new
49
+ p.title = context.title if context.title
50
+ p.x_axis_label = context.xlabel if context.xlabel
51
+ p.y_axis_label = context.ylabel if context.ylabel
52
+ labels = context.series.map {|data| data.xs.to_a}.flatten.uniq
53
+ labels.each do |label|
54
+ data_ys = context.series.map do |data|
55
+ if data.xs.to_a.index(label)
56
+ data.ys.to_a[data.xs.to_a.index(label)]
57
+ else
58
+ 0
59
+ end
60
+ end
61
+ p.data(label, data_ys)
62
+ end
63
+ p.labels = context.series.each_with_index.inject({}) do |attr, (data, i)|
64
+ attr[i] = data.label
65
+ attr
66
+ end
67
+ p
50
68
  when :box_plot
51
69
  # refs. https://github.com/topfunky/gruff/issues/155
52
70
  raise NotImplementedError
@@ -40,6 +40,14 @@ module Charty
40
40
  @plot.show
41
41
  end
42
42
 
43
+ def save(context, filename)
44
+ plot(@plot, context)
45
+ if filename
46
+ FileUtils.mkdir_p(File.dirname(filename))
47
+ @plot.savefig(filename)
48
+ end
49
+ end
50
+
43
51
  def plot(plot, context, subplot: false)
44
52
  # TODO: Since it is not required, research and change conditions.
45
53
  # case
@@ -68,7 +76,7 @@ module Charty
68
76
  plot.barh(data.xs.to_a.map(&:to_s), data.ys.to_a)
69
77
  end
70
78
  when :box_plot
71
- plot.boxplot(context.data.to_a)
79
+ plot.boxplot(context.data.to_a, labels: context.labels)
72
80
  when :bubble
73
81
  context.series.each do |data|
74
82
  plot.scatter(data.xs.to_a, data.ys.to_a, s: data.zs.to_a, alpha: 0.5, label: data.label)
@@ -155,14 +155,14 @@ module Charty
155
155
  Series = Struct.new(:xs, :ys, :zs, :xerr, :yerr, :label)
156
156
 
157
157
  class RenderContext
158
- attr_reader :function, :range, :series, :method, :data, :title, :xlabel, :ylabel
158
+ attr_reader :function, :range, :series, :method, :data, :title, :xlabel, :ylabel, :labels
159
159
 
160
160
  def initialize(method, **args, &block)
161
161
  @method = method
162
162
  configurator = Configurator.new(**args)
163
163
  configurator.instance_eval &block
164
164
  # TODO: label も外から付けられた方がよさそう
165
- (@range, @series, @function, @data, @title, @xlabel, @ylabel) = configurator.to_a
165
+ (@range, @series, @function, @data, @title, @xlabel, @ylabel, @labels) = configurator.to_a
166
166
  end
167
167
 
168
168
  class Configurator
@@ -191,6 +191,10 @@ module Charty
191
191
  @ylabel = ylabel
192
192
  end
193
193
 
194
+ def labels(labels)
195
+ @labels = labels
196
+ end
197
+
194
198
  def label(x, y)
195
199
 
196
200
  end
@@ -204,7 +208,7 @@ module Charty
204
208
  end
205
209
 
206
210
  def to_a
207
- [@range, @series, @function, @data, @title, @xlabel, @ylabel]
211
+ [@range, @series, @function, @data, @title, @xlabel, @ylabel, @labels]
208
212
  end
209
213
 
210
214
  def method_missing(method, *args)
@@ -228,6 +232,10 @@ module Charty
228
232
  @plotter_adapter.render(self, filename)
229
233
  end
230
234
 
235
+ def save(filename=nil)
236
+ @plotter_adapter.save(self, filename)
237
+ end
238
+
231
239
  def apply(plotter_adapter)
232
240
  case
233
241
  when !@series.empty?
@@ -23,6 +23,8 @@ module Charty
23
23
  else
24
24
  raise ArgumentError, "column_names is required to convert to_a from ActiveRecord::Relation"
25
25
  end
26
+ when table.kind_of?(Array)
27
+ table
26
28
  else
27
29
  raise ArgumentError, "unsupported object: #{table.inspect}"
28
30
  end
@@ -1,5 +1,5 @@
1
1
  module Charty
2
- VERSION = "0.1.4-dev"
2
+ VERSION = "0.1.5-dev"
3
3
 
4
4
  module Version
5
5
  numbers, TAG = VERSION.split("-")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: charty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4.dev
4
+ version: 0.1.5.dev
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2019-06-06 00:00:00.000000000 Z
13
+ date: 2019-07-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -68,20 +68,6 @@ dependencies:
68
68
  - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
- - !ruby/object:Gem::Dependency
72
- name: gruff
73
- requirement: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- type: :development
79
- prerelease: false
80
- version_requirements: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '0'
85
71
  description: Visualizing your data in a simple way.
86
72
  email:
87
73
  - youchan01@gmail.com
@@ -113,8 +99,9 @@ files:
113
99
  - examples/sample_images/bar_gruff.png
114
100
  - examples/sample_images/bar_pyplot.png
115
101
  - examples/sample_images/bar_rubyplot.png
102
+ - examples/sample_images/barh_gruff.png
116
103
  - examples/sample_images/barh_pyplot.png
117
- - examples/sample_images/boxplot_pyplot.png
104
+ - examples/sample_images/box_plot_pyplot.png
118
105
  - examples/sample_images/bubble_pyplot.png
119
106
  - examples/sample_images/bubble_rubyplot.png
120
107
  - examples/sample_images/curve_gruff.png
@@ -122,7 +109,7 @@ files:
122
109
  - examples/sample_images/curve_rubyplot.png
123
110
  - examples/sample_images/curve_with_function_pyplot.png
124
111
  - examples/sample_images/curve_with_function_rubyplot.png
125
- - examples/sample_images/errorbar_pyplot.png
112
+ - examples/sample_images/error_bar_pyplot.png
126
113
  - examples/sample_images/hist_pyplot.png
127
114
  - examples/sample_images/scatter_gruff.png
128
115
  - examples/sample_images/scatter_pyplot.png
@@ -133,6 +120,7 @@ files:
133
120
  - examples/sample_rubyplot.ipynb
134
121
  - images/design_concept.png
135
122
  - lib/charty.rb
123
+ - lib/charty/backends/bokeh.rb
136
124
  - lib/charty/backends/google_chart.rb
137
125
  - lib/charty/backends/gruff.rb
138
126
  - lib/charty/backends/pyplot.rb