在JFreeChart甘特图中更改x轴的单位 [英] Change the unit of x axis in JFreeChart Gantt chart

查看:104
本文介绍了在JFreeChart甘特图中更改x轴的单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

    }
}


推荐答案

需要考虑的一些可能性:

Some possibilities to consider:


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

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

"TIME (ms)", // range axis label


  • 使用 setDateFormatOverride() 更改轴标签的格式,例如三位数值。

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


  • 使用 setMaximumDate() ,如果有保证。

  • Use setMaximumDate(), if warranted.

    axis.setMaximumDate(new Date(300));
    


  • 这篇关于在JFreeChart甘特图中更改x轴的单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