Link to home
Start Free TrialLog in
Avatar of Kennywen
Kennywen

asked on

Change X-axis label's value in TimeSeries chart (jfreechart)

Sample code as below:

XYPlot plot = chart.getXYPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 1,new SimpleDateFormat("dd-MMM HH:mm")));

the above coding will display the dd-MMM HH:mm as my X-axis label. just wonder is there anyway to display the dd-MMM as X-axis label if the dd-MMM is enter to a new date.

thanks
Avatar of Mayank S
Mayank S
Flag of India image

Do you want to change the display on some specific event or automatically, after a period of time? If so, you might need a separate thread and keep a timer.
Avatar of Kennywen
Kennywen

ASKER

axis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 1,new SimpleDateFormat("dd-MMM HH:mm")));

actually the above code will display the date and time on the X-axis label. E.g. 12-Apr 18:16, 12-Apr 19:16, 12-Apr 20:16, 12-Apr 21:16, 12-Apr 22:16 and so on. But i want to display the date only when the date is not exist recently on the X-axis label E.g. 12-Apr 18:16, 19:16, 20:16, 21:16, 22:16, 23:16, 13-Apr 00:16, 01:16 and so on. Any idea to doing above example?

thanks
Perhaps you can display the date once and then use: new SimpleDateFormat ( "HH:mm" ) ) ) ;
can you provide more code for me?

thanks
Just that:

>> axis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 1,new SimpleDateFormat("dd-MMM HH:mm")));

would become:

axis.setTickUnit ( new DateTickUnit ( DateTickUnit.HOUR, 1, new SimpleDateFormat ( "HH:mm" ) ) ) ;
>> axis.setTickUnit ( new DateTickUnit ( DateTickUnit.HOUR, 1, new SimpleDateFormat ( "HH:mm" ) ) ) ;

this will display the HH:mm as the X-axis label. but i want to display the date also when it's a new date.
Do you have some method/ means for reading the contents which have been displayed? If so, then it might be solved.
Currently i don't have any solution yet because:
>> axis.setTickUnit ( new DateTickUnit ( DateTickUnit.HOUR, 1, new SimpleDateFormat ( "HH:mm" ) ) ) ;
will set the HH:mm as the X-axis label and

>> axis.setTickUnit ( new DateTickUnit ( DateTickUnit.HOUR, 1, new SimpleDateFormat ( "dd:MMM" ) ) ) ;
will set the dd:MMM as the X-axis label

so how can i display the dd:MMM when it's a new day (E.g. 13 - Apr, 14 - Apr and so on) but display the HH:mm as the X-axis label

E.g.
12-Apr 18:16, 19:16, 20:16, 21:16, 22:16, 23:16, 13-Apr 00:16, 01:16
Try this:

Calendar cal = Calendar.getInstance () ;
String format = ( cal.get ( Calendar.HOUR ) == 0 && cal.get ( Calendar.MINUTE ) == 0 ) ? "dd-MMM HH:mm" : "HH:mm" ;

axis.setTickUnit ( new DateTickUnit ( DateTickUnit.HOUR, 1, new SimpleDateFormat ( format ) ) ) ;
tested and it display the HH:mm as my X-axis label. But the date is not appear in the x-axis.
It will display a new date when a new day will start. Set your system time to something like 23:59 and see if it prints the date when a new date starts.
X-axis label is depend on my database's data and one of my database column is the date.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial