How to Skip labels on domain Axis when data is more

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
inder14
Posts: 11
Joined: Fri Jul 10, 2009 7:09 pm

How to Skip labels on domain Axis when data is more

Post by inder14 » Sat Jul 11, 2009 10:34 am

Hello,
i am using CombinedDomainCategoryPlot to create a graph having a line and bar together to show two different kind of data.
we have facility to show data for one year and data is shown on weekly basis where data is show from week1 to week52 of year.
user can choose multiple years in this case we show the data for all the weeks in the selected year range.

the problem that i am facing is that when user chooses two year time sapn we have 104 weeks data and label on x axis are as 'week1 y0y0/y1y1' ....

'week52 y2y2/y3y3'. though these labels are aligned at 90 degree but when teh year range is selceted more than one year the labels collide with each other which are not user readable.
do we a functionality where we can skip some of the labels in a regular interval. please let me know how to achieve the desired result.

thanks & regards
Inder

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to Skip labels on domain Axis when data is more

Post by paradoxoff » Sat Jul 11, 2009 10:44 am

The only thing I can advice is to use a CombinedDomainXYPlot. A CategoryPlot will always have problems when the number of categories is getting too high.

inder14
Posts: 11
Joined: Fri Jul 10, 2009 7:09 pm

Re: How to Skip labels on domain Axis when data is more

Post by inder14 » Sat Jul 11, 2009 10:58 am

Hi paradoxoff,
thanks a lot for your reply, can you please give an example for the same to plot a Line and Bar graph in sigle plot and skip the labels

thanks in advance

regards,
Inder

inder14
Posts: 11
Joined: Fri Jul 10, 2009 7:09 pm

Re: How to Skip labels on domain Axis when data is more

Post by inder14 » Sat Jul 11, 2009 1:26 pm

Hi Gilbert,
can you please help me regarding my query for skipping the labels

thanks & regards
Inder

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to Skip labels on domain Axis when data is more

Post by paradoxoff » Sun Jul 12, 2009 9:35 pm

Here is an example for an XYPlot with bars and shapes and lines:

Code: Select all

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.JFrame;
import java.io.*;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.title.*;
import org.jfree.data.time.*;
import org.jfree.data.xy.DefaultXYDataset;

public class TimeBarsDemo {
	public static void main(String[] args) {
		Random random = new Random();
		TimeSeries barSeries = new TimeSeries("Week");
		TimeSeries shapeSeries = new TimeSeries("Number");
		for(int i = 1; i < 54; i++){
			barSeries.add(new Week(i, 2008), random.nextDouble() * 20);
			shapeSeries.add(new Week(i, 2008), random.nextDouble() * 40);
		}
		ValueAxis xAxis = new DateAxis("Week");
		ValueAxis yAxis = new NumberAxis("Values");
		XYItemRenderer renderer = new XYBarRenderer();
		TimeSeriesCollection barCollection = new TimeSeriesCollection(barSeries);
		TimeSeriesCollection shapeCollection = new TimeSeriesCollection(shapeSeries);
		shapeCollection.setXPosition(TimePeriodAnchor.MIDDLE);

		XYPlot plot = new XYPlot(barCollection, xAxis, yAxis, renderer);
		plot.setDataset(1, shapeCollection);
		plot.setRenderer(1, new XYLineAndShapeRenderer(true, true));
		JFreeChart chart = new JFreeChart("Time Bar Demo", new Font("Tahoma", 2, 18), plot, true);
		JFrame frame = new JFrame("Demo");
		frame.setContentPane(new ChartPanel(chart));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}

inder14
Posts: 11
Joined: Fri Jul 10, 2009 7:09 pm

Re: How to Skip labels on domain Axis when data is more

Post by inder14 » Mon Jul 13, 2009 4:35 am

Hello paradoxoff ,
thanks a lot for your help , but i am not able to get the desired result using the XYPlot.
is there any way i can use to skip the labels in categoryPlot?
please do let me know.

Thanks & Regards
Inder

inder14
Posts: 11
Joined: Fri Jul 10, 2009 7:09 pm

Re: How to Skip labels on domain Axis when data is more

Post by inder14 » Mon Jul 13, 2009 6:13 am

Hi All,
i got it , thanks a lot for all your help.

regards,
Inder

conductor80
Posts: 1
Joined: Wed Jul 15, 2009 3:23 am

Re: How to Skip labels on domain Axis when data is more

Post by conductor80 » Wed Jul 15, 2009 3:26 am

Hi Inder;

Can you share your solutions with us? I have the similar requirements also.

Conductor

jfreeuserblr
Posts: 1
Joined: Thu Oct 08, 2009 5:34 am
antibot: No, of course not.

Re: How to Skip labels on domain Axis when data is more

Post by jfreeuserblr » Thu Oct 08, 2009 5:38 am

Hi inder14,

We too have similar case of domain axis labels getting jammed for any category plot, Could you share the solution, as i see in ur reply that you could solve this.

nik9000
Posts: 1
Joined: Tue Oct 20, 2009 8:36 pm
antibot: No, of course not.

Re: How to Skip labels on domain Axis when data is more

Post by nik9000 » Tue Oct 20, 2009 9:28 pm

I did it by making a new CategoryAxis and then using it instead of the standard one. Its a little hacky but it get the job done.

Code: Select all

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryTick;
import org.jfree.text.TextBlock;
import org.jfree.ui.RectangleEdge;

/**
 * A category axis that only writes the tick labels on a few of the ticks.
 */
public class SparselyLabeledCategoryAxis extends CategoryAxis {
    private static final long serialVersionUID = 478725789943763302L;
    
    /**
     * The number of ticks to label.
     */
    private final int labeledTicks;

    /**
     * Construct an axis without a label.
     * @param labeledTicks show only this many labeled ticks
     */
    public SparselyLabeledCategoryAxis(int labeledTicks) {
        this.labeledTicks = labeledTicks;
    }

    /**
     * Construct and axis with a label.
     * @param labeledTicks show only this many labeled ticks
     * @param label the axis label
     */
    public SparselyLabeledCategoryAxis(int labeledTicks, String label) {
        super(label);
        this.labeledTicks = labeledTicks;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea,
            RectangleEdge edge) {
        List<CategoryTick> standardTicks = super.refreshTicks(g2, state, dataArea, edge);
        if (standardTicks.isEmpty()) {
            return standardTicks;
        }
        int tickEvery = standardTicks.size() / labeledTicks;
        if (tickEvery < 1) {
            return standardTicks;
        }
        
        //Replace a few labels with blank ones
        List<CategoryTick> fixedTicks = new ArrayList<CategoryTick>(standardTicks.size());
        //Skip the first tick so your 45degree labels don't fall of the edge
        CategoryTick tick = standardTicks.get(0);
        fixedTicks.add(new CategoryTick(tick.getCategory(), new TextBlock(), tick
            .getLabelAnchor(), tick.getRotationAnchor(), tick.getAngle()));
        for (int i = 1; i < standardTicks.size(); i++) {
            tick = standardTicks.get(i); 
            if (i % tickEvery == 0) {
                fixedTicks.add(tick);
            }
            else {
                fixedTicks.add(new CategoryTick(tick.getCategory(), new TextBlock(), tick
                    .getLabelAnchor(), tick.getRotationAnchor(), tick.getAngle()));
            }
        }
        return fixedTicks;
    }
}

Locked