我有一个甘特图,它从数据库中获取数据集,而我
坚持使用chartMouseClicked
。我想打印细节
每当我单击任何子任务时,该子任务中的位置。
GanttChartDemo23.java
public class GanttDemo23 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public static final TaskSeriesCollection model = new TaskSeriesCollection();
public GanttDemo23(final String title) {
super(title);
final IntervalCategoryDataset dataset = createSampleDataset();
// create the chart...
final JFreeChart chart = ChartFactory.createGanttChart(
"Gantt Chart", // chart title
"Flights", // domain axis label
"Time", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
chartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent event) {
CategoryItemEntity entity = (CategoryItemEntity) event.getEntity();
JPanel panel = new JPanel(new GridLayout(0, 1));
Comparable row = entity.getRowKey();
Comparable col = entity.getColumnKey();
panel.add(new JLabel(String.valueOf(row.toString())));
panel.add(new JLabel(String.valueOf(col)));
panel.add(new JLabel(String.valueOf(entity.getDataset().getValue(row, col))));
JOptionPane.showMessageDialog(rootPane, panel);
}
@Override
public void chartMouseMoved(ChartMouseEvent arg0) {
// TODO Auto-generated method stub
}
});
//GanttRenderer personnalisé..
MyRenderer renderer = new MyRenderer(model);
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.BLACK);
}
/**
* @see https://stackoverflow.com/questions/8938690
*/
private static class MyRenderer extends GanttRenderer {
private static final int PASS = 2; // assumes two passes
private final List<Color> clut = new ArrayList<>();
private final TaskSeriesCollection model;
private int row;
private int col;
private int index;
public MyRenderer(TaskSeriesCollection model) {
this.model = model;
}
@Override
public Paint getItemPaint(int row, int col) {
if (clut.isEmpty() || this.row != row || this.col != col) {
initClut(row, col);
this.row = row;
this.col = col;
index = 0;
}
int clutIndex = index++ / PASS;
return clut.get(clutIndex);
}
private void initClut(int row, int col) {
clut.clear();
TaskSeries series = (TaskSeries) model.getRowKeys().get(row);
List<Task> tasks = series.getTasks(); // unchecked
int taskCount = tasks.get(col).getSubtaskCount();
taskCount = Math.max(1, taskCount);
System.out.println("----> " + taskCount);
String description;
for (int i = 0; i < taskCount; i++) {
description = tasks.get(col).getSubtask(i).getDescription();
System.out.println(description + ": " + i);
if (description.equals("Cancelled")) {
clut.add(Color.red);
System.out.println("green");
}
if (description.equals("Delayed")) {
clut.add(Color.yellow);
System.out.println("yellow");
}
if (description.equals("On Time")) {
clut.add(Color.blue);
System.out.println("blue");
}
}
}
}
private IntervalCategoryDataset createSampleDataset() {
System.out.println(java.time.LocalDate.now());
int month = java.time.LocalDate.now().getMonthValue();
int year = java.time.LocalDate.now().getYear();
int day = java.time.LocalDate.now().getDayOfMonth();
List<String> fltEqps = new ArrayList<>();
fltEqps.add("DNA");
fltEqps.add("DNB");
fltEqps.add("DNC");
final TaskSeries s1 = new TaskSeries("Scheduled");
for (String fltEqp : fltEqps) {
MyConnection dao = new MyConnection();
final Task t = new Task(fltEqp, date(day, (month-1), year, 00,00), date(day, (month-1), year, 23,59));
List <FlightDetails> flightDetails = dao.flightDetails(fltEqp, java.time.LocalDate.now());
/* This is the Sample of Flight Data, being retrieved from the Db.
FlightDetails flightDetail1 = new FlightDetails();
flightDetail1.setArrTime("0830");
flightDetail1.setDepTime("1030");
flightDetail1.setDuration("0200");
flightDetail1.setFltArr("ISB");
flightDetail1.setFltDep("KHI");
flightDetail1.setFltDate("2018-10-07");
flightDetail1.setFltEqp("738");
flightDetail1.setFltEqpRegNo("DNB");
flightDetail1.setFltNo("0502");
flightDetails.add(flightDetail1);*/
for (FlightDetails flightDetail : flightDetails)
{
int depHour = Integer.valueOf(flightDetail.getDepTime().substring(0, 2));
int depMin = Integer.valueOf(flightDetail.getDepTime().substring(2));
int arrHour = Integer.valueOf(flightDetail.getArrTime().substring(0, 2));
int arrMin = Integer.valueOf(flightDetail.getArrTime().substring(2));
final Task st0 = new Task("On Time", date(day, (month-1), year, depHour,depMin),
date(day, (month-1), year, arrHour,arrMin));
t.addSubtask(st0);
}
s1.add(t);
}
model.add(s1);
return model;
}
private static Date date(final int date, final int month, final int year, final int hourOfDay, final int minute) {
final Calendar calendar = Calendar.getInstance();
calendar.set(year, month, date, hourOfDay, minute);
final Date result = calendar.getTime();
return result;
}
public static void main(final String[] args) {
final GanttDemo23 demo = new GanttDemo23("Gantt Chart Demo 23");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
这是我收到的消息。我想显示子任务详细信息。