2

我正在使用Jfreechart编写一个分析日志文件的项目,我有一个overvide generateToolTip的问题,我想要做的是当用户将鼠标移动到一个条的点时,“tip”将显示信息这个块(一个子任务),我需要显示每个子任务的描述。

我把我的例子放在这里,我只能显示最后一个子任务的描述。

如果可以的话,请给我一些帮助。谢谢

公共类测试{

class MyToolTipGenerator extends IntervalCategoryToolTipGenerator {

    DateFormat format;

    private MyToolTipGenerator(String value, DateFormat format) {
        super(value, format);
        this.format = format;
    }

    @Override
    public String generateToolTip(CategoryDataset cds, int row, int col) {

        final String s = super.generateToolTip(cds, row, col);
        TaskSeriesCollection tsc = (TaskSeriesCollection) cds;
        StringBuilder sb = new StringBuilder(s);
        String subTaskDes = null;

        // ArrayList<TaskSeries> ats=new ArrayList<>();
        // TaskSeries ts = new TaskSeries(s);

        int a = tsc.getSeriesCount();
        System.out.println(a + "________________");

        for (int i = 1; i < tsc.getSeriesCount(); i++) {
            TaskSeries ts = tsc.getSeries(i);
            Task task = ts.get(0);
            int count = task.getSubtaskCount();
            for (int j = 0; j < task.getSubtaskCount(); j++) {
                while (count-- != 0) {
                    subTaskDes = task.getSubtask(j).getDescription()
                            .toString();
                }
            }
            // for (int j = 0; j < tsc.getSubIntervalCount(row, col); j++) {
            // tsc.getSeries(0).getTasks().get(0).toString();
            // tsc.getSeries(key)
            // System.out.println(tsc.getSubIntervalCount(row, col));
            // tsc.getColumnKey(i).toString();
            // }
        }

        // System.out.println(ats.get(0).getDescription());

        for (int i = 0; i < tsc.getSubIntervalCount(row, col); i++) {
            sb.append(format.format(tsc.getStartValue(row, col, i)));
            sb.append("-");
            sb.append(format.format(tsc.getEndValue(row, col, i)));
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        // return sb.toString();
        return subTaskDes;
    }
}

private JFreeChart createChart() {
    IntervalCategoryDataset xyDataset = createDataset();
    JFreeChart jFreeChart = ChartFactory.createGanttChart("Gantt", "time",
            "value", xyDataset, true, true, true);
    CategoryPlot plot = jFreeChart.getCategoryPlot();
    plot.getRenderer().setBaseToolTipGenerator(
            new MyToolTipGenerator("{0}, {1}: ", DateFormat
                    .getTimeInstance(DateFormat.SHORT)));
    return jFreeChart;
}

private IntervalCategoryDataset createDataset() {
    TaskSeriesCollection dataset = new TaskSeriesCollection();
    TaskSeries unavailable1 = new TaskSeries("Unavailable 1");
    TaskSeries unavailable2 = new TaskSeries("Unavailable 2");
    TaskSeries unavailable3 = new TaskSeries("Unavailable 3");
    Task t1 = new Task("Meeting Room 1", date(7), date(18));
    t1.addSubtask(new Task("Meeting 1", date(9), date(16)));
    unavailable1.add(t1);

    Task t2 = new Task("Meeting Room 2", date(8), date(18));
    t2.addSubtask(new Task("Meeting 4", date(10), date(11)));
    t2.addSubtask(new Task("Meeting 5", date(13), date(15)));
    t2.addSubtask(new Task("Meeting 6", date(16), date(18)));
    unavailable2.add(t2);

    Task t3 = new Task("Meeting Room 3", date(8), date(18));
    t2.addSubtask(new Task("Meeting 7", date(11), date(11)));
    t2.addSubtask(new Task("Meeting 8", date(13), date(15)));
    t2.addSubtask(new Task("Meeting 9", date(18), date(18)));
    unavailable3.add(t3);

    dataset.add(unavailable1);
    dataset.add(unavailable2);
    dataset.add(unavailable3);
    return dataset;
}

private Date date(int hour) {
    final Calendar calendar = Calendar.getInstance();
    calendar.set(2009, Calendar.DECEMBER, 1, hour, 0, 0);
    return calendar.getTime();
}

private void display() {
    JFrame f = new JFrame("Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new ChartPanel(createChart()));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test().display();
        }
    });
}

}enter code here

4

2 回答 2

4

调用时,工厂设置 a ,如此IntervalCategoryToolTipGenerator所示。您可以自定义生成器以查看其他值是否有用,如此所示,或者您可以创建一个新的生成器。MessageFormatStandardCategoryToolTipGenerator

TaskSeriesCollection附录:这是在生成器中访问甘特图数据集的示例。对于单个子任务,您可以跟踪索引,如下所示

图片

import java.awt.EventQueue;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.IntervalCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;

/**
 * @see https://stackoverflow.com/a/15241250/230513
 */
public class Test {

    class MyToolTipGenerator extends IntervalCategoryToolTipGenerator {

        DateFormat format;

        private MyToolTipGenerator(String value, DateFormat format) {
            super(value, format);
            this.format = format;
        }

        @Override
        public String generateToolTip(CategoryDataset cds, int row, int col) {
            final String s = super.generateToolTip(cds, row, col);
            TaskSeriesCollection tsc = (TaskSeriesCollection) cds;
            StringBuilder sb = new StringBuilder(s);
            for (int i = 0; i < tsc.getSubIntervalCount(row, col); i++) {
                sb.append(format.format(tsc.getStartValue(row, col, i)));
                sb.append("-");
                sb.append(format.format(tsc.getEndValue(row, col, i)));
                sb.append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
            return sb.toString();
        }
    }

    private JFreeChart createChart() {
        IntervalCategoryDataset xyDataset = createDataset();
        JFreeChart jFreeChart = ChartFactory.createGanttChart("Gantt",
            "time", "value", xyDataset, true, true, true);
        CategoryPlot plot = jFreeChart.getCategoryPlot();
        plot.getRenderer().setBaseToolTipGenerator(
            new MyToolTipGenerator(
            "{0}, {1}: ", DateFormat.getTimeInstance(DateFormat.SHORT)));
        return jFreeChart;
    }

    private IntervalCategoryDataset createDataset() {
        TaskSeriesCollection dataset = new TaskSeriesCollection();
        TaskSeries unavailable = new TaskSeries("Unavailable");
        Task t1 = new Task("Meeting Room 1", date(7), date(18));
        t1.addSubtask(new Task("Meeting 1", date(9), date(16)));
        unavailable.add(t1);

        Task t2 = new Task("Meeting Room 2", date(8), date(18));
        t2.addSubtask(new Task("Meeting 4", date(10), date(11)));
        t2.addSubtask(new Task("Meeting 5", date(13), date(15)));
        t2.addSubtask(new Task("Meeting 6", date(16), date(18)));
        unavailable.add(t2);
        dataset.add(unavailable);
        return dataset;
    }

    private Date date(int hour) {
        final Calendar calendar = Calendar.getInstance();
        calendar.set(2009, Calendar.DECEMBER, 1, hour, 0, 0);
        return calendar.getTime();
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(createChart()));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
于 2013-03-06T07:17:21.540 回答
0

这是我编写的用于提取子任务描述作为工具提示的代码。它提供了显示不同工具提示信息的每个子任务。

它可能不是最好的编码示例,但它可以工作。您可以相应地修改并使用它。

class MyCategoryToolTipGenerator implements CategoryToolTipGenerator
{
    HashMap<String, List<Task>> subTaskMap;

    public MyCategoryToolTipGenerator()
    {
        subTaskMap = new HashMap<String, List<Task>>();
    }

    @Override
    public String generateToolTip(CategoryDataset categoryDataset, int row, int col )
    {

        TaskSeriesCollection taskSeriesCollection = (TaskSeriesCollection) categoryDataset;

        if ( row >= taskSeriesCollection.getRowCount() )
        {
            return "";
        }
        TaskSeries taskSeries = taskSeriesCollection.getSeries( row );


        if ( col >= taskSeries.getItemCount() )
        {
            return "";
        }
        Task task = taskSeries.get( col );

        //Sub Task tooltip handling//
        if( task.getSubtaskCount() > 0 )
        {
            //Task Key for main task
            String taskKey = "r=" + row + "c=" + col;

            //System.out.println( "Sub Task Count = " + task.getSubtaskCount() + " r=" + row + " c=" + col  );
            List<Task> subTaskList = null;
            if( subTaskMap.containsKey( taskKey ) )
            {
                //If already added, pick the list of available sub tasks
                subTaskList = subTaskMap.get( taskKey );
            }
            else
            {
                //If not found, add all sub tasks to map
                int subTaskCount = task.getSubtaskCount();
                subTaskList = new ArrayList<Task>( subTaskCount );
                for( int i = 0; i < subTaskCount; i++ )
                {
                    //System.out.println("Adding sub task " + task.getSubtask( i ).getDescription());
                    subTaskList.add( task.getSubtask( i ) );
                }
                subTaskMap.put( taskKey, subTaskList );
            }

            if( subTaskList != null && subTaskList.size() > 0 )
            {
                //Take the top most sub task and remove it from list
                Task subTask = subTaskList.remove( 0 );
                //System.out.println("Remove sub task " + subTask.getDescription());

                //If no other sub tasks available remove key from list
                if( subTaskList.size() <= 0 )
                {
                    //System.out.println("Remove Key " + taskKey);
                    subTaskMap.remove( taskKey );
                }

                //Return the top most subtask description
                return subTask.getDescription();
            }
        }

        return task.getDescription();
    }
}
于 2015-09-08T06:00:34.763 回答