Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / Visual Basic
Article

Gantt Chart

Rate me:
Please Sign up or sign in to vote.
4.75/5 (80 votes)
2 Oct 2008CPOL2 min read 590.2K   38.1K   254   171
Adds an easy to use Gantt chart to your application.

GanttChart.JPG

Introduction

This component makes it easy to add a Gantt chart to your application. You just need three lines of code to get it working.

This Gantt chart control includes these features:

  • The columns are automatically shown based on the width of the component and the time between the start date and the end date. If there're more than two days between those dates, it will only show the date; otherwise, it will show the time of the day (with minimum 5 minutes apart).
  • You can set the color (including the hover color) for each bar individually.
  • The Gantt chart automatically shows a custom scroll bar when it contains more rows than the visible area allows.
  • You can easily obtain information about the bar when hovering your mouse above one.
  • An easy to use multi-row tooltip text.
  • Change bars using your mouse.

Background

I tried to find a good, free, and easy Gantt chart control to use in my project, but didn't have any luck. Then, I decided to make one myself, which actually was easier than I thought it would be.

Using the code

To get the Gantt chart component to show something, you only need this:

VB
GanttChart1.FromDate = New Date(2007, 12, 12, 0, 0, 0)
GanttChart1.ToDate = New Date(2007, 12, 24, 0, 0, 0)
GanttChart1.AddChartBar("Row 1", Nothing, New Date(2007, 12, 12), 
    New Date(2007, 12, 16), Color.Aqua, Color.Khaki, 0))

As mentioned above, this component also includes a multi-line tooltip text.

VB
With GanttChart2
  Dim toolTipText As New List(Of String)
  toolTipText.Add("Time:")

  .ToolTipTextTitle = .MouseOverRowText
  .ToolTipText = toolTipText
 
End With

When a tooltip-text-row is included, the line is automatically bolded.

It is also possible to save the Gantt Chart to an image file:

VB
GanttChart2.SaveImage("C:\TestImage.jpg")

The included Zip file contains a project showing how to use its different features.

History

  • Version 0.55
    • Added the feature to drag the bars (after editing a bar, a BarChanged event is fired).
    • If time between start and end date is larger than 60 days, the Gantt chart switches over to showing months instead of days.
  • Version 0.54
    • The Gantt chart can now be saved to an image file.
  • Version 0.53
    • ScrollBar added to component.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to change line height for bars ? Pin
Mish.me3-Nov-12 10:12
Mish.me3-Nov-12 10:12 
Question1 Question and 1 Error Pin
mjfuchs2-Oct-12 2:06
mjfuchs2-Oct-12 2:06 
Questionsignatures on blocks Pin
ibrogim4-Sep-12 1:29
ibrogim4-Sep-12 1:29 
QuestionC# Version (bug fix help needed) Pin
Orilon1-Sep-12 0:08
professionalOrilon1-Sep-12 0:08 
GeneralRe: C# Version (bug fix help needed) Pin
Orilon2-Sep-12 23:59
professionalOrilon2-Sep-12 23:59 
Questionadd more rows Pin
woerny24-Jul-12 11:08
woerny24-Jul-12 11:08 
AnswerRe: add more rows Pin
Adagio.8124-Jul-12 19:26
Adagio.8124-Jul-12 19:26 
GeneralRe: add more rows Pin
woerny25-Jul-12 10:13
woerny25-Jul-12 10:13 
AnswerRe: add more rows Pin
Member 1356378822-Dec-17 6:43
Member 1356378822-Dec-17 6:43 
QuestionAdd a Text Column Pin
mono543218-Jul-12 7:06
mono543218-Jul-12 7:06 
AnswerRe: Add an other Text Column Pin
mono543213-Aug-12 11:27
mono543213-Aug-12 11:27 
QuestionFix for Ghost Bar Pin
Schoof13-Apr-12 1:00
Schoof13-Apr-12 1:00 
QuestionGhost Chart bar? Pin
Schoof5-Mar-12 4:52
Schoof5-Mar-12 4:52 
Hello,

First of all I wanted to thank you for your amazing control, it has been a real life-saver for me!

