number formatting - JFreeChart LogAxis Frequency/Resistance multipliers -
i'm trying figure out how dynamically format values on axes of line chart based on multiplier.
i'm using logaxis both x , y axes, follows:
final logaxis rangeaxis = new logaxis(valueaxislabel); rangeaxis.setstandardtickunits(logaxis.createlogtickunits(locale.english)); rangeaxis.setrange(0.01, 10.0); //10 mohms 10 ohms plot.setrangeaxis(rangeaxis); final logaxis domainaxis = new logaxis(frequencyaxislabel); domainaxis.setstandardtickunits(logaxis.createlogtickunits(locale.english)); domainaxis.setrange(100, 10000000); //100hz 10mhz plot.setdomainaxis(domainaxis);
i have following values on y axis:
0.01, 0.1, 1, 10
display as
10mohm, 100mohm, 1ohm, 10ohm
and on x axis have
100, 1,000, 10,000, 100,000, 1,000,000, 10,000,000
see
100hz, 1khz, 10khz, 100khz, 1mhz, 10mhz
i know can override numberformat
used on axis, haven't found way numberformat
overridden dynamically based on value this. possible? need extend numberformat
this?
edit: per accepted answer, extended numberformat
follows (note implementation isn't complete rather hacked quick demo purposes boss)
public class unitnumberformat extends numberformat { /** * */ private static final long serialversionuid = -8544764432101798895l; private unitvalue unitvalue; public unitnumberformat(unitvalue unitvalue) { super(); this.unitvalue = unitvalue; } /* * (non-javadoc) * @see java.text.numberformat#format(double, java.lang.stringbuffer, * java.text.fieldposition) */ @override public stringbuffer format(double number, stringbuffer toappendto, fieldposition pos) { stringbuffer formattedvalue = new stringbuffer(); bigdecimal bd = new bigdecimal(number); bigdecimal multiplier = new bigdecimal(1); string multiplierstring = ""; if(number < 1 && number > 0) { multiplier = new bigdecimal(1000); multiplierstring = "m"; } else if(number < 1000 && number >= 1) { multiplier = new bigdecimal(1); multiplierstring = ""; } else if(number < 1000000 && number >= 1000) { multiplier = new bigdecimal(1. / 1000); multiplierstring = "k"; } else if(number < 1000000000 && number >= 1000000) { multiplier = new bigdecimal(1. / 1000000); multiplierstring = "m"; } else { throw new numberformatexception("this formatter doesn't yet support values beyond mega"); } bd = bd.multiply(multiplier).round(new mathcontext(1, roundingmode.half_up)); formattedvalue.append(bd.toplainstring()); formattedvalue.append(" "); formattedvalue.append(multiplierstring); formattedvalue.append(this.unitvalue.getunit()); return formattedvalue; } /* * (non-javadoc) * @see java.text.numberformat#format(long, java.lang.stringbuffer, * java.text.fieldposition) */ @override public stringbuffer format(long number, stringbuffer toappendto, fieldposition pos) { return null; } /* * (non-javadoc) * @see java.text.numberformat#parse(java.lang.string, * java.text.parseposition) */ @override public number parse(string source, parseposition parseposition) { return null; } }
and unitvalue
follows:
public enum unitvalue { hertz("hz"), ohms("Ω"), ; private final string unit; private unitvalue(string unit) { this.unit = unit; } /** * @return unit */ public string getunit() { return unit; } }
yes need subclass numberformat
there example here
Comments
Post a Comment