cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
ISeries timeSeries = chart.getSeriesSet().getSeries()[0];
int line = Arrays.binarySearch(timeSeries.getXDateSeries(), cal.getTime());
if (line >= 0)
return cal.getTime();
// otherwise: find closest existing date
line = -line - 1;
if (line == 0)
return timeSeries.getXDateSeries()[line];
int length = timeSeries.getXDateSeries().length;
if (line >= length)
return timeSeries.getXDateSeries()[length - 1];
// check which date is closer to the targeted date
long target = cal.getTimeInMillis();
Date left = timeSeries.getXDateSeries()[line - 1];
Date right = timeSeries.getXDateSeries()[line];
return target - left.getTime() < right.getTime() - target ? left : right;
}