2.2 mpie.rb Draw pie graph

The command draws a pie chart. By specifying the attributes assigned to x axis and y axis, users can create 1 dimensional or 2 dimensional pie matrix. The output of the graph is saved as HTML file which can be displayed in popular browsers.

The input data in CSV format is shown in Table 2.3. The fields that construct the pie chart can be specified at f= parameter. The attributes assigned to x-axis and y-axis can be specified in k= parameter. When one field is specified at k= parameter, one-dimensional pie chart matrix is created, if two fields are specified at k= parameter, two-dimensional pie chart matrix is created. When k= parameter is not specified, one pie chart is created.

The pie chart is created with the JavaScript library D3.js (Data-Driven Documents). Please refer to the official website of D3.js (http://d3js.org/) for more details. Note that Nysol/mcmd library is required for this command.

Table 2.3: Number of individuals by age and prefecture

Pref

Age

Population

Nara

10

310504

Nara

20

552339

Nara

30

259034

Nara

40

450818

Nara

50

1231572

Nara

60

1215966

Nara

70

641667

Hokkaido

10

310504

Hokkaido

20

252339

Hokkaido

30

859034

Hokkaido

40

150818

Hokkaido

50

9231572

Hokkaido

60

4215966

Hokkaido

70

341667


2.2.1 Format

  mpie.rb [i=] f= v= [o=] [k=] [title=] [pr=] [cc=] [--help]

i=

Input data file name (CSV format)

f=

Specify the field name of component.

 

Null data is ignored.

v=

Specify the field name of component ratio (item that determines the arc length of the pie chart).

 

Data is treated as zero if it consists of null value.

 

Values started with 0 is ignored. Values except numeric characters returns error.

o=

Output file name (HTML file)

k=

Specify two or less field names assigned to x-axis and y-axis.

 

Create one pie chart when this parameter is not specified.

 

Create a one-dimensional pie chart when one field is specified,

 

create two-dimensional pie chart when two fields are specified.

title=

Specify the title of the graph

pr=

Specify the radius of the pie chart (default is 160).

cc=

Specify the maximum number of pie charts to be displayed in 1 row (default is 5).

 

Specify for one-dimension pie chart matrix.

--help

Show help

Noted that in mpie.rb command, the fields specified in f= parameter and k= parameter do not have automatic sorting function. It is necessary to sort the data prior to this command as needed.

2.2.2 Examples

例1: Draw a pie chart

Using the data dat1.csv, set Age as the unit of composition, draw a one dimensional pie chart showing the population. The output of the pie chart can be viewed in the web browser. Note that in the pie chart, when you place the mouse cursor over the bar chart, details of the unit of composition is shown in a pop-up box. The graph can also be moved by dragging the mouse, and the size of the graph can be scaled by scrolling the mouse.

$ more dat1.csv
Age,Population
10,310504
20,552339
30,259034.5555
40,0450818
50,1231572
60,1215966
70,641667
$ mpie.rb i=dat1.csv v=Population f=Age o=result1.html
#END# /usr/bin/mpie.rb i=dat1.csv v=Population f=Age o=result1.html
$ head result1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}
\includegraphics[scale=0.7]{figure/mpie1.eps}

例2: Draw a one dimensional pie chart

Using the data dat2.csv, set Age as the unit of composition, draw the pie chart showing the population by age. Specify Pref at k= parameter, which is designated on the x-axis as a one-dimensional pie chart. Specify the title of the graph at title= parameter.

$ more dat2.csv
Pref,Age,Population
Nara,10,310504
Nara,20,552339
Nara,30,259034
Nara,40,450818
Nara,50,1231572
Nara,60,1215966
Nara,70,641667
Hokkaido,10,310504
Hokkaido,20,252339
Hokkaido,30,859034
Hokkaido,40,150818
Hokkaido,50,9231572
Hokkaido,60,4215966
Hokkaido,70,341667
$ mpie.rb i=dat2.csv k=Pref v=Population f=Age o=result2.html title='Population of Nara and 
Hokkaido by Age Group'
#END# /usr/bin/mpie.rb i=dat2.csv k=Pref v=Population f=Age o=result2.html title=Population of Nara and Hokkaido by Age Group
$ head result2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}
\includegraphics[scale=0.5]{figure/mpie2.eps}

例3: Draw a two dimensional pie chart

Using the data dat3.csv, set ThemePark as the unit of composition, draw the pie chart showing the theme parks by Number field, and specify the radius at 100 at the pr= parameter. Specify Gender and Age at k= parameter, which designates Gender on the x-axis, and Age on y-axis.

$ more dat3.csv
Gender,Age,ThemePark,Number
Male,30,Disney,100
Male,30,UFJ,59
Male,30,Umeyashiki,180
Male,40,Disney,200
Male,40,UFJ,3
Male,40,Umeyashiki,10
Male,50,Disney,110
Male,50,UFJ,40
Female,30,Umeyashiki,100
Female,30,Disney,80
Female,30,UFJ,200
Female,40,Disney,90
Female,40,UFJ,80
Female,40,Umeyashiki,120
Female,50,Disney,99
Female,50,UFJ,80
Female,50,Umeyashiki,110
$ mpie.rb i=dat3.csv k=Gender,Age v=Number f=ThemePark o=result3.html
#END# /usr/bin/mpie.rb i=dat3.csv k=Gender,Age v=Number f=ThemePark o=result3.html
$ head result3.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}
\includegraphics[scale=0.5]{figure/mpie3.eps}