我有一个TimeChart和初始日期格式是" MM-dd-yyyy"如下。
mChart = ChartFactory.getTimeChartView(getActivity(), mDataset, mRenderer,"MM-dd-yyyy");
现在我希望格式为" MMM-yy"来自" MM-dd-yyyy"没有调用" ChartFactory.getTimeChartView"方法再次创建整个图表视图。
这可能吗?
答案 0 :(得分:1)
您无法直接执行此操作,但还有其他一些解决方案,例如自定义标签。
答案 1 :(得分:1)
我有一个改变格式的解决方案。
private static final int NUM_LABELS = 5;
private XYMultipleSeriesRenderer mMultiRenderer;
private XYMultipleSeriesDataset mDataset;
private GraphicalView mChart;
private void applyCustomXLabels(String format, boolean useFormat) {
// Get the min and max (dates) in the current 'window'
double min = mMultiRenderer.getXAxisMin();
double max = mMultiRenderer.getXAxisMax();
// Remove any custom x labels
mMultiRenderer.clearXTextLabels();
// If not using the custom format, allow the labels to be auto generated
// using the default label generator
if (!useFormat) {
// Set the number of auto-generated labels
mMultiRenderer.setXLabels(NUM_LABELS);
} else {
// Get evenly spaced label locations between min and max
List<Double> labelLocations = MathHelper.getLabels(min, max, NUM_LABELS);
// Go through each location
for(Double d : labelLocations) {
// Add a custom x label using the double as the location and format it to a date
mMultiRenderer.addXTextLabel(d, new SimpleDateFormat(format).format(new Date(Math.round(d))));
}
// Remove the auto-generated x labels
mMultiRenderer.setXLabels(0);
}
// May or may not be needed
mChart.repaint();
}
现在,请记住,一旦调用它,一旦缩放或平移它将毫无用处。为了使它有用,请实现以下内容:
private static final String NEW_LABEL_FORMAT = "MMM-yy";
private class ChartZoomListener implements ZoomListener {
@Override
public void zoomApplied(ZoomEvent zoomEvent) {
applyCustomXLabels(NEW_LABEL_FORMAT, true);
}
@Override
public void zoomReset() { }
}
private class ChartPanListener implements PanListener {
@Override
public void panApplied() {
applyCustomXLabels(NEW_LABEL_FORMAT, true);
}
}
private void setupChart(){
// Getting a reference to LinearLayout
LinearLayout chartContainer = (LinearLayout) getView().findViewById(R.id.trends_chart_container);
mChart = ChartFactory.getTimeChartView(getActivity().getBaseContext(), mDataset, mMultiRenderer, "MM-dd-yyyy");
chartContainer.addView(mChart);
mChart.addZoomListener(new ChartZoomListener(), true, true);
mChart.addPanListener(new ChartPanListener());
}