Homework 6
comp 125-609, Goldwasser
Due: 6:00pm Tuesday, November 9, 1999 (worth 5 points)
Late Option: if submitted between 6:00pm November 9 and 6:00pm November 16, you can recieve up to 3 of 5 points. No late homeworks will be accepted after 6:00pm November 16.
Purpose: Using Graphics
Overview: Making a pie graph

The chapter in the text book gives some very nice programs for creating line graphs and bar graphs based on data from a file. For this homework, we will design a program for creating a pie chart.

Your form should contain a large picture box, a textbox with label for getting a filename from the user, and a command button captioned "go" which causes the file to be read, and the pie chart to be drawn.


Input:

The file format is as follows:

  • The first line of the file will be a string which is the title for the graph.
  • The second line of the file will be an integer which is the number of categories contained.
  • The remainder of the file will consist of a line for each category, where each such line contains a string naming the category followed by a number, which is the total for that category.
  • A sample such file would be:
    "Population 1999"
    7
    "China",1246871951
    "India",1000848550
    "United States",272639608
    "Indonesia",216108345
    "Brazil",171853126
    "Russia",146393569
    "Other",2941500191
    
    A set of interesting datafiles is located in http://www.cs.luc.edu/~mhg/comp125/homework/hw6input/
    Output:

    You must create a pie chart with one slice for each of the categories given in the input. The size of each pie slice should be proportional to the data for that category, such that all of the categories sum to a complete circle. Also, the radius of the circle should be quite large, so that it is easy for the reader to see the figure.

    You must use a different color for each slice. You may assume that there are at most 16 categories, and so you can use the predefined colors QBColor(0), ..., QBColor(15) for this. It would be nice if you also used different FillStyle's for the slices, but this is more of an optional feature.

    To identify the slices for the user, we will place all of the category names, together with the percentages, to the far right side of the picture box (so as not to interfere with the circle). Print each name/percentage using the color that matches the correct slice so that the reader can interpret the figure. (for a more interesting way to label the slices, see the extra credit!)


    Advice:

    Before you even start with the graphics, make sure that you are able to correctly read and store the data. After reading the full data, you should make another pass through all of the data so that you can calculate the overall total of the numbers, and then convert the the appropriate percentages which you will need in designing the chart.

    Now comes the graphics. You will obviously rely very heavily on the Circle method to draw the pie slices properly. We will give a few pieces of advice, but you should refer back to the lecture notes for the full details.

    You may immediately want to create a variable twopi and set its value as twopi = 8*atn(1) as this gives you the equivalent of "360-degrees" in radians. You can calculate all of your slices based on the starting and ending percentages multiplied by twopi.

    The only other reminder is that to get the pie slices filled, you must put a negative sign in front of both the start and end parameters to the Circle method. Finally, for the very first slice with start equal to 0*twopi, placing a negative sign in front of the start will not work (as -0 = 0). Therefore as a special case you should fudge this one number and use -0.00001 instead.


    Extra Credit (1 point): We would like to incorporate the text labels directly with the figure (as opposed to a separate list on the side). Unfortunately, it is a bit of a challenge to place these labels in a way so that they are clear, and that they do not interfere with each other.

    Here is the method we suggest. For each slice, calculate the angle which is the "middle" of the slice. Draw a thin line which goes from the edge of the circle outward along that angle. At the end of this line, place the text label appropriately. If the angle is on the left half of the circle, place the text so that the entire text is to the left of the extra line you have drawn (that is the last character should end near the line). If the angle is on the right half, place the text so that the entire text is to the right of the line you have drawn (that is the first character should start near the line).

    Finally, converting from angles to (x,y) coordinates will require the use of trigonometric functions built into Visual Basic. If we assume that the center of the circle is (0,0) and that the angle we are interested is stored in radians, then the point which is d units away from the center along the direction of angle will be ( d*cos(angle), d*sin(angle) ).


    To submit your homework you should,
  • Print out your project as follows. Click "File". Click "Print". In the Range box, select "Current project". In the Print What box, click only on "Code".
  • Save both the form and the project to a floppy disk
  • Place the printouts and the floppy disk inside a large manilla envelope. Please make sure that your name appears on the envelope as well as the disk and the printouts.