/**
* Adjusts the position of the legend to fit in its chart.
*/
public void updatePosition() {
double inset = chart.getDefaultInsets();
PreciseRectangle bbox = chart.getBBox();
double chartX = bbox.getX() + inset;
double chartY = bbox.getY() + inset;
double chartWidth = bbox.getWidth() - (inset * 2.0);
double chartHeight = bbox.getHeight() - (inset * 2.0);
if (isDisplayed()) {
// Find the position based on the dimensions
if (position == Position.LEFT) {
x = inset;
y = Math.floor(chartY + chartHeight / 2.0 - height / 2.0);
} else if (position == Position.RIGHT) {
x = Math.floor(chart.getSurface().getWidth() - width) - inset;
y = Math.floor(chartY + chartHeight / 2.0 - height / 2.0);
} else if (position == Position.TOP) {
x = Math.floor(chartX + chartWidth / 2.0 - width / 2.0);
y = inset;
} else if (position == Position.BOTTOM) {
x = Math.floor(chartX + chartWidth / 2.0 - width / 2.0);
y = Math.floor(chart.getSurface().getHeight() - height) - inset;
} else {
x = Math.floor(origX) + inset;
y = Math.floor(origY) + inset;
}
// Update the position of each item
for (int i = 0; i < items.size(); i++) {
items.get(i).updatePosition(x, y);
}
// Update the position of the containing box
if (border != null) {
PreciseRectangle rect = getBBox();
border.setX(rect.getX());
border.setY(rect.getY());
border.setWidth(rect.getWidth());
border.setHeight(rect.getHeight());
border.redraw();
}
}
}