container.addContainerProperty(Timeline.PropertyId.VALUE, Float.class, 0f);
// Add some random data to the container Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1);
Date today = new Date();
Random generator = new Random();
while(cal.getTime().before(today)){ // Create a point in time
Item item = container.addItem(cal.getTime());
//Set the timestamp property item.getItemProperty(Timeline.PropertyId.TIMESTAMP)
.setValue(cal.getTime());
//Set the value property item.getItemProperty(Timeline.PropertyId.VALUE)
.setValue(generator.nextFloat());
cal.add(Calendar.DAY_OF_MONTH, 1);
}
return container;
}
This method will create an indexed container with some random points. As you can see we are using an IndexedContainer and define two properties to it which was discussed earlier. Then we just generate some random data in the container. Here we are using the default property ids for the timestamp and value but you could use your own if you wished. We'll see later how you would tell the Timeline which property ids to use if you used your own.
Next, lets add some markers to our graph. Markers are arrow like shapes in the bottom of the timeline with which you can mark some occurrence that happened at that time.To create markers you again have to create a data source for them. I'll first show you how the code to create them and then explain what it all means. Add the following method to the main Application class:
/**
* Creates a marker container with a marker for each seven days */
public Container.Indexed createMarkerDataSource(){
// Create the container
Container.Indexed container = new IndexedContainer();
//Add the required property IDs (use the default ones here) container.addContainerProperty(Timeline.PropertyId.TIMESTAMP,
Date.class, null); container.addContainerProperty(Timeline.PropertyId.CAPTION,
String.class, "Our marker symbol"); container.addContainerProperty(Timeline.PropertyId.VALUE,
String.class, "Our description");
//Add a marker for every seven days
Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1);
Date today = new Date(); SimpleDateFormat formatter =
new SimpleDateFormat("EEE, MMM d, ''yy"); while(cal.getTime().before(today)){
// Create a point in time
Item item = container.addItem(cal.getTime());
// Set the timestamp property