java - 更改JFreeChart甘特图中x轴的单位

标签 java jfreechart

我是 JFreeChart 的初学者。我想将此图表的 x 轴值更改为毫秒,间隔为 5 毫秒。我已经尝试过

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

但我一直遇到编译错误。我在网上找到了一些建议,但没有任何对我有用。另外,有没有办法设置 x 轴的最大值,例如 300 毫秒。

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);

    }
}

最佳答案

一些需要考虑的可能性:

  • 创建图表时在相应轴标签中指定单位。

    "TIME (ms)", // range axis label
    
  • 使用setDateFormatOverride()更改轴标签的格式,例如三位数的值。

    DateAxis axis = (DateAxis) plot.getRangeAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("SSS"));
    
  • 使用setMaximumDate() ,如果有保证的话。

    axis.setMaximumDate(new Date(300));
    

image

关于java - 更改JFreeChart甘特图中x轴的单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693048/

相关文章:

java - Jackson - 反序列化期间不允许对象属性和数组元素为 null

java - 使用 xsd :assert? 基于非同级元素值的可选/强制元素

java - 在 JFreeChart 中制作自定义环形图

java - 在运行时更改 XYSeries 的 key

java - 从 JFree Spider Chart 隐藏标签

java - JFreeChart 时间序列不刷新

java - 如何在JFreeChart的气泡图上正确显示标签?

java - 是否调用new Object();两次使第一次调用创建的对象符合垃圾回收条件?

java - 与 PHP 相同的 Java 多数组/对象

java - 找不到 jumblr 的 .jar 文件(Tumblr android 包装器)