Apologies if this issue has already been addressed but I am pretty new to JFreeChart.
I am currently experimenting with a TimeSeries Chart and I have a data set with the date and time and a temperature value. The date/time information does not have a uniform interval and I think this might be confusing JFreeChart. The output result is a graph with the date in the x-axis having date values which are out of bounds, eg. 59th February and 74th March. On the otherhand ToolTipText gives correct date and time information.
Can anyone help please? Is there a way I can set limits to the date values or is there a way to manually set the date ranges?
This is my output:
http://tinyurl.com/jynwwu2
This is my source code:
Code: Select all
public class ChartDemo extends ApplicationFrame {
public ChartDemo(String title) {
super(title);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setMouseZoomable(true, false);
setContentPane(chartPanel);
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Temperature Profile","Date","Temperature",dataset,true,true,false);
XYPlot plot = (XYPlot) chart.getPlot();
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesFilled(0, true);
}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("DD MMM YY"));
return chart;
}
private static XYDataset createDataset() {
TimeSeries s1 = new TimeSeries("Part Name");
//sec,min,hr,day,mon,yr
s1.add(new Second(00, 01, 06, 01, 01, 2016), 104.0);
s1.add(new Second(00, 30, 12, 29, 01, 2016), 105.0);
s1.add(new Second(00, 15, 00, 01, 02, 2016), 37.0);
s1.add(new Second(00, 11, 23, 02, 02, 2016), 133.0);
s1.add(new Second(00, 12, 11, 16, 02, 2016), 20.0);
s1.add(new Second(00, 45, 15, 01, 03, 2016), 20.0);
s1.add(new Second(00, 01, 21, 11, 03, 2016), 86.0);
s1.add(new Second(00, 40, 17, 26, 03, 2016), 125.0);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
public static void main(String[] args) {
ChartDemo demo = new ChartDemo("Temperature Profile Chart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}