In [1]:
require 'daru/view'
Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.

Install the mechanize gem version ~>2.7.5 for using mechanize functions.
Out[1]:
true
In [2]:
Daru::View.plotting_library = :googlecharts
Out[2]:
:googlecharts
In [3]:
data = [
          ['Task', 'Hours per Day'],
          ['Work',     11],
          ['Eat',      2],
          ['Commute',  2],
          ['Watch TV', 2],
          ['Sleep',    7]
  ]
pie_chart_table = Daru::View::Table.new(data)
pie_chart_table.show_in_iruby
Out[3]:
In [7]:
pie_chart_options = {
  type: :pie, height: 400,  title: 'My Daily Activities'
}
pie_chart_chart = Daru::View::Plot.new(pie_chart_table.table, pie_chart_options)
pie_chart_chart.show_in_iruby
Out[7]:
In [10]:
pie_chart3D_options = {
  type: :pie, height: 400,  title: 'My Daily Activities', is3D: true
}
pie_chart3D = Daru::View::Plot.new(pie_chart_table.table, pie_chart3D_options)
pie_chart3D.show_in_iruby
Out[10]:

Making a Donut Chart

In [11]:
pie_chart_dount_options = {
  type: :pie, height: 400,  title: 'My Daily Activities',pieHole: 0.4,
}
pie_chart_dount = Daru::View::Plot.new(pie_chart_table.table, pie_chart_dount_options)
pie_chart_dount.show_in_iruby
Out[11]:
In [12]:
pie_chart_dount_options = {
  type: :pie, height: 400,  title: 'My Daily Activities',pieHole: 0.5,
          pieSliceTextStyle: {
            color: 'black',
          },
          legend: 'none'
}
pie_chart_dount = Daru::View::Plot.new(pie_chart_table.table, pie_chart_dount_options)
pie_chart_dount.show_in_iruby
Out[12]:

Rotating a Pie Chart

In [13]:
data = [
          ['Language', 'Speakers (in millions)'],
          ['German',  5.85],
          ['French',  1.66],
          ['Italian', 0.316],
          ['Romansh', 0.0791]
  ]
pie_chart_rotate_table = Daru::View::Table.new(data)
pie_chart_rotate_table.show_in_iruby
Out[13]:
In [18]:
pie_chart_rotate_options = {
type: :pie, height: 500
}
pie_chart_rotate_dount = Daru::View::Plot.new(pie_chart_rotate_table.table, pie_chart_rotate_options)
pie_chart_rotate_dount.show_in_iruby
Out[18]:
In [19]:
pie_chart_rotate_options = {
  legend: 'none',
        pieSliceText: 'label',
        title: 'Swiss Language Use (100 degree rotation)',
        pieStartAngle: 100,type: :pie, height: 500
}
pie_chart_rotate_dount = Daru::View::Plot.new(pie_chart_rotate_table.table, pie_chart_rotate_options)
pie_chart_rotate_dount.show_in_iruby
Out[19]:

Exploding a Slice

In [22]:
data = [
          ['Language', 'Speakers (in millions)'],
          ['Assamese', 13], ['Bengali', 83], ['Bodo', 1.4],
          ['Dogri', 2.3], ['Gujarati', 46], ['Hindi', 300],
          ['Kannada', 38], ['Kashmiri', 5.5], ['Konkani', 5],
          ['Maithili', 20], ['Malayalam', 33], ['Manipuri', 1.5],
          ['Marathi', 72], ['Nepali', 2.9], ['Oriya', 33],
          ['Punjabi', 29], ['Sanskrit', 0.01], ['Santhali', 6.5],
          ['Sindhi', 2.5], ['Tamil', 61], ['Telugu', 74], ['Urdu', 52]
  ]
pie_chart_slice_table = Daru::View::Table.new(data, pageSize: 5)
pie_chart_slice_table.show_in_iruby
Out[22]:
In [24]:
pie_chart_slice_options = {
type: :pie, height: 500
}
pie_chart_slice = Daru::View::Plot.new(pie_chart_slice_table.table, pie_chart_slice_options)
pie_chart_slice.show_in_iruby
Out[24]:
In [26]:
pie_chart_slice_options = {
  title: 'Indian Language Use',
          legend: 'none',
          pieSliceText: 'label',
          slices: {  4 => {offset: 0.2},
                    12 => {offset: 0.3},
                    14 => {offset: 0.4},
                    15 => {offset: 0.5},
          },
        type: :pie, height: 500
}
pie_chart_slice = Daru::View::Plot.new(pie_chart_slice_table.table, pie_chart_slice_options)
pie_chart_slice.show_in_iruby
Out[26]:

Removing Slices

In [27]:
idx = Daru::Index.new ['Pac Man', 'Percentage']
data_rows = [
          ['', 75],
          ['', 25]]
df_pac_man = Daru::DataFrame.rows(data_rows)
df_pac_man.vectors = idx
df_pac_man
Out[27]:
Daru::DataFrame(2x2)
Pac Man Percentage
0 75
1 25
In [30]:
pie_pac_chart = Daru::View::Plot.new(df_pac_man, type: :pie)
pie_pac_chart.show_in_iruby
Out[30]:
In [32]:
opts = {
  legend: 'none',
          pieSliceText: 'none',
          pieStartAngle: 135,
          tooltip: { trigger: 'none' },
          slices: {
            0 => { color: 'yellow' },
            1 => { color: 'transparent' }
          },
  type: :pie
  }
pie_pac_chart = Daru::View::Plot.new(df_pac_man, opts)
pie_pac_chart.show_in_iruby
Out[32]:

Slice Visibility Threshold

In [35]:
idx = Daru::Index.new ['Pizza', 'Populartiy']
data_rows = [
          ['Pepperoni', 33],
        ['Hawaiian', 26],
        ['Mushroom', 22],
        ['Sausage', 10], # Below limit.
        ['Anchovies', 9] # Below limit.
  ]
df_pizza = Daru::DataFrame.rows(data_rows)
df_pizza.vectors = idx
df_pizza
Out[35]:
Daru::DataFrame(5x2)
Pizza Populartiy
0 Pepperoni 33
1 Hawaiian 26
2 Mushroom 22
3 Sausage 10
4 Anchovies 9
In [40]:
opts = {
title: 'Popularity of Types of Pizza',
        sliceVisibilityThreshold: 0.2,
  type: :pie, height: 400
  }
pie_pizza = Daru::View::Plot.new(df_pizza, opts)
pie_pizza.show_in_iruby
Out[40]: