import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
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.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
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.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;public class TestGannt {
 public static void main(String[] args) {
        IntervalCategoryDataset dataset = createSampleDataset();
        JFreeChart chart = ChartFactory.createGanttChart("任务管理系统","任务各阶段详细实施计划","任务周期",dataset,true,true,true);
        CategoryPlot plot=chart.getCategoryPlot();
        chart.getTitle().setFont(new Font("新宋体",Font.BOLD,20));
        CategoryAxis domainAxis=plot.getDomainAxis();
   //水平底部列表
   domainAxis.setLabelFont(new Font("新宋体",Font.BOLD,14));
   //水平底部标题
   domainAxis.setTickLabelFont(new Font("新宋体",Font.BOLD,12));
   //垂直标题
        ValueAxis rangeAxis = plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("新宋体",Font.BOLD,16));
        //用来控制时间轴的显示,防止乱码
        DateAxis da = (DateAxis)plot.getRangeAxis(0);
        System.out.println(da);
        da.setDateFormatOverride(new SimpleDateFormat("dd HH"));
        DateFormat format = new SimpleDateFormat("dd HH:mm");   
        DateTickUnit dtu = new DateTickUnit(DateTickUnitType.HOUR,1,format);  
      da.setTickUnit(dtu);//设置日期轴的日期标签  
        FileOutputStream fop = null;
        try{
//             System.out.println("Danny>> begin.");、
            fop = new FileOutputStream("G:\\gantt.jpg");
            ChartUtilities.writeChartAsJPEG(fop,1f, chart, 1600, 150,null);
            System.out.println("Danny>> successful...");
        } catch (IOException e){
            e.printStackTrace();
        } finally{
            try{
                fop.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    
    /** *//**
     * Utility method for creating <code>Date</code> objects.
     *
     * @param day 日
     * @param month 月
     * @param year 年
     *
     * @return a date.
     */
    private static Date date(int minute, int hourOfDay ,int day,  int month,  int year){          Calendar calendar = Calendar.getInstance();
        calendar.set(year,month,day,hourOfDay, minute);
        
         Date result = calendar.getTime();
        return result;
    }     /** 
     *
     * @return The dataset.
     */
    private static IntervalCategoryDataset createSampleDataset() {         TaskSeries s1 = new TaskSeries("SCHEDULE");
        
        Task t5 = new Task( "任务1", date(20,10,2,8,2011), date(30,16,2,8,2011)); 
        s1.add(t5);         
        TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);
        
        return collection;
    }
}上面的是个测试的例子,可以正常运行。
问题1是:怎么设置 da.setDateFormatOverride(new SimpleDateFormat("dd HH"));  X轴显示的dd是蓝色(天)HH(小时)为红色。
问题2是:怎么让X轴显示的时间固定为24小时制,哪怕我的Task时间段也就是不到24小时(24小时内)。
谢谢各位,本人最近正学习jfreechart希望各位大侠指导。

解决方案 »

  1.   

    好久没用jfreechart了,不知道楼主将天显示为蓝色,将小时显示为红色有什么意义,
    我只知道横纵度坐标的标题可以设置它的颜色。
    只能帮你顶一下了.
      

  2.   

    不会。但是如果是我的话会在GOOGLE里搜索:jfreechart X轴 颜色。应该会有你想要的结果。
      

  3.   

    jfreechart早已退出历史的舞台
    推荐你FusionCharts,flash的效果无敌而却上手简单
      

  4.   

    在你另外一个贴子回了。还写了这么多么代码。。用chartDirecot就10几行代码搞定了。