Custom Labels on Domain Axis

A free public discussion forum for the JFreeChart class library.

Custom Labels on Domain Axis

Postby the-mod23 » Mon Jul 19, 2010 12:27 am

Hi,

I have a chart with 7500 Objects and I want that on the domain Axis every 150 items shown as 1,2 and so on.
I have tried it with a ValueAxis and a NumberTickUnit(150), so the chart shows a label every 150 items but not starting with 1.

This is what I have tried

Code: Select all
ValueAxis axis = plot.getDomainAxis();
((NumberAxis) axis).setTickUnit(new NumberTickUnit(150));
axis.setLabel("Generationen");
axis.setTickLabelsVisible(true);
plot.setDomainAxis(axis);



Can you please help me that it works?


regards
the-mod23
 
Posts: 5
Joined: Mon Jul 19, 2010 12:19 am

Re: Custom Labels on Domain Axis

Postby skunk » Mon Jul 19, 2010 1:18 am

Either subclass the axis and override this method to return whatever tick labels you need
Code: Select all
public abstract java.util.List refreshTicks(java.awt.Graphics2D g2,
                                            AxisState state,
                                            java.awt.geom.Rectangle2D dataArea,
                                            org.jfree.ui.RectangleEdge edge)

Or write a custom formatter and install it using
Code: Select all
public void setNumberFormatOverride(java.text.NumberFormat formatter)
skunk
 
Posts: 974
Joined: Thu Jun 02, 2005 10:14 pm
Location: Boston, USA

Re: Custom Labels on Domain Axis

Postby the-mod23 » Mon Jul 19, 2010 11:07 am

Hi skunk,

thx for the fast reply.
I´m sorry that I have needle you. :wink:

I tired your suggestions but it don´t work :(

First with the overridden Method.

Code: Select all

public class CustomAxis extends ValueAxis{

   protected CustomAxis(String label, TickUnitSource standardTickUnits) {
      super(label, standardTickUnits);
      // TODO Auto-generated constructor stub
   }

   /**
    *
    */
   private static final long serialVersionUID = 7497989746183508906L;

   @Override
   protected void autoAdjustRange() {
      // TODO Auto-generated method stub
      
   }

   @Override
   public double java2DToValue(double arg0, Rectangle2D arg1,
         RectangleEdge arg2) {
      // TODO Auto-generated method stub
      return 0;
   }

   @Override
   public double valueToJava2D(double arg0, Rectangle2D arg1,
         RectangleEdge arg2) {
      // TODO Auto-generated method stub
      return 0;
   }

   @Override
   public void configure() {[img][/img]
      // TODO Auto-generated method stub
      
   }

   @Override
   public AxisState draw(Graphics2D arg0, double arg1, Rectangle2D arg2,
         Rectangle2D arg3, RectangleEdge arg4, PlotRenderingInfo arg5) {
      // TODO Auto-generated method stub
      return null;
   }

   @Override
   public List refreshTicks(Graphics2D arg0, AxisState arg1, Rectangle2D arg2,
         RectangleEdge arg3) {
      
      List<String> test=new ArrayList<String>();
      test.add("test");
   
      return test;
   }



}


but I get only a white Chart.


And then I tried the Formatter.
But Formatter used only to formating the ouput and don´t manipulate


Code: Select all
NumberAxis xAxis = new NumberAxis();
           xAxis.setTickUnit(new NumberTickUnit(150));
           DecimalFormat format = (DecimalFormat)DecimalFormat.getNumberInstance(Locale.ENGLISH);
           format.applyPattern("#");
           xAxis.setNumberFormatOverride(format);
           plot.setDomainAxis(xAxis);



Here I have a Screenshot.
The red labels show how I want it.

Image
the-mod23
 
Posts: 5
Joined: Mon Jul 19, 2010 12:19 am

Re: Custom Labels on Domain Axis

Postby skunk » Mon Jul 19, 2010 1:38 pm

skunk wrote:Or write a custom formatter and install it using
Code: Select all
public void setNumberFormatOverride(java.text.NumberFormat formatter)

Try this
Code: Select all
axis.setNumberFormatOverride(new DecimalFormat() {
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
        return toAppendTo.append((int)(number / 150));
    }
});
skunk
 
