3D Pie Chart

Discussions related to graphics (2D and 3D), animation and games programming
guest

3D Pie Chart

Post by guest »

Here's a simple program to display a '3D' pie chart. Because BBC BASIC cannot draw 'elliptical sectors' it cheats slightly by initially drawing them circular and then using an intermediate bitmap to reduce the height, to give the 3D effect. The depth is generated by repeatedly drawing the chart in different vertical positions, with the colours brightened on the last one. The text labels are automatically centred within the sectors.

This is the resulting graphic; the program is listed below. It is compatible with BB4W and BBCSDL (all editions).

Image

Richard.

Code: Select all

      nSlices% = 3
      DIM Label$(nSlices%-1), Value(nSlices%-1), Colour&(nSlices%-1)
      ON ERROR OSCLI "REFRESH ON" : MODE 3 : PRINT REPORT$ : END

      COLOUR 14,&87,&CE,&FF
      COLOUR 14+128
      CLS
      IF POS REM SDL thread sync

      Label$()  = "One", "Two", "Three"
      Value()   = 3.45, 4.56, 5.67
      Colour&() = 1, 2, 3

      Value() *= 2 * PI / SUM(Value())

      Cx% = @vdu%!208
      Cy% = @vdu%!212
      Rad = 300
      Depth = 128

      *REFRESH OFF
      prev = 0
      FOR Y% = Cy% TO Cy% + Depth STEP 2
        IF Y% = Cy% + Depth Colour&() OR= 8
        FOR I% = 0 TO nSlices%-1
          GCOL Colour&(I%)
          this = Value(I%)
          PROCsector(Cx%, Y%, Rad, prev, prev + this)
          prev += this
        NEXT
      NEXT Y%
      OSCLI "GSAVE """ + @tmp$ + "pie.tmp.bmp"" "+STR$(Cx%-Rad)+","+STR$(Cy%-Rad)+","+STR$(2*Rad)+","+STR$(2*Rad+Depth)
      CLS
      *REFRESH ON
      OSCLI "DISPLAY """ + @tmp$ + "pie.tmp.bmp"" "+STR$(Cx%-Rad)+","+STR$(Cy%-Rad/2)+","+STR$(2*Rad)+","+STR$(Rad+Depth/2)

      VDU 5
      GCOL 3,15
      prev = 0
      FOR I% = 0 TO nSlices%-1
        this = Value(I%)
        MOVE Cx%+Rad/2*COS(prev+this/2)-WIDTH(Label$(I%))/2, Cy%+Rad/4*SIN(prev+this/2)+@vdu%!220+Depth/2
        PRINT Label$(I%);
        prev += this
      NEXT

      REPEAT WAIT 4 : UNTIL FALSE
      END

      DEF PROCsector(cx, cy, r, a, b)
      MOVE cx+0.5, cy+0.5
      MOVE cx+r*COSa+0.5,cy+r*SINa+0.5
      PLOT 181,cx+r*COSb+0.5,cy+r*SINb+0.5
      ENDPROC
      
roy
Posts: 31
Joined: Mon 02 Apr 2018, 15:48

Re: 3D Pie Chart

Post by roy »

Nice demo Richard ;)
Kjell
Posts: 6
Joined: Fri 13 Apr 2018, 11:58

Re: 3D Pie Chart

Post by Kjell »

admin wrote: Sun 08 Apr 2018, 16:39 Here's a simple program to display a '3D' pie chart. ...
Simple... ?
In the same way as rocket science is "simple"
guest

Re: 3D Pie Chart

Post by guest »

Kjell wrote: Fri 13 Apr 2018, 12:02Simple... ?
In the same way as rocket science is "simple"
If you think that program is not "simple" I don't know what to say. It is, after all, almost entirely using features of BBC BASIC that have remained unchanged for 30 years. If you delete the vertical resizing, to create a circular rather than elliptical pie chart, the resulting program would require very little modification to run on a 1987-vintage Acorn Archimedes!

Richard.