Java 2 ways to implement a Gantt chart: Swiftgantt and Jfree

Source: Internet
Author: User
Tags pear

Http://blog.sina.com.cn/s/blog_50a7c4a601009817.html

the first method uses Swiftgantt to implement a Gantt Chart (the Progress map recommends This)Import java.awt.Color;
Import java.io.IOException;
Import java.util.GregorianCalendar;
Import com.yuxingwang.gantt.Config;
Import com.yuxingwang.gantt.GanttChart;
Import com.yuxingwang.gantt.model.GanttModel;
Import com.yuxingwang.gantt.model.Task;
Import com.yuxingwang.gantt.ui.TimeUnit;

public class Swiftganttchartdemo {


public static void main (string[] Args) throws ioexception{
1. First initialize the main class com.yuxingwang.gantt.GanttChart of a Gantt chart,
is a component of swing that inherits from Javax.swing.JScrollPanel and can be used to display a Gantt chart:
GanttChart Gantt = new GanttChart ();

2. Set the units of the timescale for the Gantt chart, such as in weeks, where each tick on the timeline represents one weeks:
Gantt.settimeunit (timeunit.day);

3. Configure the Gantt chart, where you can configure your Gantt chart with the Config class,
You can set the color, width, etc. of each element of the Gantt chart, and refer to the description of the Config class in the API documentation for detailed configuration Items.
Config config = gantt.getconfig ();
Config.setworkingtimebackcolor (color.red);

4. Create a Gantt chart data Model ganttmodel,
All of the project task information you want to display is provided to the GanttChart object through Ganttmodel.
Ganttmodel model = new Ganttmodel ();

5. Set the project start time and end Time:
Model.setkickofftime (new GregorianCalendar (2007, 1, 4));
Model.setdeadline (new GregorianCalendar (2007, 1, 15));

6. Create basic elements of a Gantt chart: task object tasks. A Task object behaves as a horizontal line in the Gantt chart.
Each Task object can contain any number of child task objects, forming a tree-like task model. If a Task object contains child tasks,
Automatically becomes an object group, the group of objects is still a task object, but appears as a different shape in the Gantt chart.
In the following example, Taskgroup is a task group that contains two subtasks, task1, and task2:
Task Taskgroup = new Task ("My work 1", new GregorianCalendar (1, 4), new GregorianCalendar (2007, 1, 15));
Task Task1 = new Task ("sub-task 1", new GregorianCalendar (1, 4), new GregorianCalendar (2007, 1, 8));
Task Task2 = new Task ("sub-task 2", new GregorianCalendar (1, 8), new GregorianCalendar (2007, 1, 15));

Task1.setbackcolor (color.cyan);
Task1.setbackcolor (color.green);
Taskgroup.add (new task[]{task1, task2});

7. Specifies a dependency between tasks. If a task needs to be completed before another task can begin,
Then you need to set up another task as a predecessor to this Task. :
Task2.addpredecessor (task1);

8. Add the main task group to the model of the Gantt Chart and pass the model object to Ganttchart:
Model.addtask (taskgroup);
Gantt.setmodel (model);
Then GanttChart can show the Gantt chart.

Specify the path to generate the Picture:
String FilePath = "d:\\gantt.jpg";
Gantt.generateimagefile (filePath);

This creates a Gantt Chart picture file on the path you specify.
}
}

Diagram: gantt.jpg

--------------------------------------------------------------

Jfreechart is an open source project on sourceforge.net, and its source code and APIs are available for Free. Jfreechart is very powerful, can realize pie chart (two and three-dimensional), histogram (horizontal, vertical), line chart, point chart, timing chart, Gantt chart, stock Market chart, Mix chart, Thermometer chart, Scale chart and other common business charts, graphics can be exported Into PNG and JPEG formats, and can also be associated with PDFs and EXCEL, enabling zooming, zooming, and 3D display of common graphics

The second method uses Jfree to implement the Gantt chart
Import java.awt.Color;
Import java.io.*;
Import java.util.ArrayList;
Import java.util.List;

Import org.jfree.data.*;
Import org.jfree.chart.*;
Import org.jfree.chart.plot.*;
Import org.jfree.chart.renderer.category.BarRenderer3D;
Import org.jfree.data.category.CategoryDataset;
Import org.jfree.data.category.DefaultCategoryDataset;
Import org.jfree.chart.demo.BarChartDemo1;

public class Jfreebarchartdemo {


public static void main (string[] Args) throws ioexception{
First Step: set up a data source
Categorydataset DataSet = GetDataSet2 ();
Step two: Generate Jfreechart Graphics
Jfreechart chart = Chartfactory.createbarchart3d (
"fruit Production Chart",//chart Title
"fruit",//catalog Axis Display Label
"output",//value Axis Display Label
Dataset,//dataset
Plotorientation.vertical,//chart direction: horizontal, Vertical
True,//whether The legend is displayed (must be false for a simple bar chart)
False,//whether the tool is generated
False//whether to generate a URL link
);
Step three: generate a special panel for loading graphics [jv5kl_q{
Chart.settitle ("set title here");
Categoryplot plot = Chart.getcategoryplot ();
Set color

Barrenderer3d renderer = new Barrenderer3d ();
Renderer.setseriespaint (0, color.green);
Renderer.setseriespaint (1, New Color (0, 100, 255));
Renderer.setseriespaint (2, color.red); Plot.setrenderer (renderer);


FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream ("d:\\fruit.jpg");
Chartutilities.writechartasjpeg (fos_jpg,1,chart,400,300,null);
} finally {
try {
Fos_jpg.close ();
} catch (Exception E) {}
}
}

private static Categorydataset GetDataSet () {
Defaultcategorydataset DataSet = new Defaultcategorydataset ();
Dataset.addvalue (+, null, "apple");
Dataset.addvalue (n, null, "pear");
Dataset.addvalue (+, null, "grape");
Dataset.addvalue (+, null, "banana");
Dataset.addvalue (+, null, "litchi");
Return dataset;
}

private static Categorydataset GetDataSet2 () {
Defaultcategorydataset DataSet = new Defaultcategorydataset ();

Dataset.addvalue (100, "beijing", "apple");
Dataset.addvalue (100, "shanghai", "apple");
Dataset.addvalue (100, "guangzhou", "apple");
Dataset.addvalue (200, "beijing", "pear");
Dataset.addvalue (200, "shanghai", "pear");
Dataset.addvalue (200, "guangzhou", "pear");
Dataset.addvalue (300, "beijing", "grape");
Dataset.addvalue (300, "shanghai", "grape");
Dataset.addvalue (300, "guangzhou", "grape");
Dataset.addvalue (400, "beijing", "banana");
Dataset.addvalue (400, "shanghai", "banana");
Dataset.addvalue (400, "guangzhou", "banana");
Dataset.addvalue (500, "beijing", "litchi");
Dataset.addvalue (500, "shanghai", "litchi");
Dataset.addvalue (500, "guangzhou", "litchi");
Return dataset;
}
}

Diagram: fruit.jpg

Java 2 ways to implement a Gantt chart: Swiftgantt and Jfree (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.