Reputation: 491
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:
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.
Upvotes: 3
Views: 114
Reputation: 4477
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.
Upvotes: 0
Reputation: 79848
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
Upvotes: 1