📄 categoryaxis.java
字号:
return getCategoryStart(category, categoryCount, area, edge)
+ calculateCategorySize(categoryCount, area, edge) / 2;
}
/**
* Returns the end coordinate for the specified category.
*
* @param category the category.
* @param categoryCount the number of categories.
* @param area the data area.
* @param edge the axis location.
*
* @return the coordinate.
*/
public double getCategoryEnd(int category, int categoryCount, Rectangle2D area,
RectangleEdge edge) {
return getCategoryStart(category, categoryCount, area, edge)
+ calculateCategorySize(categoryCount, area, edge);
}
/**
* Calculates the size (width or height, depending on the location of the axis) of a category.
*
* @param categoryCount the number of categories.
* @param area the area within which the categories will be drawn.
* @param edge the axis location.
*
* @return the category size.
*/
protected double calculateCategorySize(int categoryCount, Rectangle2D area,
RectangleEdge edge) {
double result = 0.0;
double available = 0.0;
if ((edge == RectangleEdge.TOP) || (edge == RectangleEdge.BOTTOM)) {
available = area.getWidth();
}
else if ((edge == RectangleEdge.LEFT) || (edge == RectangleEdge.RIGHT)) {
available = area.getHeight();
}
if (categoryCount > 1) {
result = available * (1 - getLowerMargin() - getUpperMargin() - getCategoryMargin());
result = result / categoryCount;
}
else {
result = available * (1 - getLowerMargin() - getUpperMargin());
}
return result;
}
/**
* Calculates the size (width or height, depending on the location of the axis) of a category
* gap.
*
* @param categoryCount the number of categories.
* @param area the area within which the categories will be drawn.
* @param edge the axis location.
*
* @return the category gap width.
*/
protected double calculateCategoryGapSize(int categoryCount, Rectangle2D area,
RectangleEdge edge) {
double result = 0.0;
double available = 0.0;
if ((edge == RectangleEdge.TOP) || (edge == RectangleEdge.BOTTOM)) {
available = area.getWidth();
}
else if ((edge == RectangleEdge.LEFT) || (edge == RectangleEdge.RIGHT)) {
available = area.getHeight();
}
if (categoryCount > 1) {
result = available * getCategoryMargin() / (categoryCount - 1);
}
return result;
}
/**
* Estimates the space required for the axis, given a specific drawing area, without any
* information about the space required for the range axis/axes.
*
* @param g2 the graphics device (used to obtain font information).
* @param plot the plot that the axis belongs to.
* @param plotArea the area within which the axis should be drawn.
* @param edge the axis location (top or bottom).
* @param space the space already reserved.
*
* @return the estimated height required for the axis.
*/
public AxisSpace reserveSpace(Graphics2D g2, Plot plot, Rectangle2D plotArea,
RectangleEdge edge, AxisSpace space) {
// create a new space object if one wasn't supplied...
if (space == null) {
space = new AxisSpace();
}
// if the axis is not visible, no additional space is required...
if (!isVisible()) {
return space;
}
// calculate the max size of the tick labels (if visible)...
double tickLabelHeight = 0.0;
double tickLabelWidth = 0.0;
if (isTickLabelsVisible()) {
g2.setFont(getTickLabelFont());
refreshTicks(g2, 0.0, plotArea, plotArea, edge);
Insets tickLabelInsets = getTickLabelInsets();
if (RectangleEdge.isTopOrBottom(edge)) {
tickLabelHeight = tickLabelInsets.top + tickLabelInsets.bottom;
tickLabelHeight += getMaxTickLabelHeight(g2, plotArea, isVerticalCategoryLabels());
this.reservedForTickLabels = tickLabelHeight;
}
else if (RectangleEdge.isLeftOrRight(edge)) {
tickLabelWidth = tickLabelInsets.left + tickLabelInsets.right;
tickLabelWidth += getMaxTickLabelWidth(g2, plotArea);
this.reservedForTickLabels = tickLabelWidth;
}
}
// get the axis label size and update the space object...
Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge);
double labelHeight = 0.0;
double labelWidth = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
labelHeight = labelEnclosure.getHeight();
space.ensureAtLeast(labelHeight + tickLabelHeight, edge);
this.reservedForAxisLabel = labelHeight;
}
else if (RectangleEdge.isLeftOrRight(edge)) {
labelWidth = labelEnclosure.getWidth();
space.ensureAtLeast(labelWidth + tickLabelWidth, edge);
this.reservedForAxisLabel = labelWidth;
}
return space;
}
/**
* Configures the axis against the current plot. Nothing required in this class.
*/
public void configure() {
CategoryPlot plot = (CategoryPlot) this.getPlot();
this.categories = plot.getCategories();
}
/**
* Draws the axis on a Java 2D graphics device (such as the screen or a printer).
*
* @param g2 the graphics device.
* @param cursor the cursor location.
* @param plotArea the area within which the axis should be drawn.
* @param dataArea the area within which the plot is being drawn.
* @param edge the location of the axis.
*
* @return The new cursor location.
*/
public double draw(Graphics2D g2, double cursor,
Rectangle2D plotArea, Rectangle2D dataArea,
RectangleEdge edge) {
// if the axis is not visible, don't draw it...
if (!isVisible()) {
return 0.0;
}
double used1 = 0.0;
// draw the category labels
if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
used1 = drawHorizontalCategoryLabels(g2, cursor, plotArea, dataArea, edge);
}
else if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
used1 = drawVerticalCategoryLabels(g2, cursor, plotArea, dataArea, edge);
}
if (edge == RectangleEdge.TOP || edge == RectangleEdge.LEFT) {
cursor = cursor - used1;
}
else if (edge == RectangleEdge.BOTTOM || edge == RectangleEdge.RIGHT) {
cursor = cursor + used1;
}
// draw the axis label...
double used2 = drawLabel(getLabel(), g2, cursor, plotArea, dataArea, edge);
return used1 + used2;
}
/**
* Draws the category labels when the axis is 'horizontal'.
*
* @param g2 the graphics device.
* @param cursor the cursor location.
* @param plotArea the plot area.
* @param dataArea the area inside the axes.
* @param edge the axis location.
*
* @return The new cursor location.
*/
protected double drawHorizontalCategoryLabels(Graphics2D g2, double cursor,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge) {
if (isTickLabelsVisible()) {
Font tickLabelFont = getTickLabelFont();
g2.setFont(tickLabelFont);
g2.setPaint(getTickLabelPaint());
refreshTicks(g2, cursor, plotArea, dataArea, edge);
Iterator iterator = getTicks().iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (obj instanceof Tick) {
Tick tick = (Tick) obj;
if (isVerticalCategoryLabels()) {
RefineryUtilities.drawRotatedString(tick.getText(), g2,
tick.getX(), tick.getY(),
-Math.PI / 2);
}
else {
g2.drawString(tick.getText(), tick.getX(), tick.getY());
}
}
else {
Tick[] ts = (Tick[]) obj;
for (int i = 0; i < ts.length; i++) {
g2.drawString(ts[i].getText(), ts[i].getX(), ts[i].getY());
}
}
}
}
return this.reservedForTickLabels;
}
/**
* Draws the category labels when the axis is 'vertical'.
*
* @param g2 the graphics device.
* @param cursor the cursor location.
* @param plotArea the plot area.
* @param dataArea the area inside the axes.
* @param edge the axis location.
*
* @return The new cursor location.
*/
protected double drawVerticalCategoryLabels(Graphics2D g2, double cursor,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge) {
if (isTickLabelsVisible()) {
g2.setFont(getTickLabelFont());
g2.setPaint(getTickLabelPaint());
refreshTicks(g2, cursor, plotArea, dataArea, edge);
Iterator iterator = getTicks().iterator();
while (iterator.hasNext()) {
Tick tick = (Tick) iterator.next();
g2.drawString(tick.getText(), tick.getX(), tick.getY());
}
}
return this.reservedForTickLabels;
}
/**
* Returns <code>true</code> if the specified plot is compatible with the axis, and
* <code>false</code> otherwise.
*
* @param plot the plot.
*
* @return A boolean.
*/
protected boolean isCompatiblePlot(Plot plot) {
return (plot instanceof CategoryPlot);
}
/**
* Tests this axis for equality with another object.
*
* @param obj the object.
*
* @return <code>true</code> or <code>false</code>.
*/
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj instanceof CategoryAxis) {
CategoryAxis ca = (CategoryAxis) obj;
if (super.equals(obj)) {
boolean b0 = (this.lowerMargin == ca.lowerMargin);
boolean b1 = (this.upperMargin == ca.upperMargin);
boolean b2 = (this.categoryMargin == ca.categoryMargin);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -