This example demonstrates the customization of Timeline. The events get a style class from the model, which is styled with custom CSS. The menu facet defines a custom menu, here buttons that move and zoom the timeline with the client side API, and the loading facet defines the loading screen.
<style>
.vis-item.available {
background-color: var(--p-green-500);
border-color: var(--p-green-700);
color: #fff;
}
.vis-item.unavailable {
background-color: var(--p-red-500);
border-color: var(--p-red-700);
color: #fff;
}
.vis-item.maybe {
background-color: var(--p-orange-500);
border-color: var(--p-orange-700);
color: #fff;
}
</style>
<h:form id="form">
<div class="card">
<p:timeline id="timeline" value="#{customTimelineView.model}"
editable="true" eventMargin="10" eventMarginAxis="0"
start="#{customTimelineView.start}" end="#{customTimelineView.end}"
stackEvents="false" ref="timeline">
<f:facet name="menu">
<p:commandButton type="button" icon="pi pi-chevron-left" styleClass="ml-2" onclick="PF('timeline').move(-0.3);" />
<p:commandButton type="button" icon="pi pi-chevron-right" styleClass="ml-2" onclick="PF('timeline').move(0.3);" />
<p:commandButton type="button" icon="pi pi-search-minus" styleClass="ml-2" onclick="PF('timeline').zoom(-0.5);" />
<p:commandButton type="button" icon="pi pi-search-plus" styleClass="ml-2" onclick="PF('timeline').zoom(0.5);" />
</f:facet>
<f:facet name="loading">
<h1>Loading please wait...</h1>
</f:facet>
</p:timeline>
</div>
</h:form>
package org.primefaces.showcase.view.data.timeline;
import org.primefaces.model.timeline.TimelineEvent;
import org.primefaces.model.timeline.TimelineModel;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.ThreadLocalRandom;
import jakarta.annotation.PostConstruct;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named("customTimelineView")
@ViewScoped
public class CustomTimelineView implements Serializable {
private TimelineModel<String, ?> model;
private LocalDateTime start;
private LocalDateTime end;
@PostConstruct
public void init() {
// set initial start / end dates for the axis of the timeline
start = LocalDate.of(-140, 1, 1).atStartOfDay();
end = LocalDate.of(-140, 1, 2).atStartOfDay();
// groups
String[] names = new String[]{"User 1", "User 2", "User 3", "User 4", "User 5", "User 6"};
// create timeline model
model = new TimelineModel<>();
for (String name : names) {
LocalDateTime end = start.minusHours(12).withMinute(0).withSecond(0).withNano(0);
for (int i = 0; i < 5; i++) {
LocalDateTime start = end.plusHours(Math.round(ThreadLocalRandom.current().nextDouble() * 5));
end = start.plusHours(4 + Math.round(ThreadLocalRandom.current().nextDouble() * 5));
long r = Math.round(ThreadLocalRandom.current().nextDouble() * 2);
String availability = (r == 0 ? "Unavailable" : (r == 1 ? "Available" : "Maybe"));
// create an event with content, start / end dates, editable flag, group name and custom style class
TimelineEvent event = TimelineEvent.builder()
.data(availability)
.startDate(start)
.endDate(end)
.editable(true)
.group(name)
.styleClass(availability.toLowerCase())
.build();
model.add(event);
}
}
}
public TimelineModel<String, ?> getModel() {
return model;
}
public LocalDateTime getStart() {
return start;
}
public LocalDateTime getEnd() {
return end;
}
}