Tuesday, May 14, 2013

Jfreechart - Tips

Set a FormatOverride on the axis for NumberAxis and a DateAxis

 
XYPlot plot = (XYPlot) chart.getPlot();

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setNumberFormatOverride( new NumberFormat(){

    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
        return new StringBuffer(String.format("%f", number));
    }

    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
        return new StringBuffer(String.format("%f", number));
    }

    @Override
    public Number parse(String source, ParsePosition parsePosition) {
        return null;
    }

} );
DateAxis domainAaxis = (DateAxis) plot.getDomainAxis();
domainAaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
 

Range Axis Number Format from 100000 to 100k

rangeAxis.setNumberFormatOverride(new DecimalFormat("### K")); 
 
 
  

Reference:

http://stackoverflow.com/questions/11563087/jfreechart-how-to-display-hours-format-on-the-y-axis/11564855#11564855 

0 comments:

Post a Comment