File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\NoOverlapCategoryAxis.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import org.jfree.chart.axis.CategoryAxis;
27 import org.jfree.chart.axis.AxisState;
28 import org.jfree.chart.axis.CategoryTick;
29 import org.jfree.chart.axis.CategoryLabelPosition;
30 import org.jfree.chart.plot.PlotRenderingInfo;
31 import org.jfree.chart.entity.EntityCollection;
32 import org.jfree.chart.entity.CategoryLabelEntity;
33 import org.jfree.ui.RectangleEdge;
34 import org.jfree.ui.RectangleAnchor;
35 import org.jfree.text.TextBlock;
36
37 import java.awt.*;
38 import java.awt.geom.Rectangle2D;
39 import java.awt.geom.Point2D;
40 import java.util.*;
41
42 /**
43 * This class implements X-axis label skipping algorithm to
44 * avoid drawing overlapping labels.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class NoOverlapCategoryAxis extends CategoryAxis {
49 public NoOverlapCategoryAxis(String label) {
50 super(label);
51 }
52
53 @Override
54 protected AxisState drawCategoryLabels(Graphics2D g2,
55 Rectangle2D plotArea,
56 Rectangle2D dataArea,
57 RectangleEdge edge,
58 AxisState state,
59 PlotRenderingInfo plotState) {
60
61 if (state == null) {
62 throw new IllegalArgumentException("Null 'state' argument.");
63 }
64
65 if (isTickLabelsVisible()) {
66 java.util.List ticks = refreshTicks(g2, state, plotArea, edge);
67 state.setTicks(ticks);
68
69 // remember the last drawn label so that we can avoid drawing overlapping labels.
70 Rectangle2D r = null;
71
72 int categoryIndex = 0;
73 Iterator iterator = ticks.iterator();
74 while (iterator.hasNext()) {
75
76 CategoryTick tick = (CategoryTick) iterator.next();
77 g2.setFont(getTickLabelFont(tick.getCategory()));
78 g2.setPaint(getTickLabelPaint(tick.getCategory()));
79
80 CategoryLabelPosition position
81 = this.getCategoryLabelPositions().getLabelPosition(edge);
82 double x0 = 0.0;
83 double x1 = 0.0;
84 double y0 = 0.0;
85 double y1 = 0.0;
86 if (edge == RectangleEdge.TOP) {
87 x0 = getCategoryStart(categoryIndex, ticks.size(),
88 dataArea, edge);
89 x1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea,
90 edge);
91 y1 = state.getCursor() - this.getCategoryLabelPositionOffset();
92 y0 = y1 - state.getMax();
93 }
94 else if (edge == RectangleEdge.BOTTOM) {
95 x0 = getCategoryStart(categoryIndex, ticks.size(),
96 dataArea, edge);
97 x1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea,
98 edge);
99 y0 = state.getCursor() + this.getCategoryLabelPositionOffset();
100 y1 = y0 + state.getMax();
101 }
102 else if (edge == RectangleEdge.LEFT) {
103 y0 = getCategoryStart(categoryIndex, ticks.size(),
104 dataArea, edge);
105 y1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea,
106 edge);
107 x1 = state.getCursor() - this.getCategoryLabelPositionOffset();
108 x0 = x1 - state.getMax();
109 }
110 else if (edge == RectangleEdge.RIGHT) {
111 y0 = getCategoryStart(categoryIndex, ticks.size(),
112 dataArea, edge);
113 y1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea,
114 edge);
115 x0 = state.getCursor() + this.getCategoryLabelPositionOffset();
116 x1 = x0 - state.getMax();
117 }
118 Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0),
119 (y1 - y0));
120 if(r==null || !r.intersects(area)) {
121 Point2D anchorPoint = RectangleAnchor.coordinates(area,
122 position.getCategoryAnchor());
123 TextBlock block = tick.getLabel();
124 block.draw(g2, (float) anchorPoint.getX(),
125 (float) anchorPoint.getY(), position.getLabelAnchor(),
126 (float) anchorPoint.getX(), (float) anchorPoint.getY(),
127 position.getAngle());
128 Shape bounds = block.calculateBounds(g2,
129 (float) anchorPoint.getX(), (float) anchorPoint.getY(),
130 position.getLabelAnchor(), (float) anchorPoint.getX(),
131 (float) anchorPoint.getY(), position.getAngle());
132 if (plotState != null && plotState.getOwner() != null) {
133 EntityCollection entities
134 = plotState.getOwner().getEntityCollection();
135 if (entities != null) {
136 String tooltip = getCategoryLabelToolTip(
137 tick.getCategory());
138 entities.add(new CategoryLabelEntity(tick.getCategory(),
139 bounds, tooltip, null));
140 }
141 }
142 r = bounds.getBounds2D();
143 }
144
145 categoryIndex++;
146 }
147
148 if (edge.equals(RectangleEdge.TOP)) {
149 double h = state.getMax();
150 state.cursorUp(h);
151 }
152 else if (edge.equals(RectangleEdge.BOTTOM)) {
153 double h = state.getMax();
154 state.cursorDown(h);
155 }
156 else if (edge == RectangleEdge.LEFT) {
157 double w = state.getMax();
158 state.cursorLeft(w);
159 }
160 else if (edge == RectangleEdge.RIGHT) {
161 double w = state.getMax();
162 state.cursorRight(w);
163 }
164 }
165 return state;
166 }
167 }
168