Pie Charts with Mapserver: Simple Perl code to create WKT Insert statements for

Matthew Pettis matthew.pettis at GMAIL.COM
Thu Jan 10 15:00:11 EST 2008


Hi,

This is a solution, rather than a problem.

I had need for a pie chart with a clickable imagemap in a webpage where I
was also rendering maps with mapserver.  I thought about looking for a tool
to do this for me, then decided to write a quick script to make a small
table in my postgis database that would have the necessary information to
draw the pie chart with mapserver.  This code will generate a pie-ish chart
within a bounding box of (-1 -1 1 1).  It is pie-ish because the sectors are
actually triangles, not true circle sectors.  That could be modified, I had
over 100 segments, and that approximated a circle well enough for my
purposes.

I am submitting the perl code in case others have use for it.  Trivial abuse
of mapserver, yes, but it was very handy for me -- perhaps it will be handy
for someone else.

Below the dotted line is the perl code.

Thanks,
Matt

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

#This perl code generates coordinate combinations using WKT to create
#a table whose geometry rendered by mapserver will create a pie-ish
#chart.  It's not exactly a pie chart, as the pie wedges are triangles,
#not true circle sectors, but it's close enough for a large number of
#slices.  Code should be easily modifiable to make more circular
#objects for low number of slices.

########################################################################
# config
########################################################################
my $PI = 3.1415926535897932384626433832795;

my $slices  = 134;         # number of pie slices

my $ccw     = -1;          # 1 is counter-clockwise, -1 is clockwise

my $phase   = -$PI / 2.0;  # phase shift of the starting point
                           # -$PI / 2.0 starts slice enumeration at the
                           # 12 o'clock position

########################################################################
# generate wkt coords of slices
########################################################################
for my $i (1 .. $slices) {
  my $xy1
    = sprintf(
        "%5f %5f"
        , cos (2 * $PI * ($i - 1) * $ccw / $slices - $phase)
        , sin (2 * $PI * ($i - 1) * $ccw / $slices - $phase)
      );
  my $xy2
    = sprintf(
        "%5f %5f"
        , cos (2 * $PI * $i * $ccw / $slices - $phase)
        , sin (2 * $PI * $i * $ccw / $slices - $phase)
      );

  print "insert into temp values"
        ,"($i,GeometryFromText('POLYGON ((0 0, $xy1, $xy2, 0 0))',-1));"
        , "\n";
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapserver-users/attachments/20080110/8b5a42bf/attachment.html


More information about the mapserver-users mailing list