user3281466
user3281466

Reputation: 491

Working with data from table

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.

Upvotes: 3

Views: 114

Answers (2)

David Gilbert
David Gilbert

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

Dawood ibn Kareem
Dawood ibn Kareem

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

Related Questions