3

I am just wondering if I could get some advice on implementing an algorithm for creating a JFreeChart. Basically, I am extracting some data from a JTable which contains information about patients. There are age categories for all patients such as 20-26, 26-30, 31-35, 35-40, 50-55, 55-60 etc. There are about 30 of them and every patient belongs to their corresponding age category. There is a button on top of the JTable which opens a frame containing the age distribution graph. What I am thinking of doing:

  1. Create an integer variable for every category
  2. Loop through the age details for all patients in the JTable
  3. Compare variables with the JTable data and increment by 1 if there is a match (lots of if statements to go in the loop)
  4. Store the categories names and the amount of people registered under every category in a HashMap
  5. Pass the map to the ChartFrame

I suppose this might be a relatively good way of doing this but I was wondering if somebody could possibly suggest a way of avoiding having to create a variable for every category and then having to do all those comparisons using about 30 if statements.

EDIT: I do not have the patient's exact age - only the category they belong to.

2 Answers 2

1

I've assumed you've got your own class AgeRange which stores a range of ages. Then what you can do is store the age ranges in a TreeMap<Integer,AgeRange>, where the key is the first number of the range and the value is the range itself.

When you need to find which age range contains a particular age, use

  theMap.lowerEntry(age + 1)

to find it.

Check the TreeMap Javadoc at http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

Sign up to request clarification or add additional context in comments.

13 Comments

Hmmm, what do you mean by the first number in the range? So if I have a category 30-35 then my key would be 30 and the value 30-35? Sorry, I don't quite understand what you are trying to say!
Yes, that's exactly what I mean. So you'll set up the map at the beginning, with key/value pairs like (key:0, value 0-4), (key:5, value 5-8), (key:9, value 9-13) and so on for all the age ranges. But the most important part is using the lowerEntry method on your TreeMap, which will give you the right age range for any integer. And you need the +1 because lowerEntry will give you the age range for the next integer below the one that you pass to it.
I think I see what you mean... I read the javadoc and some examples of lowerEntry being used and if I understood correctly: if I pass age 6+1 to lowerEntry for example it would return 5 +1 which is 6 but this does not match a key in our case? So how would that work out if there isn't a key: 6? And just to say that I don't have a class for the ages (although I can make one if necessary) so I will basically be using the age ranges direct without separating them - I am sure you have taken this into account but I am still trying to understand how your suggestion can work
The number you pass doesn't have to match a key - that's kind of the point. The Javadoc says that you'll get the entry for "... the greatest key strictly less than the given key ...", which means that if you pass 7, and you've got keys for 5 and 9 (like my example), you'll get the value corresponding to 5. But the important part to notice is the "strictly less", which means that if you pass 9, and you've got keys for 5 and 9, you'll also get the value corresponding to 5. It chooses the largest number that's there, that's less than what you pass; and that's why you need to add 1.
You can just insert a new value with a given key. So if you see, for example, that the value in the map is 3 for a particular key, you can just put a value of 4, and this will replace the previous value. The only question is how you'll represent your categories - you could have a special class for this, or you could do something like represent it as a String (with a value like "30-35").
|
0

Use the SimpleHistogramDataset class in JFreeChart. Add one SimpleHistogramBin for each of your age ranges, and then call the addObservation() method for each person's age. Once you are done, you have a working dataset - it implements the IntervalXYDataset interface so you can, for example, pass it to the createXYBarChart() method to create a histogram.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.