Ignacio Diaz-Emparanza1.02014-10-23A simple 2D pie chartMAINWIN/View/GraphVars
A function for making a pie chart. With the values of a variable and a label for every value, this function prepares a file with instructions to be sent to Gnuplot (the program which gretl uses for making graphics). This file is saved in the 'dotdir' Gretl directory. Then, the file is executed by Gnuplot and the graph is presented in a popup window.
This package has a unique function with four parameters:
A: An N x 1 matrix (where N is the number of observations)(N<11)
S: a string (consisting of N labels, separated by spaces),
radius: a scalar, which determines the radius of the circle,
distance: a scalar, which determines where the labels are placed
An image of 640x480 pixels is created. The radius is a value in the range [0,1] which is measured according to the total width of the image. In tests it has been found that suitable values may be between 0.2 and 0.35. Labels are placed at an intermediate angle of each piece. The parameter 'dist' determines the distance from the origin. dist = 1 indicates that the labels will be placed right on the edge of the circle. In our tests, values between 1.1 and 1.5 have been quite adequate.
scalar nA = rows(A)
if nA > 10
funcerr "Too many values. You'd better use another type of chart"
endif
if cols(A) > 1
funcerr "The input matrix should be N x 1"
endif
string fname = ($windows==0) ? sprintf("@dotdir/%d.plt", floor(randgen1(u,1000,9999))) : sprintf("@dotdir\\%d.plt", floor(randgen1(u,1000,9999)))
matrix A = seq(1,nA)'~A/sumc(A)
matrix A = mreverse(msortby(A,2))
matrix inter=zeros(nA,2)
loop i=1..nA --quiet
scalar io = A[i,1]
string s$i=strsplit(S, io)
scalar v$i=A[i,1]
matrix inter[$i,] = (i==1)? { 0, A[i,2] } : { inter[i-1,2], inter[i-1,2]+A[i,2] }
endloop
matrix inter2 = inter*360
set echo off
set messages off
outfile "@fname" --write
print "reset"
printf "b=%g; B=0.5; h=1.5*b*%g\n", radius, dist
print "unset border; unset tics; unset key"
print "set angles degrees; set yrange [0:1]; set size ratio 1.0; set style fill solid 1.0 border -1"
loop for (j=1;j<=nA;j++) --quiet
printf "set obj %d circle arc [%g:%g] fc lt %d\n", j, inter2[j,1], inter2[j,2], j
printf "set obj %d circle at screen B,B size screen b front\n", j
endloop
print "set angles radians"
d=inter[1,2]
printf "set label \"%s\" at graph B+cos(%f*pi)*h, graph B+sin(%f*pi)*h center front\n", s1, d, d
printf "set label \"%.1f%%\" at graph B+cos(%f*pi)*b, graph B+sin(%f*pi)*b center front tc rgb \"white\"\n", A[1,2]*100, d, d
loop for (j=2;j<=nA;j++) --quiet
d1=inter[j,1]*2
dm2=A[j,2]
printf "set label \"%s\" at graph B+cos((%f+%f)*pi)*h, graph B+sin((%f+%f)*pi)*h center front\n", s$j, d1, dm2, d1, dm2
printf "set label \"%.1f%%\" at graph B+cos((%f+%f)*pi)*b, graph B+sin((%f+%f)*pi)*b center front tc rgb \"white\"\n", A[j,2]*100, d1, dm2, d1, dm2
endloop
print "plot 2"
outfile --close
set force_decpoint on
gnuplot --input="@fname" --output=display
return fname
include gnuplot_piechart.gfn
nulldata 8
#Results of election for European Parliament 2014
matrix A = {221; 191; 70; 67; 52; 50; 48; 52}
string S = "EPP PASD ECR ALDE EUL-NGL Greens-EFA EFDD NI"
string pie1 = gnuplot_piechart(A, S, 0.3, 1.15)
print "@pie1"
quit