How to set the Date Format for CombinedXYChart in AChartEngine?

若如初见. 提交于 2019-12-11 03:54:01

问题


I can set the date format in the time chart like this

final GraphicalView view = ChartFactory.getTimeChartView(context, dataset,
                mRenderer, "dd-MMM-yyyy");

but I can't do the same in the case of ComninedXYChart

String[] types = new String[] { TimeChart.TYPE , ScatterChart.TYPE, ScatterChart.TYPE};
final GraphicalView view = 
    ChartFactory.getCombinedXYChartView(context, dataset, mRenderer, types);

image below:

any ideas?


回答1:


In the CombinedXYChart case, you will have to use custom labels:

// disable the default labels
renderer.setXLabels(0);
// add several such labels
renderer.addXTextLabel(x, "label");



回答2:


Actually I have found much better approach. The trick is to use method setXLabelFormat combined with overloaded NumberFormat:

 SimpleDateFormat mDateFormatter = new SimpleDateFormat("d MMM");
 renderer.setXLabelFormat(new NumberFormat() {
        @Override
        public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {
            return mDateFormatter.format(value, buffer, field);
        }

        @Override
        public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) {
            return mDateFormatter.format(value, buffer, field);
        }

        @Override
        public Number parse(String string, ParsePosition position) {
            return mDateFormatter.parse(string, position).getTime();
        }
    });

Of course it would much bettter if method setXLabelFormat accepted Format (which is base class of all formats) instead of NumberFormat, however i think it is still elegant and quite simple sollution.



来源:https://stackoverflow.com/questions/15320739/how-to-set-the-date-format-for-combinedxychart-in-achartengine

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