1

我想弄清楚如何根據乘數動態地格式化我的折線圖的座標軸上的值。JFreeChart LogAxis頻率/電阻乘法器

我使用LogAxis用於X軸和Y軸兩者,如下所示:

final LogAxis rangeAxis = new LogAxis(valueAxisLabel); 
    rangeAxis.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH)); 
    rangeAxis.setRange(0.01, 10.0); //10 mOhms to 10 Ohms 
    plot.setRangeAxis(rangeAxis); 

    final LogAxis domainAxis = new LogAxis(frequencyAxisLabel); 
    domainAxis.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH)); 
    domainAxis.setRange(100, 10000000); //100Hz to 10MHz 
    plot.setDomainAxis(domainAxis); 

我現在有我的Y軸的以下值:
0.01,0.1,1,10
但希望它顯示爲
規格爲10Mohm,100mOhm,1歐姆,10歐姆

和在X軸上我有
100,1000,10,000,100,000,1,000,000,10,000,000
但希望看到
的100Hz,1kHz時,爲10kHz,100kHz時爲1MHz,10MHz的

我知道你可以覆蓋在軸上使用的NumberFormat,但我還沒有找到一種方法,這樣做在NumberFormat被基於這樣的值動態覆蓋。這可能嗎?我需要延長NumberFormat才能做到這一點嗎?

編輯: 每接受的答案,我伸出NumberFormat如下(請注意,執行不完整的,而是黑客攻擊的快速演示目的爲我的老闆)

public class UnitNumberFormat extends NumberFormat 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = -1798895L; 

    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; 
    } 

} 

UnitValue如下:

public enum UnitValue { 
    HERTZ("Hz"), 

    OHMS("Ω"), 

    ; 

    private final String unit; 


    private UnitValue(String unit) 
    { 
     this.unit = unit; 
    } 


    /** 
    * @return the unit 
    */ 
    public String getUnit() 
    { 
     return unit; 
    } 
} 

回答

1

是你需要繼承NumberFormat有一個例子here

+0

謝謝。我添加了一些我的解決方案的演示代碼,以便未來的搜索者受益。 – StormeHawke