I have a chart in an applet which is obtaining data from our database. I am querying a time period of 7 days, 5 of which have data. If I let the chart auto range the data then it plots the 5 days along the x axis and the chart looks normal. However, if I specifically set the maximum and minimum dates for the x axis (the 7 days that were queried) then the data is not plotted and I end up with an empty chart.
Here is the code:
Code: Select all
TimeSeries ts = new TimeSeries(info.getInstrumentName());
while(secondIterator.hasNext()) {
int second = ((Integer) secondIterator.next()).intValue();
int minute = ((Integer) minuteIterator.next()).intValue();
int hour = ((Integer) hourIterator.next()).intValue();
int day = ((Integer) dayIterator.next()).intValue();
int month = ((Integer) monthIterator.next()).intValue();
int year = ((Integer) yearIterator.next()).intValue();
Second aTime = new Second(second,minute,hour,day,month,year);
Double aValue = (Double) valueIterator.next();
if(aTime != null) {
ts.addOrUpdate(aTime, aValue);
}
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(ts);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"",
"Time [UTC]",
info.getSensorTypeName() + " [" + info.getSensorUofM() + "]", // y-axis label
dataset,
false,
true,
false);
// chart title
TextTitle title = new TextTitle("Instrument: " + info.getInstrumentName());
title.setHorizontalAlignment(HorizontalAlignment.LEFT);
chart.setTitle(title);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setNoDataMessage("No data");
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(false);
renderer.setShapesFilled(false);
// line color
renderer.setPaint(info.getSensorColor());
// horizontal axis - date
DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
domainAxis.setTickLabelsVisible(true);
domainAxis.setDateFormatOverride(new SimpleDateFormat("dd hh:mma"));
domainAxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR,tickInterval));
domainAxis.setVerticalTickLabels(true);
//whenever one or both of these lines below is uncommented, data isn't plotted
//domainAxis.setMaximumDate(lastDate);
//domainAxis.setMinimumDate(firstDate);
// Vertical axis
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
HashMap<String, Double> minMaxValues = info.getMinMaxValues();
if (minMaxValues.get("min") != null) {
rangeAxis.setLowerBound(minMaxValues.get("min"));// Math.floor(minMaxValues.get("min")));
}
if (minMaxValues.get("max") != null) {
rangeAxis.setUpperBound(minMaxValues.get("max")); // Math.ceil(minMaxValues.get("max")));
}
PanningChartPanel panel = new PanningChartPanel(chart);