Posts: 974
Joined: Thu Jun 02, 2005 10:14 pm
Location: Boston, USA

Re: Custom Labels on Domain Axis

Postby the-mod23 » Mon Jul 19, 2010 7:53 pm

Hi skunk,


you are my man :)

Since 3 days I sit hours for hours in front of my laptop and now
It works, it works, yeah.

thank you so much.


Greetings from Germany
the-mod23
 
Posts: 5
Joined: Mon Jul 19, 2010 12:19 am

Re: Custom Labels on Domain Axis

Postby JackBristow » Tue Jul 20, 2010 12:35 pm

Hi!
I have a problem seems like yours.
I have a chart with 5000 objects and i want to reperesent domain axis with values each 500 objects.
I'm using "DefaultCategoryDataset" and CategoryPlot, and the code used by you is not working:
Code: Select all
ValueAxis axis = plot.getDomainAxis();
((NumberAxis) axis).setTickUnit(new NumberTickUnit(150));
axis.setLabel("Generationen");
axis.setTickLabelsVisible(true);
plot.setDomainAxis(axis);



Best regards.
JackBristow
 
Posts: 3
Joined: Tue Jul 20, 2010 12:16 pm

Re: Custom Labels on Domain Axis

Postby the-mod23 » Tue Jul 20, 2010 5:18 pm

you need also this

Code: Select all
axis.setNumberFormatOverride(new DecimalFormat() {
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
        return toAppendTo.append((int)(number / 150));
    }
});



And you have to replace the 150 with 500, also at the TickUnit


regards
the-mod23
 
Posts: 5
Joined: Mon Jul 19, 2010 12:19 am

Re: Custom Labels on Domain Axis

Postby JackBristow » Tue Jul 27, 2010 10:47 am

Hi, I'm sorry to answer you so late, but I've had problems with my internet conexion.
How have I to use your code? I don't understand. I have this:
Code: Select all
graphic1 = ChartFactory.createLineChart("Graphic", "Samples","Error", dataset,PlotOrientation.VERTICAL, true, true, true);
final CategoryPlot plot = graphic1.getCategoryPlot();

Thank you,
bye.
JackBristow
 
Posts: 3
Joined: Tue Jul 20, 2010 12:16 pm

Re: Custom Labels on Domain Axis

Postby the-mod23 » Fri Jul 30, 2010 9:23 pm

Hi,

i don´t know if it works for every kind of charts, but try this.
Instanziate the plot without final.

Code: Select all
graphic1 = ChartFactory.createLineChart("Graphic", "Samples","Error", dataset,PlotOrientation.VERTICAL, true, true, true);
CategoryPlot plot = graphic1.getCategoryPlot();
ValueAxis axis = plot.getDomainAxis();
((NumberAxis) axis).setTickUnit(new NumberTickUnit(500));

axis.setNumberFormatOverride(new DecimalFormat() {
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
        return toAppendTo.append((int)(number / 500));
    }
});

axis.setLabel(<set the label of the axis>);
axis.setTickLabelsVisible(true);
plot.setDomainAxis(axis);
the-mod23
 
Posts: 5
Joined: Mon Jul 19, 2010 12:19 am

Re: Custom Labels on Domain Axis

Postby paradoxoff » Sat Jul 31, 2010 9:40 am

JackBristow wrote:Hi, I'm sorry to answer you so late, but I've had problems with my internet conexion.
How have I to use your code? I don't understand. I have this:
Code: Select all
graphic1 = ChartFactory.createLineChart("Graphic", "Samples","Error", dataset,PlotOrientation.VERTICAL, true, true, true);
final CategoryPlot plot = graphic1.getCategoryPlot();

Thank you,
bye.

You are using a CategoryPlot for which the domain axis is a CategoyAxis. The labels along a CategoryAxis are created from the column keys of the respective CategoryDataset.
Given the large number of categories, you should better switch to an XYPlot.
paradoxoff
 
Posts: 826
Joined: Sat Feb 17, 2007 1:51 pm


Return to JFreeChart - General

Who is online

Users browsing this forum: MSN [Bot] and 3 guests