Re: pie chart ?

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Fri Dec 18 2009 - 16:03:30 MST

The attached is very rough. In particular the labeling needs work.
It uses gsn_polyline, psn_polygon and gsn_text.
If you want paneling you will have to do as Mary suggests use the
_add_ versions of the procedures

Still, it could help you develop a pie chart.

Good luck

Mary Haley wrote:
> Hi Ozan,
>
> I'm afraid we don't have any ready-made scripts to do this, but maybe
> somebody else has created one.
>
> I would either use gsn_polygon_ndc, gsn_polyline_ndc, etc, or else
> gsn_blank_plot in conjunction with gsn_add_polygon and
> gsn_add_polyline (if you needed
> to create multiple pies per page).
>
> --Mary
>
> On Dec 18, 2009, at 7:51 AM, ozan mert gokturk wrote:
>
>
>> Dear All,
>>
>> Just out of curiosity: Is there a way of making a pie chart with
>> NCL? Or has someone ever tried? If yes, how?
>>
>> Thanks in advance.
>>
>> Ozan
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

-- 
======================================================
Dennis J. Shea                  tel: 303-497-1361    |
P.O. Box 3000                   fax: 303-497-1333    |
Climate Analysis Section                             |
Climate & Global Dynamics Div.                       |
National Center for Atmospheric Research             |
Boulder, CO  80307                                   |
USA                        email: shea 'at' ucar.edu |
======================================================

; ******************************************************************
; ******* THERE IS NO SUPPORT FOR THE PIE CHART FUNCTION **********
; ******************************************************************
; ******* These were developed to draw the figures the way I like them.
; ******* If the user wishes to change something to their preference,
; ******* they may excerpt the relevent function and modify as they wish.
; ******* Good luck!
; ******************************************************************

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"

undef("pie_chart")
function pie_chart(wks:graphic, percent[*]:numeric, name[*]:string \
                              , colors[*]:string, pieChartRes:logical)

; Plot a Pie Chart
; Nomenclature
; percent - percent [%] for each section
; name - names to be associated with each section
; color colors for each section
; color = (/ "red", "green", "blue", "brown" /)
; pieChartRes - resources which affect plot appearance
;

 begin
  debug = True

  rad = 4.*atan(1.0)/180. ; degress to radians
                                               ; Specify limits for X and Y axes.
  circ = 75 ; arbitrary
  extraSpace = 25 ; Extra space beyond outer circle

  opts = True ; local and default options
  opts@trXMinF = -circ-extraSpace ; min X
  opts@trXMaxF = circ+extraSpace ; max X
  opts@trYMinF = -circ-extraSpace ; min Y
  opts@trYMaxF = circ+extraSpace ; max Y
  opts@tmXTOn = False ; turn off tick marks on each side
  opts@tmXBOn = False
  opts@tmYLOn = False
  opts@tmYROn = False
  opts@tmXBBorderOn = False ; turn off borders on each side
  opts@tmXTBorderOn = False
  opts@tmYLBorderOn = False
  opts@tmYRBorderOn = False
  opts@gsnFrame = False ; do not advance frame
  opts@gsnDraw = False ; do not draw
  opts@xyMonoDashPattern = True ; set all circles to solid
  if (pieChartRes .and. isatt(pieChartRes,"tiMainString")) then
      opts@tiMainString = pieChartRes@tiMainString
  end if

  if (debug) then
      print(opts)
  end if
 
  plotPC = gsn_xy(wks,(/0.0,0.0/),(/0.0,0.0/),opts)

  nSections = dimsizes(percent)

  plRes = True
  pgRes = True

  xBase = 0
  yBase = 0
  
  pcPie = (percent/100.)*360 ; percent of 360 degrees
  pcStrt = 0.0
  pcLast = pcPie(0)
  phase = 90.0 - 0.5*pcPie(0) ; 'center' 1st section n=0

  do n=0,nSections-1

     npts = round(pcPie(n), 3)
     npts2 = npts+2
     xx = new (npts2, "double", "No_FillValue")
     yy = new (npts2, "double", "No_FillValue")
     xx(0) = xBase
     yy(0) = yBase
     xx(1:npts2-2) = circ*cos((fspan(pcStrt, pcLast, npts)+phase)*rad)
     yy(1:npts2-2) = circ*sin((fspan(pcStrt, pcLast, npts)+phase)*rad)
     xx(npts2-1) = xBase
     yy(npts2-1) = yBase

     if (pieChartRes .and. isatt(pieChartRes,"OutLine") .and. pieChartRes@OutLine) then
        ;plRes@gsLineColor = colors(n)
         plRes@gsLineColor = "black"
         plRes@gsLineThicknessF = 2.0
         gsn_polyline(wks, plotPC, xx, yy, plRes)
     end if

     pgRes@gsFillColor = colors(n)
     gsn_polygon (wks,plotPC,xx,yy,pgRes)

     txRes = True ; text mods desired
     txRes@txFontHeightF = 0.03 ; font smaller. default big

     xxText = xx(npts/2)
     yyText = yy(npts/2)
     gsn_text (wks,plotPC,name(n),xxText,yyText,txRes)

     delete(xx)
     delete(yy)
     if (n.lt.(nSections-1)) then
         pcStrt = pcLast+0.001
         pcLast = pcLast+pcPie(n+1)
     end if
  end do

  return (plotPC)
 end

;========================================================================
; Main
;========================================================================

 percent = (/10.5, 50, 24.5, 15 /)
 color = (/ "red", "green", "blue", "yellow" /)
 name = (/ "AAAA", "BBB", "C", "DCBABCD" /)

 pcRes = True
 pcRes@OutLine = True
 pcRes@tiMainString = "Simple Test Pie Chart"

 wks = gsn_open_wks("x11","PieChart")
 plot = pie_chart(wks, percent, name, color, pcRes)
 draw(plot)
 frame(wks)
Received on Fri Dec 18 16:04:36 2009

This archive was generated by hypermail 2.1.8 : Mon Dec 21 2009 - 09:22:56 MST