Author Topic: Make a Pie Chart and fill each portion  (Read 1745 times)

Offline onumad

  • Mc. Print
  • *
  • Posts: 15
    • View Profile
First of all, sorry for my bad english.

I have this code (Original from Pure Basic) that create a PieChart.
Code: (glbasic) [Select]
// --------------------------------- //
// Project: PIECHART
// Start: Monday, June 11, 2012
// IDE Version: 10.283


// SETCURRENTDIR("Media") // go to media files

SETSCREEN 400,400, 1

GLOBAL mychart = 0
PieChartMake(9,0,0,200)
CLEARSCREEN 0
DRAWSPRITE mychart,100,100
SHOWSCREEN
MOUSEWAIT

FUNCTION PieChartMake: Parts, x, y, Width

  // Initial values
  LOCAL PI# = ACOS(0)*2

  STATIC Resolution = 100     // Resolution is the part used FOR the pie-chart IN percent, the other part is used FOR the description on the right side
  STATIC Border = 0           // Border (IN pixel) on left AND right side of the pie-chart
  LOCAL AngleStart# = 0    // needed later FOR calculating the circle-parts
  LOCAL AngleEnd# = 0         // defines where the drawing starts: 0 = right, #Pi = left, #Pi/2 = bottom, -#Pi/2 = top
  LOCAL Radius#
  LOCAL parteengrados#
  LOCAL id,px%,py%
  LOCAL Height% = Width
  // Calculate initial chart values
  LOCAL LeftWidth = Width * Resolution / 100
  LOCAL temp1% = (LeftWidth ) / 2
  LOCAL temp2% = (Height) /2
   IF temp1 < temp2
    Radius = temp1
  ELSE
    Radius = temp2
  ENDIF
  LOCAL MX = x + Border + ((LeftWidth-Border) / 2)
  LOCAL MY = y + temp2


  parteengrados = (360/(Parts))

  FOR id=1 TO Parts
    AngleStart = AngleEnd
    AngleEnd = AngleStart + (parteengrados * 2 * PI / 360)

    // Set black AS DEFAULT color FOR all border lines
    //FrontColor(RGB(0,0,0))

    // Draw the lines from inside the circle TO the border
    IF id<Parts
    DRAWLINE MX,MY,COS(AngleStart)*(Radius+1)+MX,SIN(AngleStart)*(Radius+1)+MY, RGB(255,255,0) // note: Radius must be increases by 1 here,
    DRAWLINE MX,MY,COS(AngleEnd)*(Radius+1)+MX,SIN(AngleEnd)*(Radius+1)+MY, RGB(255,255,0) // because otherwise sometimes misses a pixel
    ENDIF



    // Draw the circle
    FOR a = AngleStart * Radius TO AngleEnd * Radius  //Step 2
      px = COS(a / Radius) * Radius + MX
      py = SIN(a / Radius) * Radius + MY
      SETPIXEL px, py, RGB(255,255,0)
    NEXT

    // Calc the coordinates for filling point and finally fill the selected area
    px = COS((AngleEnd + AngleStart) / 2)*(Radius / 2) + MX
    py = SIN((AngleEnd + AngleStart) / 2)*(Radius / 2) + MY
    //FillArea(px,py,0,Stats(id)\Color)
  NEXT

  GRABSPRITE mychart, x, y, Width, Height+5
 
ENDFUNCTION

My question: Are there any fast function to fill each portion of the circle with different colour? (I already searched in forum for 'flood fill', 'fill area', ..., but I can't find anything.

Thank you in advance.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline onumad

  • Mc. Print
  • *
  • Posts: 15
    • View Profile
Re: Make a Pie Chart and fill each portion
« Reply #2 on: 2012-Jun-11 »
Check this out: http://www.glbasic.com/forum/index.php?topic=4498.msg49701#msg49701
Nice!!! Thank you.

I did a fast test and it is ok!
Code: (glbasic) [Select]
DrawOval(100,100,99,99,RGB(255,0,0))
SHOWSCREEN
MOUSEWAIT


FUNCTION DrawOval%: x, y, w, h, col%

INC x, w/2
INC y, h/2

LOCAL phi
STARTPOLY -1, 0
POLYVECTOR x,y, 0,0,col
FOR phi = 0 TO 360 //STEP 5 // how poly do you want it?
IF MOD(phi,45)=0
col = RGB(RND(255),RND(255),RND(255))
ENDIF
POLYVECTOR x+COS(phi)*w, y-SIN(phi)*h, 0,0,col
NEXT
ENDPOLY

ENDFUNCTION