AutoHotkey Community

It is currently May 27th, 2012, 1:05 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Pie Chart Function
PostPosted: January 2nd, 2012, 8:45 am 
Offline

Joined: December 18th, 2009, 6:08 pm
Posts: 63
I recently had a use for a pie chart in ahk, and had trouble finding any solution for this.. So I created a pie chart function, and thought someone else may be able to use it.

Screenshot:
Image

function:PieChart(hwnd, x_coord, y_coord, diameter,segments,bordersize, showpercentages = 0)
The parameters for the function are:
hwnd of the window you wish to draw pie chart on
x coord of the pie chart (relative to the window)
y coord of the pie chart (relative to the window)
diameter of the pie chart
segments is a csv list of name|size of segments, these arent percentages, but they can be you so choose. (the function will calc the percentages otherwise)
bordersize - the thickness of the border around the ellipse, as well as between the segments, 0 = no border
showpercentages - display the percentage for each pie segment (0 for no, 1 for yes)

Example Usage:
Code:
gui, show, x300 y100 h800 w1000, piecharts
whwnd := WinExist("A")
data = Internet Explorer|20, Firefox|30, Chrome|50, Opera|20, Other|10
PieChart(whwnd,40,20,300,data,3,1)


todo - create key for chart (if anyone has a use for this)
- fix bugs that im sure this will have :roll:

edit 1 - cleaned up the % display, moved to inside pie

Function:
Code:
PieChart(hwndWin, xpos, ypos, size,segments,bordersize, showpercentages = 0)
{
   ;define colors
   color_1   := 0x008000                     ;green
   color_2 := 0x0000FF                     ;red
   color_3   := 0xFF0000                     ;blue
   color_4 := 0x00FFFF                     ;yellow
   color_5 := 0x00FF00                     ;lime
   color_6 := 0x808000                     ;teal
   color_7 := 0xFFFF00                     ;aqua
   color_8   := 0xFF00FF                     ;fuchsia
   color_9 := 0x800080                     ;purple
   color_10:= 0xFFFFFF                     ;white
   
   ;get a dc for the window
   hdcWin := DllCall("GetDC", "UInt", hwndWin)
   Pen := DllCall("CreatePen", int, 0, int, bordersize, int, 0x000000)
   DllCall("SelectObject", uint, hdcWin, uint, pen)

   ;draw ellipse to the window
   DllCall("Ellipse", uint, hdcWin, int, xpos, int, ypos, int, xpos + size, int, ypos + size)
   
   ;set variables
   radius := size / 2
   tradius := size / 2 * .8                        ;test radius
   2pi := 2*3.1415926
   center_x := xpos + size / 2
   center_y := ypos + size / 2
   dangle   := 0                                 ;set the initial angle to 0 degrees
   rangle   := dangle * .01745329252                  ;convert degrees to radians
   prev_x   := center_x + radius * cos(rangle-2pi/4)      ;find the x coord of the initial angle
   prev_y   := center_y + radius * sin(rangle-2pi/4)      ;find the y coord of the initial angle
   
   ;count the number of segments
   Loop, Parse, segments, `,
      {
         Segment_Count := A_Index
         Loop, Parse, A_Loopfield, |
            {
               If (A_Index & 1)
                  continue
               Piece_Count += A_Loopfield
            }
      }

   ;get the size of each piece
   piece_size_deg := 1 / Piece_Count * 360
   
   ;draw pie slice
   loop, parse, segments, `,
      {
         bcolor := A_Index
         if A_Index > 10                        ; only storing 10 diffent colors, if there is more pie pieces
            bcolor := A_Index - 10               ; than this, then start reusing the colors..
         
         bcolor := color_%bcolor%
         Loop, parse, A_Loopfield, |
            {
               if (A_Index & 1)
                  continue
               else
                  {
                     brush := DllCall("CreateSolidBrush", int, bcolor)
                     DllCall("SelectObject", uint, hdcWin, uint, brush)
                     if !(bordersize)
                        {   
                           Pen := DllCall("CreatePen", int, 0, int, 1, int, bcolor)
                           DllCall("SelectObject", uint, hdcWin, uint, pen)
                        }
                     dangle += piece_size_deg * A_Loopfield
                     rangle   := dangle * .01745329252
                     next_x := center_x + radius * cos(rangle-2pi/4)
                     next_y := center_y + radius * sin(rangle-2pi/4)
                     DllCall("Pie", uint, hdcWin, int, xpos, int, ypos, int, xpos + size, int, ypos + size, int, next_x, int, next_y, int, prev_x, int, prev_y)
                     ;show percentages if that parameter is set.
                     If (showpercentages)
                        {
                           segment_percent := Round((A_LoopField / piece_count) * 100,1) "%"
                           StringLen,segment_percentL, segment_percent
                           tangle := dangle - (piece_size_deg * A_LoopField) /2
                           rangle   := tangle * .01745329252
                           text_x := center_x + tradius * cos(rangle-2pi/4)
                           text_y := center_y + tradius * sin(rangle-2pi/4)
                           DllCall("SetBkColor", "Int", hdcWin, "Int", bcolor)
                           DllCall("SetTextAlign", uint, hdcWin, int, 6)
                           DllCall("TextOut", uint, hdcwin, int, text_x, int, text_y, uint, &segment_percent, int, segment_percentL)
                        }
                     
                     prev_x := next_x
                     prev_y := next_y
                     DllCall("DeleteObject", uint, brush)
                     if !(bordersize)
                        DllCall("DeleteObject", uint, pen)
                  }
            }
      }
DllCall("DeleteObject", uint, pen)
DllCall("ReleaseDC", uint, hdcWin)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2012, 9:26 am 
thanks, could be very useful!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2012, 9:36 am 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
#Thanks!
Question: Why dont you display the Names of the data, eg. Internet Explorer, Firefox, Chrome etc.....?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2012, 5:35 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
Nice;
I'm a fan of graphing functions in AHK;
"Pie" is not typically my graph of choice, but now I know where to go if/when I need it.

Thanks.

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2012, 2:38 pm 
Offline

Joined: October 27th, 2006, 10:12 am
Posts: 649
So names are not used in the plot?


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Bon, sks and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group