我自己找到了解决方案,我在自定义 TimeChart 类中覆盖了 drawXLabels 方法。
private class CustomTimeChart extends TimeChart {
public CustomTimeChart(XYMultipleSeriesDataset dataset,
XYMultipleSeriesRenderer renderer) {
super(dataset, renderer);
}
@Override
protected void drawXLabels(List<Double> xLabels,
Double[] xTextLabelLocations, Canvas canvas, Paint paint,
int left, int top, int bottom, double xPixelsPerUnit,
double minX, double maxX) {
int length = xLabels.size();
if (length > 0) {
boolean showLabels = mRenderer.isShowLabels();
boolean showGridY = mRenderer.isShowGridY();
DateFormat format = new SimpleDateFormat("MMMM");
for (int i = 0; i < length; i++) {
long label = Math.round(xLabels.get(i));
float xLabel = (float) (left + xPixelsPerUnit
* (label - minX));
if (showLabels) {
paint.setColor(mRenderer.getXLabelsColor());
canvas.drawLine(xLabel, bottom, xLabel, bottom
+ mRenderer.getLabelsTextSize() / 3, paint);
drawText(canvas, format.format(new Date(label)).substring(0, 1).toUpperCase(Locale.getDefault()),
xLabel, bottom + mRenderer.getLabelsTextSize()
* 4 / 3, paint,
mRenderer.getXLabelsAngle());
}
if (showGridY) {
paint.setColor(mRenderer.getGridColor());
canvas.drawLine(xLabel, bottom, xLabel, top, paint);
}
}
}
drawXTextLabels(xTextLabelLocations, canvas, paint, true, left,
top, bottom, xPixelsPerUnit, minX, maxX);
}
还要确保在 XYMultipleSeriesRenderer 实例上使用 setXLabels(int i) 来设置要在 x 轴上显示的月份数。