0

I am developing a java application on netbeans.

I have created a method for bar graph using a JTable.

The problem is the bar graph is not working as expected.

Here is the code for JTable :

public void populateTable(){
    DefaultTableModel model = (DefaultTableModel) Table.getModel();

    model.setRowCount(0);
    for (WorkRequest request: account.getWorkQueue().getWorkRequestList()){

        if(request instanceof SmartWatchWorkRequest){
        Object[] row = new Object[7];

        String condition = ((SmartWatchWorkRequest)request).getCondition();
        row[0] = request;
        int respiratory = ((SmartWatchWorkRequest)request).getRespiratoryRate();
        row[1] = respiratory;
        int heart = ((SmartWatchWorkRequest)request).getHeartRate();
        row[2] = heart;
        int blood = ((SmartWatchWorkRequest)request).getBloodPressure();
        row[3] = blood;
        float weight = ((SmartWatchWorkRequest)request).getWeight();
        row[4] = weight;
        row[5] = request.getDate();
        row[6] = request.getTime();

        model.addRow(row);
        }
    }
}

Here is the code for bar graph:

private void createChart()
{
    DefaultCategoryDataset vitalSignDataset=new DefaultCategoryDataset();
    int selectedRow = Table.getSelectedRow();
    WorkRequest request = (WorkRequest) Table.getValueAt(selectedRow, 0);

    ArrayList<WorkRequest> List = account.getWorkQueue().getWorkRequestList();

    for (WorkRequest vitalSign : List) {
        if(vitalSign instanceof SmartWatchWorkRequest){
        vitalSignDataset.setValue(((SmartWatchWorkRequest)vitalSign).getRespiratoryRate(),"RR", ((SmartWatchWorkRequest)vitalSign).getDate());
        vitalSignDataset.setValue(((SmartWatchWorkRequest)vitalSign).getHeartRate(),"HR", ((SmartWatchWorkRequest)vitalSign).getDate());
        vitalSignDataset.setValue(((SmartWatchWorkRequest)vitalSign).getBloodPressure(),"BP", ((SmartWatchWorkRequest)vitalSign).getDate());
        vitalSignDataset.setValue(((SmartWatchWorkRequest)vitalSign).getWeight(),"WT", ((SmartWatchWorkRequest)vitalSign).getDate());
        }
        JFreeChart vitalSignChart= ChartFactory.createBarChart3D("Vital Sign Chart", "Time Stamp", "Rate", vitalSignDataset, PlotOrientation.VERTICAL, true, false, false);
    vitalSignChart.setBackgroundPaint(Color.white);
    CategoryPlot vitalSignChartPlot = vitalSignChart.getCategoryPlot();
    vitalSignChartPlot.setBackgroundPaint(Color.lightGray);  


    CategoryPlot P=vitalSignChart.getCategoryPlot();
    P.setRangeGridlinePaint(Color.BLUE);

    ChartFrame frame=new ChartFrame("Vitalsign Bar Chart", vitalSignChart);
    frame.setVisible(true);
    frame.setSize(500, 500);

}}
Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Uday Verma
  • 99
  • 9

1 Answers1

0

First of all , your rows are not same kind of object. the first row is a WorkRequest object , your second row is an integer etc. Also you should have got a ClassCastException during runtime in the line WorkRequest request = (WorkRequest) Table.getValueAt(selectedRow, 0); when you select any row other than the first one. Usually model contains a list of similar objects.

SomeDude
  • 13,876
  • 5
  • 21
  • 44