Jfreechart: Display weeks on x axis for value of days

独自空忆成欢 提交于 2019-12-02 10:32:05

问题


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"));


来源:https://stackoverflow.com/questions/43676446/jfreechart-display-weeks-on-x-axis-for-value-of-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!