I have a time series chart where domain range is auto-fixed, and due to this only part of large data-set is visible in the chart at a time. I have enabled only domain panning.
I am looking for a functionality such that when user pan in either direction, i.e. left or right, the range axis min/max values should adjust automatically as per the data visible in chart (and not as per complete data-set). Does JFreeChart-1.0.19 provide any such functionality out of the box?
On the other hand, I know that when dynamic data is added in time series (with fixed domain axis range), then setAutoAdjust on range axis will automatically adjust the min/max as per data visible on chart.
Similar question was asked here.
Please find below the sample code: TimeSeriesDemo.java
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.ui.ApplicationFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.axis.DateAxis;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Day;
import org.jfree.chart.ChartPanel;
import org.jfree.ui.RefineryUtilities;
public class TimeSeriesDemo extends ApplicationFrame {
public TimeSeriesDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Nifty 50 Close", // title
"Date", // x-axis label
"Value", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(false);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(false);
}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
axis.setFixedAutoRange(6.0 * 24 * 60 * 60 * 1000);
return chart;
}
private static XYDataset createDataset() {
TimeSeries s1 = new TimeSeries("Nifty 50");
s1.add(new Day(11,1,2017),8380.65);
s1.add(new Day(12,1,2017),8407.2);
s1.add(new Day(13,1,2017),8400.35);
s1.add(new Day(16,1,2017),8412.8);
s1.add(new Day(17,1,2017),8398);
s1.add(new Day(18,1,2017),8417);
s1.add(new Day(19,1,2017),8435.1);
s1.add(new Day(20,1,2017),8349.35);
s1.add(new Day(23,1,2017),8391.5);
s1.add(new Day(24,1,2017),8475.8);
s1.add(new Day(25,1,2017),8602.75);
s1.add(new Day(27,1,2017),8641.25);
s1.add(new Day(30,1,2017),8632.75);
s1.add(new Day(31,1,2017),8561.3);
s1.add(new Day(1,2,2017),8716.4);
s1.add(new Day(2,2,2017),8734.25);
s1.add(new Day(3,2,2017),8740.95);
s1.add(new Day(6,2,2017),8801.05);
s1.add(new Day(7,2,2017),8768.3);
s1.add(new Day(8,2,2017),8769.05);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel cp = new ChartPanel(chart);
cp.setDomainZoomable(false);
cp.setRangeZoomable(false);
return cp;
}
public static void main(String[] args) {
TimeSeriesDemo1 demo = new TimeSeriesDemo1("Time Series Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}