--
Also we added the ability to put stagger labels.
ravish wrote:I did this:
chart = ChartFactory.createBarChart3D(title, "", "Frequency", dataset, PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
plot = categoryPlot;
So replaced the instance I got from chart object created by the factory with the customized category plot instance initialized with your custom axis. But i think ChartFactory method either needs to allow setting this in constructor or there should be a setCategoryPlot method may be. It seems the code organisation needs a second look maybe?
Any comments?
hello,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.
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.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; } }
Regards,
Jaime
Code: Select all
@Override
protected AxisState drawCategoryLabels(Graphics2D g2, Rectangle2D plotArea,
Rectangle2D dataArea, RectangleEdge edge, AxisState state,
PlotRenderingInfo plotState) {
return drawCategoryLabels(g2, dataArea, edge, state, plotState);
}