My goal is to write a small program that reaches into our Public Safety database and retrieves ambulance response times. Then I want to take that data and generate a histogram. I am having a very difficult time getting JFreechart to print a histogram of ambulance response times. When I run my code Netbeans generates an error message stating:
Exception in thread "main" java.lang.ClassCastException: org.jfree.data.jdbc.JDBCCategoryDataset cannot be cast to org.jfree.data.xy.IntervalXYDataset at EMSResearch.histogram1.main(histogram1.java:39) C:\Users\kulpandm\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 7 seconds)
I have absolutely no idea how to solve this issue. If I can get a histogram to print out I can then move on and hack away at making it look good for my reports. Shown below is my code:
package EMSResearch;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.data.xy.IntervalXYDataset;
public class histogram1
{
public static void main(String[] args) throws ClassNotFoundException
{
try
{
Connection conn = null;
conn = DriverManager.getConnection("jdbc:sqlserver://MyURL;database=MyDatabase;integratedsecurity=false;user=MyUser;password=MyPassword");
//dataset = new JDBCCategoryDataset(conn);
String query = "SELECT PUN_UnitID\n"
+ ", DATEDIFF(SECOND, IIU_tenroute, IIU_tArrive) AS [ResponseTime]\n"
+ "FROM IIncidentUnitSummary\n"
+ "WHERE IIU_tDispatch > 'may 15, 2016'\n"
+ "AND PUN_UnitID LIKE 'M[0-4]_'\n"
+ "AND IIU_tEnroute IS NOT NULL\n"
+ "AND IIU_tArrive IS NOT NULL\n"
+ "AND DATEDIFF(SECOND, IIU_tEnroute, IIU_tArrive) BETWEEN 0 AND 1800";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(conn, query);
dataset.executeQuery(query);
conn.close();
//the following blows up.
JFreeChart chart;
chart = ChartFactory.createHistogram("data", "ws range", "freq", (IntervalXYDataset) dataset, PlotOrientation.VERTICAL, true, false, false);
int width = 560;
int height = 370;
File histChart = new File("c:\\histChart2.jpeg");
try
{
ChartUtilities.saveChartAsJPEG(histChart, chart, width, height);
} catch (IOException ex)
{
Logger.getLogger(chart2.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
Can you please show me what I am doing incorrectly? What can I do to fix this? (please be specific since I am not an expert programmer).