Menu

2D doughnut chart generation

Gianni
2017-12-14
2020-11-05
  • Gianni

    Gianni - 2017-12-14

    Hi gnuplotters,

    I am quite a newbie to gnuplot but I was quickly able to get what I
    need; now I am facing a real issue:

    after successfully generating various chart types I am now trying to
    generate a multiserie doughnut chart out from a CSV datafile;
    everything is working properly with 2 data series but when I try to add
    a third serie GNUplot is plotting only a maximum of 6 slices out of X in
    the third serie (see attachment).

    I've tried to:
    - reduce number of slices in each serie => it is reducing number of
    slices also in the third series
    - increment number of slices in each serie => in the third serie it
    plots up to a maximum of six slices

    My tests were done in a Windows environment; trying to execute same
    script on my main ubuntu machine is giving me back this error:
    "doughnut.gplot", line 94: "with" allowed only after parametric function
    fully specified
    regarding first usage of "with filledcurves" in the plot command. I did
    not try to find a solution to this second issue because the first one is
    blocking me.

    Could someone help me to undestand if these are bugs or I am simply
    doing something wrong?

    Thanks in advance,
    Gianni

    CSV datafile:

    ;Serie1;Serie2;Serie3;Serie4;
    One;1;2.5;12;5;
    Two;2;3;11;5;
    Three;3;5;10;5;
    Four;4;7;9;5;
    Five;5;9;8;5;
    Six;6;11;7;5;
    Seven;7;12;6;5;
    Eight;8;10;5;5;
    Nine;9;8;4;5;
    Ten;10;6;3;5;

    GNUplot script:

    reset

    input file

    filename = 'oth\doughnut.csv' # With headers
    rowi = 1 # Initial row
    rowf = 10 # Final row

    set datafile separator ';'

    obtain sum(column(x)) from rows rowi to rowf

    stats filename using 2 every ::rowi::rowf noout prefix "A"
    stats filename using 3 every ::rowi::rowf noout prefix "B"
    stats filename using 4 every ::rowi::rowf noout prefix "C"
    stats filename using 4 every ::rowi::rowf noout prefix "D"

    defining functions

    angleA(x)=x360/A_sum
    percentA(x)=x
    100/A_sum
    angleB(x)=x360/B_sum
    percentB(x)=x
    100/B_sum
    angleC(x)=x360/C_sum
    percentC(x)=x
    100/C_sum
    angleD(x)=x360/D_sum
    percentD(x)=x
    100/D_sum

    init colour for each serie

    colourA = 0 # init colour serie1
    colourB = 0 # init colour serie2
    colourC = 0 # init colour serie3
    colourD = 0 # init colour serie4

    output file

    set terminal pngcairo size 1280,800 enhanced font "Arial,20"
    set output 'oth\doughnut.png'

    circumference and dimensions for pie-chart

    radius=1
    centerX=0
    centerY=0

    label positions

    yposmin = 0.0
    yposmax = 0.98radius
    xpos = 1.5
    radius
    ypos(i) = yposmax - i(yposmax-yposmin)/(1.0rowf-rowi)

    -----------------------------------------------------

    now we can configure the canvas

    set style fill solid 1 # filled pie-chart
    unset key # no automatic labels
    unset tics # remove tics
    unset border # remove borders; if some label is missing, comment to see
    what is happening

    set size ratio -1 # equal scale length
    set xrange [-radius:2*radius] # [-1:2] leaves space for labels
    set yrange [-radius:radius] # [-1:1]

    set title "DOUGHNUT CHART TITLE"

    set linetype 1 linecolor rgb '#F0F0F0'
    set linetype 2 linecolor rgb '#FFFF00'
    set linetype 3 linecolor rgb '#FFA500'
    set linetype 4 linecolor rgb '#FFC0CB'
    set linetype 5 linecolor rgb '#FF0000'
    set linetype 6 linecolor rgb '#A52A2A'
    set linetype 7 linecolor rgb '#00FF00'
    set linetype 8 linecolor rgb '#32CD32'
    set linetype 9 linecolor rgb '#00FFFF'
    set linetype 10 linecolor rgb '#4169E1'
    set linetype 11 linecolor rgb '#EE82EE'
    set linetype 12 linecolor rgb '#AFAFAF'

    Prepare for border of each serie

    set trange [0:2*pi]
    set parametric
    set style line 1 linecolor rgb '#000000' linetype 1 linewidth 4
    set style line 2 linecolor rgb '#FFFFFF' linetype 1 linewidth 10

    Parametric functions for a circle line

    fx(t) = rcos(t)
    fy(t) = r
    sin(t)

    -----------------------------------------------------

    pos = 0; mpos = 0.0; # init angles
    mid = 0.0; Bi = 0.0; # mid angle for chart values

    1st line: external border

    2nd line: plot pie-chart slices

    3rd line: draw colored boxes at (xpos):(ypos)

    4th line: place labels at (xpos+offset):(ypos)

    from 5th line other series

    plot (colourA=0, r=radius1.00, fx(t)),fy(t) linestyle 1, \      filename using
    (centerX):(centerY):(radius
    1.00):(pos):(pos=pos+angleA($2)):(colourA=colourA+1)
    every ::rowi::rowf with circle linecolor var, \    for [i=0:rowf-rowi] '+' using (xpos):(ypos(i)) with points pointtype
    5 pointsize 4 linecolor i+1, \    for [i=0:rowf-rowi] filename using (xpos):(ypos(i)):(sprintf('%s
    (%s)', stringcolumn(1), stringcolumn(2))) every ::i+rowi::i+rowi with
    labels left offset 3,0, \      (colourB=0, r=radius0.75, fx(t)),fy(t) with filledcurves
    linestyle 2, \      filename using
    (centerX):(centerY):(radius
    0.75):(pos):(pos=pos+angleB($3)):(colourB=colourB+1)
    every ::rowi::rowf with circle linecolor var, \      (colourC=0, r=radius0.50, fx(t)),fy(t) with filledcurves
    linestyle 2, \      filename using
    (centerX):(centerY):(radius
    0.50):(pos):(pos=pos+angleC($4)):(colourC=colourC+1)
    every ::rowi::rowf with circle linecolor var, \      (colourD=0, r=radius0.25, fx(t)),fy(t) with filledcurves
    linestyle 2, \      filename using
    (centerX):(centerY):(radius
    0.25):(pos):(pos=pos+angleD($5)):(colourD=colourD+1)
    every ::rowi::rowf with circle linecolor var

     
  • Ethan Merritt

    Ethan Merritt - 2017-12-15

    Iteration inside a  plot command is not possible for parametric plots.
    In most cases you can use instead a

    For example instead of
      set parametric; plot sin(x), cos(x)
    Usr
      plot '+' using (sin($1)):(cos($1))

     
  • Xiao Dong

    Xiao Dong - 2020-11-05

    i'm not sure if you are still interested in a solution to your problem, since it's been a long time since your post. anyway, i think it's this thing causing the problem:
    "(colourB=0, r=radius0.75, fx(t)),fy(t) with filledcurves linestyle 2,"

    i rewrote your code to the following and it works:

    plot \
    for [i=0:rowf-rowi] '+' using (xpos):(ypos(i)) with points pointtype 5 pointsize 4 linecolor i+1 , \
    for [i=0:rowf-rowi] filename using (xpos):(ypos(i)):(sprintf('%s(%s)', stringcolumn(1), stringcolumn(2))) every ::i+rowi::i+rowi with labels left offset 3,0 , \
    filename using (centerX):(centerY):(radius1.00):(pos):(pos=pos+angleA($4)):(colourA=colourA+1) with circle linecolor var, \
    filename using (centerX):(centerY):(radius
    0.90):(pos):(pos=pos+angleB($3)):(colourB=colourB+1) with circle linecolor var, \
    filename using (centerX):(centerY):(radius0.80):(pos):(pos=pos+angleC($2)):(colourC=colourC+1) with circle linecolor var, \
    (r=radius
    0.90, fx1(t)),fy1(t) with l linestyle 2, \
    (r=radius0.80, fx2(t)),fy2(t) with l linestyle 2, \
    (r=radius
    0.70, fx3(t)),fy3(t) with filledcurves linestyle 2

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.