Well, here is what i did to solve my problem.
I extend 2 classes from JFreeChart:
Code: Select all
public class RepartDateTickUnit extends DateTickUnit {
/**
* Creates a new date tick unit. You can specify the units using one of
* the constants YEAR, MONTH, DAY, HOUR, MINUTE, SECOND or MILLISECOND.
* In addition, you can specify a unit count, and a date format.
*
* @param unit the unit.
* @param count the unit count.
* @param formatter the date formatter (defaults to DateFormat.SHORT).
*/
public RepartDateTickUnit(int unit, int count, DateFormat formatter) {
super(unit, count, unit, count, formatter);
}
/**
* Calculates the next date by adding one unit to the base date.
*
* @param base the base date.
*
* @return A new date one unit after the base date.
*/
public Date nextDate(Date base) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(base);
calendar.add(getCalendarField(getUnit()), 1);
return calendar.getTime();
}
/**
* Returns a field code (that can be used with the Calendar class) for a
* given 'unit' code. The 'unit' is one of: {@link #YEAR}, {@link #MONTH},
* {@link #DAY}, {@link #HOUR}, {@link #MINUTE}, {@link #SECOND} and
* {@link #MILLISECOND}.
*
* @param tickUnit the unit.
*
* @return The field code.
*/
private int getCalendarField(int tickUnit) {
switch (tickUnit) {
case (YEAR):
return Calendar.YEAR;
case (MONTH):
return Calendar.MONTH;
case (DAY):
return Calendar.DATE;
case (HOUR):
return Calendar.HOUR_OF_DAY;
case (MINUTE):
return Calendar.MINUTE;
case (SECOND):
return Calendar.SECOND;
case (MILLISECOND):
return Calendar.MILLISECOND;
default:
return Calendar.MILLISECOND;
}
}
}
Code: Select all
public class RepartDateAxis extends DateAxis {
/**
* Indique si on affiche les dates standards JFreeChart ou la première date de la période
*/
private boolean displayFirstDate = false;
protected List refreshTicksHorizontal(Graphics2D g2,
Rectangle2D dataArea,
RectangleEdge edge) {
List result = new java.util.ArrayList();
Font tickLabelFont = getTickLabelFont();
g2.setFont(tickLabelFont);
if (isAutoTickUnitSelection()) {
selectAutoTickUnit(g2, dataArea, edge);
}
RepartDateTickUnit unit = (RepartDateTickUnit) getTickUnit();
Date tickDate = calculateLowestVisibleTickValue(unit);
Date tickNoDate = getMinimumDate();
Date upperDate = getMaximumDate();
// float lastX = Float.MIN_VALUE;
while (tickDate.before(upperDate) || tickNoDate.before(upperDate)) {
TextAnchor anchor = null;
TextAnchor rotationAnchor = null;
double angle = 0.0;
if (isVerticalTickLabels()) {
anchor = TextAnchor.CENTER_RIGHT;
rotationAnchor = TextAnchor.CENTER_RIGHT;
if (edge == RectangleEdge.TOP) {
angle = Math.PI / 2.0;
}
else {
angle = -Math.PI / 2.0;
}
}
else {
if (edge == RectangleEdge.TOP) {
anchor = TextAnchor.BOTTOM_CENTER;
rotationAnchor = TextAnchor.BOTTOM_CENTER;
}
else {
anchor = TextAnchor.TOP_CENTER;
rotationAnchor = TextAnchor.TOP_CENTER;
}
}
// add tick with no label
while(tickNoDate.before(tickDate)){
Tick tick = new DateTick(tickNoDate, "", anchor, rotationAnchor, angle);
result.add(tick);
tickNoDate = unit.nextDate(tickNoDate);
}
tickNoDate = unit.nextDate(tickNoDate);
if (!isHiddenValue(tickDate.getTime())) {
// work out the value, label and position
String tickLabel;
DateFormat formatter = getDateFormatOverride();
if (formatter != null) {
tickLabel = formatter.format(tickDate);
}
else {
tickLabel = this.getTickUnit().dateToString(tickDate);
}
Tick tick = new DateTick(
tickDate, tickLabel, anchor, rotationAnchor, angle
);
result.add(tick);
tickDate = unit.addToDate(tickDate);
}
else {
tickDate = unit.rollDate(tickDate);
}
}
return result;
}
/**
* Calculates the value of the lowest visible tick on the axis.
*
* @param unit date unit to use.
*
* @return The value of the lowest visible tick on the axis.
*/
public Date calculateLowestVisibleTickValue(DateTickUnit unit) {
if(isDisplayFirstDate()){
return getMinimumDate();
} else {
return nextStandardDate(getMinimumDate(), unit);
}
}
/**
* @return
*/
public boolean isDisplayFirstDate() {
return displayFirstDate;
}
/**
* @param b
*/
public void setDisplayFirstDate(boolean b) {
displayFirstDate = b;
}
}
Using these classes, every ticks on the domainAxis are displayed and, when setting the boolean displayFirstDate at true, the first date displayed on the domainAxis is the first date of the period.
this thread (
http://www.jfree.org/phpBB2/viewtopic.php?t=18240) deals withthis subject too.
Dropsy