Help in using the code to skip the labels on domainaxis

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

Help in using the code to skip the labels on domainaxis

Post by inder14 » Sun Jul 12, 2009 1:25 pm

jsaiz wrote:
Below is the class that derives from CategoryAxis.

NOTE: This class has a drawback, in that it does not display tooltips on the tick labels. This is because drawCategoryLabels method prepares tooltips, but it has not access to CategoryAxis' private member categoryLabelToolTips, and there is no public nor protected getCategoryLabelToolTips() in CategoryAxis.


Code: Select all
/**
* This class enhances <code>CategoryAxis</code> in that it allows
* to skip some labels to be printed in the category axis.
* However, it does not display tooltips on the labels.
*/
public class CategoryAxisSkipLabels extends CategoryAxis
{
private static final int DEFAULT_INTERVAL = 1;
private int m_interval;

/** Default constructor. */
public CategoryAxisSkipLabels()
{
this(null, DEFAULT_INTERVAL);
}

/**
* Constructs an axis with a label.
* @param label Axis label (may be null).
*/
public CategoryAxisSkipLabels(String label)
{
this(label, DEFAULT_INTERVAL);
}

/**
* Constructs a category axis with a label and an interval.
* @param label Axis label (may be null).
* @param interval This number controls the labels to be printed.
* For instance, if <code>interval = 1</code>, all labels are printed; if
* <code>interval = 10</code>, only one of every 10 labels are printed (first label
* is always printed).
*/
public CategoryAxisSkipLabels(String label, int interval)
{
super(label);
m_interval = interval;
}

/**
* Draws the category labels and returns the updated axis state.
* NOTE: This method redefines the corresponding one in <code>CategoryAxis</code>,
* and is a copy of that, with added control to skip some labels to be printed.
*
* @param g2 the graphics device (<code>null</code> not permitted).
* @param dataArea the area inside the axes (<code>null</code> not
* permitted).
* @param edge the axis location (<code>null</code> not permitted).
* @param state the axis state (<code>null</code> not permitted).
* @param plotState collects information about the plot (<code>null</code>
* permitted).
*
* @return The updated axis state (never <code>null</code>).
*/
protected AxisState drawCategoryLabels(Graphics2D g2, Rectangle2D dataArea,
RectangleEdge edge, AxisState state,
PlotRenderingInfo plotState)
{
if (state == null)
{
throw new IllegalArgumentException("Null 'state' argument.");
}

if (isTickLabelsVisible())
{
g2.setFont(getTickLabelFont());
g2.setPaint(getTickLabelPaint());
List ticks = refreshTicks(g2, state, dataArea, edge);
state.setTicks(ticks);

int categoryIndex = 0;
Iterator iterator = ticks.iterator();
while (iterator.hasNext())
{
CategoryTick tick = (CategoryTick) iterator.next();
g2.setPaint(getTickLabelPaint());

CategoryLabelPosition position = getCategoryLabelPositions()
.getLabelPosition(edge);
double x0 = 0.0;
double x1 = 0.0;
double y0 = 0.0;
double y1 = 0.0;
if (edge == RectangleEdge.TOP)
{
x0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
x1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
y1 = state.getCursor() - getCategoryLabelPositionOffset();
y0 = y1 - state.getMax();
}
else if (edge == RectangleEdge.BOTTOM)
{
x0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
x1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
y0 = state.getCursor() + getCategoryLabelPositionOffset();
y1 = y0 + state.getMax();
}
else if (edge == RectangleEdge.LEFT)
{
y0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
y1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
x1 = state.getCursor() - getCategoryLabelPositionOffset();
x0 = x1 - state.getMax();
}
else if (edge == RectangleEdge.RIGHT)
{
y0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
y1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
x0 = state.getCursor() + getCategoryLabelPositionOffset();
x1 = x0 - state.getMax();
}
Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0));
Point2D anchorPoint =
RectangleAnchor.coordinates(area, position.getCategoryAnchor());

// THIS CODE IS NOW CONTROLLED BY THE "IF" =============
if (categoryIndex % m_interval == 0)
{
TextBlock block = tick.getLabel();
block.draw(g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
position.getLabelAnchor(), (float) anchorPoint.getX(),
(float) anchorPoint.getY(), position.getAngle());
Shape bounds = block.calculateBounds(g2, (float) anchorPoint.getX(),
(float) anchorPoint.getY(),
position.getLabelAnchor(),
(float) anchorPoint.getX(),
(float) anchorPoint.getY(),
position.getAngle());
if (plotState != null)
{
EntityCollection entities = plotState.getOwner().getEntityCollection();
if (entities != null)
{
//String tooltip = (String) categoryLabelToolTips.get(tick.getCategory());
String tooltip = null;
entities.add(new TickLabelEntity(bounds, tooltip, null));
}
}
}
// END IF ========================================

categoryIndex++;
}

if (edge.equals(RectangleEdge.TOP))
{
double h = state.getMax();
state.cursorUp(h);
}
else if (edge.equals(RectangleEdge.BOTTOM))
{
double h = state.getMax();
state.cursorDown(h);
}
else if (edge == RectangleEdge.LEFT)
{
double w = state.getMax();
state.cursorLeft(w);
}
else if (edge == RectangleEdge.RIGHT)
{
double w = state.getMax();
state.cursorRight(w);
}
}
return state;
}
}


What I propose is to add a constructor to CategoryAxis that receives the interval parameter, and redefine drawCategoryLabels to add the remarked "if". It would not break existing code (this would be only a new constructor) and the tooltips drawback would dissapear.

