I'm trying to display my logs in a Gantt Chart. Each log is represented as a Task and each Task has 7 subtasks, which represents the partiall timings. The subtasks are to be shown in the chart if their value is not 0 (otherwise they won't be shown...). The color of each subtask depends on their description.
I've found something close to what I want (http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=25206&sid=e506218a236895b1018d40d586b9fb7c) but in this example it's only being used to change the colors of the task depending on their description, soo not the subtask. I've also read somewhere that to change the subtask color it might be necessary to modify the drawTasks() method...but I really have no clue what to modify in this method... I'd be really thankful if someone could help me here...
This is how my code looks like at the moment:
public class Chart extends ApplicationFrame {
private static final long serialVersionUID = -8599149330893077525L;
private List<ChartEntry> logs = new ArrayList<ChartEntry>();
private ChartEntry chartEntry;
static IntervalCategoryDataset dataset;
final JFreeChart chart;
public Chart(final String title) {
super(title);
// create new dataset and chart
dataset = createDataset();
chart = createChart(dataset);
// create plot and renderer
// 04.02.2014 hier weitermachen, wenn MyGanttRenderer ist fertig
// save as PNG-File
Date today = new Date();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
File fileName = new File("/home/rinay/Dokumente/Auswertung Chart/"
+ DATE_FORMAT.format(today) + "_" + TestFirefox.testName
+ "_Chart.png");
try {
ChartUtilities.saveChartAsPNG(fileName, chart, 600, 900, null);
} catch (IOException e) {
throw new RuntimeException("Error saving a file", e);
}
// final ChartPanel chartPanel = new ChartPanel(chart);
// chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
// setContentPane(chartPanel);
}
private static class MyGanttRenderer extends GanttRenderer {
@Override
public Paint getItemPaint(int row, int col) {
Paint result;
// String subDescription = null;
XYTaskDataset tds = (XYTaskDataset) dataset;
TaskSeriesCollection tsc = tds.getTasks();
TaskSeries ts = tsc.getSeries(row);
Task t = ts.get(col);
// int count = t.getSubtaskCount();
// if (count == 0)
// return result;
// for(int i= 0; i< count;i++){
// subDescription = t.getSubtask(i).getDescription();
// }
result = getCategoryPaint(t.getDescription());
return result;
}
private Paint getCategoryPaint(String description) {
Paint result = Color.black;
switch (description) {
case "blocked":
result = Color.red;
break;
case "dns":
result = Color.orange;
break;
case "connect":
result = Color.yellow;
break;
case "ssl":
result = Color.green;
break;
case "send":
result = Color.cyan;
break;
case "wait":
result = Color.blue;
break;
case "receive":
result = Color.magenta;
break;
}
return result;
}
}
....
....
private IntervalCategoryDataset createDataset() {
// copy relevant information of each entry to a ChartEntry-Object
saveAsChartEntry(HarCreator.har);
// sort logs-List by date
sortLogsByDate();
// add log-List objects to JFreeChart-Dataset
final TaskSeries taskSeries = new TaskSeries("Zeiten");
// List<Task> tasks = new ArrayList<Task>();
// Test1: Logs-Anzahl ausgeben
System.out.println("Test 1: Anzahl Logs = " + logs.size());
// Test1
for (ChartEntry log : logs) {
// description of task
String description = log.getMethod() + " " + log.getURL();
// Start date
Date start = log.getDate();
// add total time of request to start date
Calendar cal = Calendar.getInstance();
Calendar timeCounter = Calendar.getInstance();
cal.setTime(start);
timeCounter.setTime(start);
cal.add(Calendar.MILLISECOND, (int) log.getTime());
Date end = cal.getTime();
// create new task
Task task = new Task(description, start, end);
for (int i = 0; i < log.getTimings().length; i++) {
String subDescription;
switch (i) {
case 0:
subDescription = "blocked";
break;
case 1:
subDescription = "dns";
break;
case 2:
subDescription = "connect";
break;
case 3:
subDescription = "ssl";
break;
case 4:
subDescription = "send";
break;
case 5:
subDescription = "wait";
break;
case 6:
subDescription = "receive";
break;
default:
subDescription = "";
break;
}
if (log.getTimingElement(i) != 0) {
Date subStart = timeCounter.getTime();
timeCounter.add(Calendar.MILLISECOND,
(int) log.getTimingElement(i));
Date subEnd = timeCounter.getTime();
Task subTask = new Task(subDescription, subStart, subEnd);
task.addSubtask(subTask);
} else
continue;
}
// add the new task above to tasks-List
taskSeries.add(task);
}
// Test 3: anzahl von tasks
System.out.println("Test 3: Anzahl von Tasks= " + taskSeries.getItemCount());
// Test 3
TaskSeriesCollection collection = new TaskSeriesCollection();
collection.add(taskSeries);
return collection;
}
....
....
The method getCategoryPaint is suppose to determine the color of the subtask based on the given description...but I don't know where to implement this method...