XYStepChart / XYDataset / TimeSeries

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

XYStepChart / XYDataset / TimeSeries

Post by Nisbo » Sat Dec 30, 2017 11:00 am

Hello,

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;
}

John Matthews
Posts: 423
Joined: Wed Sep 12, 2007 3:18 pm

Re: XYStepChart / XYDataset / TimeSeries

Post by John Matthews » Tue Jan 02, 2018 12:13 am

You'll likely have to poll the data source periodically, as you're doing now, or see if the data source supports some kind of change notification, for example.

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: XYStepChart / XYDataset / TimeSeries

Post by Nisbo » Tue Jan 02, 2018 8:39 am

I did a combination of both.

- at the beginning in pull all the data in the requested time frame,
- after this I implemented a method to send new data to the Chart Window
- on the chart window I store the last status in an integer
- on the chart window I have a timer to addorupdate the last value every second

Kayaris
Posts: 1
Joined: Fri Dec 29, 2017 11:20 am
antibot: No, of course not.

Re: XYStepChart / XYDataset / TimeSeries

Post by Kayaris » Mon Jan 08, 2018 10:11 am

Hi Nisbo, did you choose the 1 second value for any particular reason? Is that optimal for what you're doing or in general as well?

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: XYStepChart / XYDataset / TimeSeries

Post by Nisbo » Mon Jan 08, 2018 10:46 am

It's because I want to emulate a "Live View" and time is ticking

Post Reply