I'm trying to create a bar chart from data that's held in a .txt
file. Currently it displays, but is missing the last row of the text file. There are 18 rows in my text file, but my bar chart is only showing data for the first 17. Here is my code for creating the chart.
public class BarChart {
/**
* declare variables
*/
public static final char DELIMITER = ',';
public static final char EOLN = '\n';
public static final String QUOTE = "\"";
public static final String USERDIRECTORY = System.getProperty("user.dir");
String part2File = USERDIRECTORY + "//GCCCarParkData.txt";
BSTMaster bst = new BSTMaster();
/**
* creates the data set from getters
* from an object
*/
public void main() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
try (BufferedReader br = new BufferedReader(new FileReader(part2File))) {
String id;
String location;
int occupancy;
int capacity;
int percentOccupancy = 0;
double latitude;
double longitude;
String[] temp;
String line = br.readLine();
while (line != null) {
temp = line.split(Character.toString(DELIMITER));
id = temp[0];
location = temp[1];
occupancy = Integer.parseInt(temp[2]);
capacity = Integer.parseInt(temp[3]);
latitude = Double.parseDouble(temp[4]);
longitude = Double.parseDouble(temp[5]);
BSTMasterP theBSTP = new BSTMasterP(id, location, occupancy, capacity, percentOccupancy, latitude, longitude);
dataset.addValue(theBSTP.getPercentOccupancy(), "", theBSTP.getLocation());
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(AppController.class.getName()).log(Level.INFO, null, ex);
}
createChart(dataset);
}
/**
*
* @param dataset
* constructs the bar chart with the dataset
*/
public void createChart(DefaultCategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart("GCC Car Park Capacity", "", "Percentage of Spaces occupied", dataset,
PlotOrientation.VERTICAL, false, true, false);
org.jfree.chart.axis.CategoryAxis axis = chart.getCategoryPlot().getDomainAxis();
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
chart.setBackgroundPaint(Color.white);
chart.getTitle().setPaint(Color.blue);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.black);
ChartFrame frame1 = new ChartFrame("Bar Chart", chart);
frame1.setVisible(true);
frame1.setSize(1500, 1000);
}
}`
here is my current bar chart:
and here is my .txt
file. As you can see, it skips the last row (the bar chart uses the second item in each row as the location and the Occupancy Percentage is calculated in the model class which is then used for the height of each bar.
CPG25C_1,SECC,405,1600,55.85988985,-4.28234137
CPG24C,Duke Street,64,1170,55.85966176,-4.23652876
CPG21C,Dundasvale,41,85,55.86916776,-4.25881365
CPG15C,Royal Infirmary,650,750,55.86548682,-4.23497772
CPG14C,George Street,124,290,55.86123812,-4.24472162
CPG13C,High Street,45,133,55.85977254,-4.23933165
CPG12C,The Glasshouse,245,510,55.85837192,-4.24861509
CPG11C,Candleriggs,376,430,55.85775922,-4.24525598
CPG10C,Waterloo Street,145,660,55.86035417,-4.26402295
CPG09C,Sauchiehall Street,56,100,55.86463876,-4.26024478
CPG08C,Mitchell Street,95,220,55.86007767,-4.2560806
CPG07C,Charing Cross,35,433,55.86473086,-4.26891253
CPG06C,Cadogan Square,58,325,55.86007563,-4.26694714
CPG05C,Shields Road,57,814,55.85023584,-4.27319591
CPG04C,Buchanan Galleries,28,2000,55.86379622,-4.24982359
CPG03C,Cambridge Street,145,812,55.86639253,-4.25822095
CPG02C,Concert Square,38,592,55.86577732,-4.25255933
CPG01C,Dundasvale,148,375,55.86786267,-4.25793868