I have a XYStepChart with XYDataset and TimeSeries and I want to show the Status of an item (0 or 1) in a fixed range before the current time.
Sometimes the status does not change for maybe a minute.
Curretly I created a Timer to always get the last status from a "database" and add this status with the current time to the chart.
This works but I want to avoid to read every second the "database" / arraylist / ... (of course I can also store the last status in a variable)
Is it possible that JFreeChart automatically update the domain axis every second with the last status ?
Code: Select all
private TimeSeries series = new TimeSeries( "----" );
private JFreeChart timechart = null;
private TimerTask timerTask;
final Timer timerTS12 = new Timer();
.....
private void drawChart(String chartTitle ) {
timechart = ChartFactory.createXYStepChart(
chartTitle,
"Time Line",
"Status",
createDataset( ),
PlotOrientation.VERTICAL,
false,
false,
false);
timechart.getTitle().setPaint(Color.CYAN);
timechart.setBackgroundPaint(Color.BLACK);
timechart.getXYPlot().setBackgroundPaint(Color.BLACK);
timechart.getXYPlot().getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
changeAutoRange();
final XYPlot plot = timechart.getXYPlot();
plot.getRenderer().setSeriesPaint(0, Color.CYAN);
ChartPanel chartPanel = new ChartPanel( timechart );
chartPanel.setPreferredSize( new java.awt.Dimension( this.panel_1.getWidth() , this.panel_1.getHeight() ) );
this.panel_1.removeAll();
this.panel_1.add(chartPanel);
pack( );
}
private void startTimerTask(){
this.timerTask = new TimerTask() {
public void run() {
// Here I can read the last status but I want to avoid this - status 1 is only for demonstration
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( (int) (System.currentTimeMillis() / 1000L) ) )) , 1 );
}
};
this.timerTS12.scheduleAtFixedRate(this.timerTask, 1000, 1000);
}
private void changeAutoRange() {
double interval = 5.0;
if(this.rdbtn15.isSelected()) interval = 15.0;
if(this.rdbtn10.isSelected()) interval = 10.0;
if(this.rdbtn5.isSelected()) interval = 5.0;
if(this.rdbtn3.isSelected()) interval = 3.0;
if(this.rdbtn1.isSelected()) interval = 1.0;
timechart.getXYPlot().getDomainAxis().setFixedAutoRange(interval * 1000 * 60);
}
private XYDataset createDataset( ) {
final XYDataset dataset=( XYDataset )new TimeSeriesCollection(series);
series.clear();
// get already expired data to show something
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606760 ) )) , 0 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606764) )) , 1 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606778 ) )) , 0 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606780 ) )) , 1 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606784 ) )) , 0 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606785 ) )) , 1 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606797 ) )) , 0 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606799 ) )) , 1 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606821 ) )) , 0 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606823 ) )) , 1 );
series.addOrUpdate(new Second(Date.from( Instant.ofEpochSecond( 1514606827 ) )) , 0 );
return dataset;
}