gpt4 book ai didi

java - Jasper 时间序列日期轴 : show monthly ticks but yearly tick labels

转载 作者:行者123 更新时间:2023-12-01 10:22:16 27 4
gpt4 key购买 nike

我想在日期时间 X 轴上显示每月刻度。我使用下面的代码实现了这一点。

DateAxis dateAxis = (DateAxis)chart.getXYPlot().getDomainAxis();
DateTickUnit unit = new DateTickUnit(DateTickUnit.MONTH,1);
dateAxis.setTickUnit(unit);

现在我只想显示特定月份的刻度标签(例如一月,其余月份的标签将保持空白)。

我怎样才能做到这一点?

最佳答案

您可以执行以下操作:

        DateFormat axisDateFormat = dateAxis.getDateFormatOverride();
if (axisDateFormat == null) {
axisDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
}
dateAxis.setDateFormatOverride(new SelectiveDateFormat(axisDateFormat, Calendar.MONTH, 0));

...

class SelectiveDateFormat extends DateFormat {
private final DateFormat format;
private final int dateField;
private final int fieldValue;

public SelectiveDateFormat(DateFormat format, int dateField, int fieldValue) {
this.format = format;
this.dateField = dateField;
this.fieldValue = fieldValue;
}

@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
Calendar calendar = Calendar.getInstance(format.getTimeZone());
calendar.setTime(date);
int value = calendar.get(dateField);
if (value == fieldValue) {
format.format(date, toAppendTo, fieldPosition);
}
return toAppendTo;
}

@Override
public Date parse(String source, ParsePosition pos) {
return format.parse(source, pos);
}
}

这有点老套,但乍一看我没有看到其他更优雅的解决方案。

关于java - Jasper 时间序列日期轴 : show monthly ticks but yearly tick labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35508704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com