Jfreechart: Display weeks on x axis for value of d

2019-02-28 09:58发布

问题:

I am using JFreeChart to display a value for each day of a month. Now I want to have my x-axis display the weeks in a month instead of the days. At the moment the values for my graph are double on the y-axis and int on the x axis.

Timestamp ts = a.getTimestamp(); Double val = a.getVal(); series1.add(ts.getDate(), val);

I am using plot.getDomainAxis().setRange(0, 31); to set the range for the days to one month and xax.setTickUnit(new NumberTickUnit(7)); for the x-axis to display the ticks at the right position. Instead of displaying the days (0, 7, 14, 21, 28) i want them to be weeks (0, 1, 2, 3, 4).

Is that even possible, and how would I be able to do that?

回答1:

You should use a DateAxis for your time axis, like ChartFactory.createTimeSeriesChart() does. Then you can use setDateFormatOverride(), like they show here, and the SimpleDateFormat for "week in month."

JFreeChart chart = ChartFactory.createTimeSeriesChart(…);
DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("W"));