Regards,
Jaime

hello,
i got this code to skip the labels(code above given by Jaime) in categoryaxis but i am not sure how to use it..............
my code is here

//starts
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Stroke;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LayeredBarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.text.TextUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class SingleGraph extends ApplicationFrame {
public double maxvalue =0.0;

public SingleGraph(String titel) {
super(titel);

final JFreeChart chart = createChartBar();
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(600, 300));
setContentPane(chartPanel);




}
public static void main(final String[] args) {

final String title = "Score";
TextUtilities.setUseFontMetricsGetStringBounds(false);
final SingleGraph chart = new SingleGraph(title);
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);

}

public double[] run1() {
double[] run = new double[]{ 10, 6, 2, 4, 7, 2, 8, 12, 9
};
return run;
}
private CategoryDataset createRunDataset2() {
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

double[] run = run1();
String xvalues="";

for (int i = 0,k=0; i < run.length; i++) {
if(run>maxvalue){
maxvalue = run;
}

xvalues = "Inder"+i;

dataset.addValue(run, "Ist Team",xvalues);

}
return dataset;
}



private JFreeChart createChartBar() {
final CategoryDataset dataset1 = createRunDataset2();

final NumberAxis rangeAxis1 = new NumberAxis("Balls");
rangeAxis1.setLabelFont(new Font("Dialog", Font.BOLD, 11));
rangeAxis1.setUpperMargin(0.0);
rangeAxis1.setLowerBound(0);
rangeAxis1.setUpperBound(maxvalue);
rangeAxis1.setAxisLinePaint(Color.black);
rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis1.setAutoTickUnitSelection(true);
rangeAxis1.setAutoTickUnitSelection(true);

final CategoryAxis domainAxis = new CategoryAxis("Score by");
domainAxis.setTickMarksVisible(true);
// renderer1.setPositiveItemLabelPositionFallback(p2);
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setTickLabelFont(new Font("Helvetica", Font.ITALIC, 14));
domainAxis.setLowerMargin(0.0);
domainAxis.setUpperMargin(0.0);
domainAxis.setLabelFont(new Font("Dialog", Font.BOLD, 11));

final LayeredBarRenderer renderer1 = new LayeredBarRenderer();
renderer1.setSeriesPaint(0, Color.red);

final CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1, renderer1);
Stroke stroke = new BasicStroke(1,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_BEVEL);
subplot1.setDomainGridlineStroke(stroke);
subplot1.setRangeGridlineStroke(stroke);

subplot1.setRenderer(1, renderer1);

final CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis);
plot.add(subplot1, 1);
plot.setOutlinePaint(Color.RED);
plot.setOutlineVisible(true);

final JFreeChart chart = new JFreeChart(
"", new Font("SansSerif",Font.BOLD, 12),
plot, true);
chart.setBorderPaint(Color.BLACK);
chart.setBorderVisible(true);


return chart;
}
}

//end

Thanks in advance
Inder

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

Re: Help in using the code to skip the labels on domainaxis

Post by paradoxoff » Sun Jul 12, 2009 4:00 pm

The "domainAxis" that you are using to construct your CombinedDomainCategoryPlot is a normal CategoryAxis. If you want to use a CategoryAxisSkipLabels instead, you need to initialized the "domainAxis"-Variable accordingly.
Please use code tags the next time when you mix code and normal text.

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

Re: Help in using the code to skip the labels on domainaxis

Post by inder14 » Sun Jul 12, 2009 4:13 pm

hey thanks for that help...............
actually i am new to java ...................
can you please help me with code i have attached.............
i have made a bar chart using categoryplot can you please modify that code to use the skip label class...
that will be of great help

and yes i will take care to differenciate between code and normal text next time i post my query


Thanks & regards,
inder

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

Re: Help in using the code to skip the labels on domainaxis

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

inder14 wrote: i have made a bar chart using categoryplot can you please modify that code to use the skip label class...
I have already indicated the changes that I think you will need to make to use the skip labels class. After a brief look in the API, I doubt that it will work, because the CategoryAxis.drawCategoryLabels(...) method with the signature given in the skip labels class has been deprecated.

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

Re: Help in using the code to skip the labels on domainaxis

Post by inder14 » Mon Jul 13, 2009 3:39 am

Hello paradoxoff ,
can you please suggest some other way doing this (i.e. skip the labels).......
it will be so nice of you............i am struggling since last 2 weeks to get a solution for this........

thanks & regards
Inder

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

Re: Help in using the code to skip the labels on domainaxis

Post by inder14 » Mon Jul 13, 2009 7:26 am

Hi All,
thanks a lot for your help. i got the solution.

thanks & regards
Inder

WILLIAMer
Posts: 1
Joined: Wed Jul 15, 2009 3:13 am

Re: Help in using the code to skip the labels on domainaxis

Post by WILLIAMer » Wed Jul 15, 2009 4:36 am

hi inder14,
could you share your solution?

Thanks a lot!

rmuthoju
Posts: 1
Joined: Fri Sep 11, 2009 8:28 pm
antibot: No, of course not.

Re: Help in using the code to skip the labels on domainaxis

Post by rmuthoju » Fri Sep 11, 2009 8:48 pm

inder14 wrote:Hi All,
thanks a lot for your help. i got the solution.

thanks & regards
Inder
Hi Inder,
I have similar requirements for 3D barchart and have to show tooltip on mouse over on bars.

Could you please share the code.

Thanks,
Ravi.

Locked