Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X-Axis LocalDate labels have wrong dates #445

Open
gzoerner opened this issue Feb 10, 2025 · 2 comments
Open

X-Axis LocalDate labels have wrong dates #445

gzoerner opened this issue Feb 10, 2025 · 2 comments

Comments

@gzoerner
Copy link

When using LocalDates for the X-Axis, the dates plotted are incorrect. The error is in the DateArraySeriesModel class.
The method: private static double[] toXSeriesArray(LocalDate[] dates, ZoneOffset zoneOffset) returns wrong dates as the conversion from LocalDate[]->Date[]-> double[] incorrectly uses the LocalDates conversion.

Here is the corrected code with commented-out printlns to show the correction.

/**
 * 
 * @param dates
 * @param zoneOffset
 * @return double[] of dates
 */
private static double[] toXSeriesArray(LocalDate[] dates, ZoneOffset zoneOffset) {

	double[] xSeries = new double[dates.length];
	for(int i = 0; i < xSeries.length; i++) {
		/*
		System.out.println("DateArraySeriesModel- date (original data) = " + dates[i]);
		
		// original code
		xSeries[i] = dates[i].atStartOfDay().toEpochSecond(zoneOffset);
		System.out.println("DateArraySeriesModel- LocalDate (wrong) = " +
				new Date((long) (xSeries[i])).toString());
		*/
		
		// corrected code
		// Date date = Date.from(dates[i].atStartOfDay(ZoneId.systemDefault()).toInstant());
		Date date = Date.from(dates[i].atStartOfDay(zoneOffset).toInstant());
		xSeries[i] = (double)(date.getTime());
		/*
		System.out.println("DateArraySeriesModel- LocalDate (corrected) = " +
				new Date((long) (xSeries[i])).toString());
		*/
	}
	return xSeries;
}
@eselmeister
Copy link
Contributor

Nice catch. Could you please create a Pull Request?

@gzoerner
Copy link
Author

gzoerner commented Feb 12, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants