Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

java - Change the unit of x axis in JFreeChart Gantt chart

I'm a beginner in JFreeChart. I want to change the x axis values of this chart to milliseconds, with 5 ms intervals. I've tried

axis.setTickUnit(new DateTickUnit(DateTickUnitType.MILLISECOND, 5));

but I keep having a compilation error. I found somme suggestions in the net, but nothing worked for me. Also, is there any way to set a maximum value for the x axis, like 300 ms.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.GanttCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Gantt extends ApplicationFrame {

    private static final long serialVersionUID = 1L;

    public Gantt(final String title) {

        super(title);

        final GanttCategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }

    public static GanttCategoryDataset createDataset() {

        final TaskSeries s1 = new TaskSeries("P0");

        final Task t4 = new Task("P0", new SimpleTimePeriod(5, 50));
        final Task st41 = new Task("1", new SimpleTimePeriod(5, 10));
        // st41.setPercentComplete(1.0);
        final Task st42 = new Task("2", new SimpleTimePeriod(20, 30));

        final Task st43 = new Task("3", new SimpleTimePeriod(40, 50));

        t4.addSubtask(st41);
        t4.addSubtask(st42);
        t4.addSubtask(st43);
        s1.add(t4);

        final TaskSeries s2 = new TaskSeries("P1");

        final Task t2 = new Task("P", new SimpleTimePeriod(0, 10));
        final Task st21 = new Task("11", new SimpleTimePeriod(5, 10));

        final Task st22 = new Task("21", new SimpleTimePeriod(20, 30));

        final Task st23 = new Task("31", new SimpleTimePeriod(35, 90));

        t2.addSubtask(st21);
        t2.addSubtask(st22);
        t2.addSubtask(st23);
        s2.add(t2);

        final TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);
        collection.add(s2);

        return collection;
    }


    /*  private static Date date(final int day, final int month, final int year) {

     final Calendar calendar = Calendar.getInstance();
     calendar.set(year, month, day);
     final Date result = calendar.getTime();
     return result;

     */
    private JFreeChart createChart(final GanttCategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createGanttChart(
                "Gantt ", // chart title
                "PRO", // domain axis label
                "TIME", // range axis label
                dataset, // data
                true, // include legend
                true, // tooltips
                false // urls
        );

        CategoryPlot plot = chart.getCategoryPlot();

        DateAxis axis = (DateAxis) plot.getRangeAxis();

    //axis.setTickUnit(new DateTickUnit(DateTickUnitType.MILLISECOND, 10));
        axis.setDateFormatOverride(new SimpleDateFormat("S"));
        return chart;
    }

    public static void main(final String[] args) {

        final Gantt demo = new Gantt("Gantt");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some possibilities to consider:

  • Specify the units in the corresponding axis label when creating the chart.

    "TIME (ms)", // range axis label
    
  • Use setDateFormatOverride() to change the format of the axis labels, e.g. three-digit values.

    DateAxis axis = (DateAxis) plot.getRangeAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("SSS"));
    
  • Use setMaximumDate(), if warranted.

    axis.setMaximumDate(new Date(300));
    

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...