| | | | Browse by category |
Question
How do you set a different background color for a specific time interval in the Gantt Sheet ?
Answer
To do this, you can use the IlvVerticalGanttGrid and overwrite the draw method to draw in the background of the grid a rectangle in the color chosen for the highlight.
This methodology is being shown in the small class below:
class MyVerticalGanttGrid extends IlvVerticalGanttGrid
{
private Date _gridStartDate;
private Date _gridEndDate;
private Color _backgroundColor;
public MyVerticalGanttGrid(Date gridStartDate, Date gridEndDate, Color backgroundColor) {
if (gridStartDate.compareTo(gridEndDate) > 0)
{
System.out.println("Dates set are incorrect. Will invert them");
_gridEndDate = gridStartDate;
_gridStartDate = gridEndDate;
}
else
{
_gridStartDate = gridStartDate;
_gridEndDate = gridEndDate;
}
_backgroundColor = backgroundColor;
}
public void draw(Graphics dst,IlvGanttGridContext context)
{
IlvTransformer t = context.getTransformer();
IlvTimeConverter converter = context.getTimeConverter();
int x1, x2;
// calculate the coordinates for this specific dates
x1 = (int)converter.getUnits(_gridStartDate);
x2 = (int)converter.getUnits(_gridEndDate);
// create a Rectangle delimited by those two values, and using the full height of the gantt sheet
Rectangle rect = dst.getClipBounds();
IlvRect highlightRect = new IlvRect(x1,0,x2-x1,(int)rect.getHeight());
// apply the context transformer to the rectangle
if (t != null)
t.applyFloor(highlightRect);
Graphics2D g2d = (Graphics2D) dst;
g2d.setColor(_backgroundColor);
// draw the highligted rectangle:
g2d.fillRect(highlightRect.xFloor(), highlightRect.yFloor(), highlightRect.widthFloor(), highlightRect.heightFloor());
// and return to default setting to draw the normal grid
super.draw(dst, context);
}
};