Search code examples
androidandroidplot

How to change domain axis labeles in android plot?


In my android plot, the domain values(that is the values displayed on the scale of the X axis) displayed currently are 0,1,2,3..... But I wanted a different order that is neither increasing or decreasing. The order that I want is 0, 26, 33, 12, 9 6, 23 ....(I already know this order and it is does not change.) How to I change the values displayed on the axis?


Solution

  • The easiest way is to define a mapping of labels that corresponds to each index. For example if your longest series consists of 6 elements:

    // domain label mapping 
    final String[] domainMap =  {"111", "222", "333", "444", "555", "666"};
    
    ...
    
    plot.setDomainValueFormat(new NumberFormat() {
        @Override
        public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {
            return new StringBuffer(domainMap[(int) value]);
        }
    
        @Override
        public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) {
            return null;
        }
    
        @Override
        public Number parse(String string, ParsePosition position) {
            return null;
        }
    });