I have been trying to make the chart bar a bit "dynamic", I can increase / decrease the days, go to month view, etc...
But I have encountered a small glitch, when I navigate to the next day there is a bar that shouldn't be there (also nothing happens when I hover over it)

This my example: I added an event on the 25th of december, from 10 to 12.

C#
public Chart()
        {
            InitializeComponent();

            ganttChart1.FromDate = new DateTime(2012, 12, 24, 08, 0, 0);
            ganttChart1.ToDate = new DateTime(2012, 12, 24, 18, 0, 0);

            DateTime test = new DateTime(2012, 12, 25, 10, 00, 00);
            DateTime test2 = new DateTime(2012, 12, 25, 12, 00, 00);

            ganttChart1.AddChartBar("Row 1", null, test, test2, Color.Aqua, Color.Khaki, 0);}
            ganttChart1.AddChartBar("Row 2", null, test, test2, Color.Red, Color.DarkRed, 1);


This appears correctly like this: [^]

Now I used this code to navigate to the next day:

C#
private void button1_Click(object sender, EventArgs e)
{
    DateTime newFrom = ganttChart1.FromDate.AddDays(1);
    DateTime newTo = ganttChart1.ToDate.AddDays(1);

    ganttChart1.FromDate = newFrom;
    ganttChart1.ToDate = newTo;

    ganttChart1.PaintChart();
    ganttChart1.Refresh();

}


But when I click that button, a strange chart bar appears from the beginning of the Gantt Chart (no matter what the start hour is), it's also the exact same length as the normal event on the previous day. http://localhostr.com/file/f2HdDA2/gantt_2.png[^]

I have no idea what's causing this. This "Ghost" chart bar doesn't appear when I go back in the days or when I go to month view.

C#
private void button2_Click(object sender, EventArgs e)
{
    DateTime newFrom = ganttChart1.FromDate.AddDays(-1);
    DateTime newTo = ganttChart1.ToDate.AddDays(-1);

    ganttChart1.FromDate = newFrom;
    ganttChart1.ToDate = newTo;

    ganttChart1.PaintChart();
    ganttChart1.Refresh();
}

private void btnMaand_Click(object sender, EventArgs e)
{
    DateTime newFrom = new DateTime(ganttChart1.FromDate.Year, ganttChart1.FromDate.Month,1);
    DateTime newTo = new DateTime(ganttChart1.ToDate.Year, ganttChart1.ToDate.Month, 31);

    ganttChart1.FromDate = newFrom;
    ganttChart1.ToDate = newTo;

    ganttChart1.PaintChart();
    ganttChart1.Refresh();
}


I'm sorry to bother you with this problem, but I can't figure it out.
Thanks,
Thomas
QuestionDo you have c# version?? Pin
liusanpi28-Feb-12 16:30
liusanpi28-Feb-12 16:30 
AnswerRe: Do you have c# version?? Pin
Member 1098361031-Jul-14 20:12
Member 1098361031-Jul-14 20:12 
QuestionI don't know how to use its !! Pin
Highgain29-Jan-12 18:57
Highgain29-Jan-12 18:57 
GeneralMy vote of 2 Pin
musthe9930-Dec-11 19:24
musthe9930-Dec-11 19:24 
Question5 star Pin
chunk2-Nov-11 4:25
chunk2-Nov-11 4:25 
GeneralMy vote of 5 Pin
ralexs26-Aug-11 17:54
ralexs26-Aug-11 17:54 
Questionnice work mate Pin
shipMe24-Aug-11 1:03
shipMe24-Aug-11 1:03 
QuestionNeed C# form source code for studying Pin
rongconmocrang13-Jul-11 0:41
rongconmocrang13-Jul-11 0:41 
AnswerRe: Need C# form source code for studying Pin
Adagio.8113-Jul-11 0:50
Adagio.8113-Jul-11 0:50 
GeneralRe: Need C# form source code for studying Pin
rongconmocrang15-Jul-11 18:50
rongconmocrang15-Jul-11 18:50 
AnswerRe: Need C# form source code for studying Pin
thatraja26-Jan-12 20:42
professionalthatraja26-Jan-12 20:42 
GeneralVery good Pin
Member 392087330-Jun-11 15:33
Member 392087330-Jun-11 15:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.