| | | | Browse by category |
Question
How to force the label to always be visible on the activity in JViews Gantt?
Answer
In order to have your activities' label always visible, you can use a custom IlvActivityLabel
together with the IlvBasicActivityBar
to render your activities. You can combine activity renderers with the IlvActivityCompositeRenderer
class. For more detail information, refer to product guide section
IBM ILOG JViews Gantt V 8.x > Programmer's documentation > Developing with the JViews Gantt SDK > Gantt charts > Customizing activity rendering > Combining activity renderers
The custom IlvActivityLabel
is responsible for computing the activity's label location to ensure it is always visible. This is done through the getBounds()
method.
Below is an example of overriding the IlvActivityLabel.getBounds()
method so that the text stays left-aligned (the actual horizontal alignment property is ignored).
Note: Because the bounding box changes depends on the view transformer, you will need to disable optimized translation on the Gantt sheet to avoid redraw artifacts when scrolling.
IlvGanttChart.getGanttSheet().setOptimizedTranslation(false);
// This is a composite renderer, containing an activity bar renderer and a text
// label renderer. The text label tries to stay in the view by updating its
// xOffset whenever it is asked to recompute its bounding box. However, this
// requires that optimized translation is disabled or else redraw artifacts will
// be created when the chart is scrolled.
public class Renderer extends IlvActivityCompositeRenderer {
public Renderer() {
IlvBasicActivityBar bar = new IlvBasicActivityBar();
addRenderer(bar);
IlvActivityLabel label = new IlvActivityLabel(bar) {
/**
* Returns the bounding rectangle of the label.
*
* @param ag The activity graphic.
* @param t The transformer.
* @return The bounding rectangle of the label.
*/
public IlvRect getBounds(IlvActivityGraphic ag, IlvTransformer t) {
// We first need to update the label text from the activity property.
// Find out what the label should be.
IlvStringProperty property = getDisplayedProperty();
if (property != null) {
String text = property.getValue(ag.getActivity());
((IlvLabelInterface) getGraphic()).setLabel(text);
}
// Start with the text label aligned with the left edge of the main
// renderer and centered vertically.
IlvRect refBox;
IlvActivityRenderer mainRenderer = getMainRenderer();
if (mainRenderer != null) {
refBox = mainRenderer.getBounds(ag, t);
} else {
refBox = getDefinitionRect(ag, t);
}
IlvRect labelBox = ((IlvLabelInterface)getGraphic()).getLabelBBox(null);
labelBox.x = refBox.x + getOffset();
labelBox.y = refBox.y + (refBox.height - labelBox.height) / 2;
// Adjust the label to fit within the view. Note that the rects have been
// transformed, so they are in view coordinates. Therefore,
// x=0 is the left edge of the Gantt sheet.
float xOffset = getOffset();
if (labelBox.x < xOffset && labelBox.width + xOffset < refBox.width) {
labelBox.x = Math.min(xOffset, refBox.x + refBox.width - labelBox.width);
}
return labelBox;
}
};
label.setOffset(5f);
addRenderer(label);
}
}