Creating Gantt Charts using the .Net Chart Control - Visual WebGui ...
Creating Gantt Charts using the .Net Chart Control - Visual WebGui ...
Creating Gantt Charts using the .Net Chart Control - Visual WebGui ...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Creating</strong> <strong>Gantt</strong> <strong><strong>Chart</strong>s</strong> <strong>using</strong> <strong>the</strong> .<strong>Net</strong> <strong>Chart</strong> <strong>Control</strong><br />
<strong>Gantt</strong> charts (that is charts which graphically display tasks by start and end dates) are one of <strong>the</strong> more<br />
challenging type of charts to create. Using <strong>the</strong> .<strong>Net</strong> <strong>Chart</strong> control however makes it a fairly easy task.<br />
This sample shows how to use <strong>the</strong>.<strong>Net</strong> <strong>Chart</strong> control, <strong>using</strong> a VWG compatible wrapper for <strong>the</strong> control<br />
and printing charts <strong>using</strong> <strong>the</strong> iTextSharp PDF open source libraries and a download Gateway.<br />
Some background for <strong>the</strong> .<strong>Net</strong> <strong>Chart</strong> control, this is taken from Alex Gorev's Web blog:<br />
"So I will start with a little bit of history... Dundas is one of <strong>the</strong> leaders in data visualization,<br />
who provides well known <strong>Chart</strong>, Gauge, Map and o<strong>the</strong>r visual controls for different Microsoft<br />
development platforms (ASP.NET, Windows Forms, SQL Reporting Services and<br />
SharePoint). Microsoft acquired Dundas Data <strong>Visual</strong>ization Intellectual Property in April<br />
2007 and is integrating this technology in different Microsoft products. New <strong>Chart</strong> and Gauge<br />
report items were already released as part of SQL Reporting Services 2008.<br />
We also announced <strong>the</strong> new Map report item which will be available in <strong>the</strong> next release of<br />
SSRS.Microsoft <strong>Chart</strong> controls (ASP.NET and Windows Forms), released at PDC 2008, also<br />
based on <strong>the</strong> source code acquired from Dundas! Microsoft <strong>Chart</strong> control is available as a<br />
separate installation for .NET Framework 3.5 SP1 and will be part of .NET Framework 4.0."<br />
This blog can be found at: http://blogs.msdn.com/b/alexgor/archive/2008/11/07/microsoft-chartcontrol-vs-dundas-chart-control.aspx<br />
Also see: http://code.msdn.microsoft.com/mschart<br />
So, requirements are ei<strong>the</strong>r .<strong>Net</strong> Framework 3.5 SP1 OR .<strong>Net</strong> Framework 4.0.<br />
This simple project was built as:<br />
1) A Winforms example (<strong>Gantt</strong>ExampleWin.zip) <strong>using</strong> VS C# 2010 with a Windows application as<br />
<strong>the</strong> output type.<br />
2) A <strong>Visual</strong>Web GUI example (<strong>Gantt</strong>ExampleVwg.zip) <strong>using</strong> VS C#2010 with a class library<br />
(<strong>Gantt</strong>ExampleVWG.dll) as <strong>the</strong> output type. This dll should be included in <strong>the</strong> bin folder of <strong>the</strong><br />
web library.<br />
3) A Web site (<strong>Gantt</strong>ExampleWeb.zip) <strong>using</strong> VS Web Developer 2010, containing a default.aspx<br />
page, a bin folder, a Reports folder and <strong>the</strong> web.config file.
For <strong>the</strong> VWG version, Lufe provided me with a VB.net wrapper for <strong>the</strong> Ms<strong>Chart</strong><br />
control and I converted it into C#. Thanks Lufe, that was very much<br />
appreciated.<br />
To run <strong>the</strong> application on <strong>the</strong> web, add <strong>the</strong> following to <strong>the</strong> applications<br />
section of <strong>the</strong> web.config:<br />
<br />
<br />
<br />
To use <strong>the</strong> .<strong>Net</strong> <strong>Chart</strong> control, you also need to add an httphandler:<br />
<br />
...<br />
<br />
...<br />
<br />
To print <strong>the</strong> chart control, I use <strong>the</strong> iTextSharp open source libraries (version 5 or above) to create PDF<br />
documents on <strong>the</strong> fly. To print <strong>the</strong> chart area, we save <strong>the</strong> chart as a png image in <strong>the</strong> reports folder,<br />
and <strong>the</strong>n use <strong>the</strong> iTextSharp functions to get an instance of <strong>the</strong> image and include it in <strong>the</strong> document.<br />
Include <strong>the</strong> iTextSharp.dll in <strong>the</strong> bin directories of <strong>the</strong> web site and in <strong>the</strong> VWG C# project include a<br />
reference to it.<br />
You will need to have a folder named “Reports” in <strong>the</strong> root directory of <strong>the</strong> web site.<br />
The example was built <strong>using</strong> VWG Release 6.4.<br />
The example in <strong>the</strong> web.config is <strong>using</strong> .ashx instead of .wgx for <strong>the</strong> VWG application.<br />
Any questions or feedback, I can be reached at kenn@kennware.com
The basics<br />
Each task, start date and end date will be saved as a datapoint series of <strong>the</strong>ir own. We create a List of <strong>the</strong> series<br />
type as "private List seriesList = new List();".<br />
For each task, we want to store <strong>the</strong> x-ordinal of <strong>the</strong> task (so tasks or resource with <strong>the</strong> same<br />
name will be laid out side-by-side) and two date values for <strong>the</strong> y-ordinal.<br />
Datapoint values in <strong>the</strong> series are of <strong>the</strong> type "double" so we will convert <strong>the</strong> values to:<br />
string xlabel = string.Empty;<br />
double xordinal = 0;<br />
double yplot1 = 0;<br />
double yplot2 = 0;
Now we loop through all <strong>the</strong> tasks (or resources) we want to plot.<br />
For each task/resource:<br />
xlabel = is <strong>the</strong> name of <strong>the</strong> task or resource to show as <strong>the</strong> label<br />
xordinal = an ordinal for <strong>the</strong> x-axis, as an example, different tasks for<br />
<strong>the</strong> same resource would have <strong>the</strong> same xordinal when displaying a<br />
resource gantt chart<br />
yplot1 = <strong>the</strong> start date.<br />
yplot2 = <strong>the</strong> end date.<br />
// To cast a date as a double, use <strong>the</strong> Office Automation date<br />
(ToOADate) function, you cannot directly cast a datetime value<br />
to a double (Note: Office Automation Dates have different min<br />
and max values than a datetime value)<br />
yplot1 = (double)FromTime.ToOADate();<br />
yplot2 = (double)ToTime.ToOADate();<br />
// Use a different series for each datapoint<br />
Series seriesInstance = new Series();<br />
// For <strong>Gantt</strong> charts, we want a RangeBar graph type<br />
seriesInstance.<strong>Chart</strong>Type = Series<strong>Chart</strong>Type.RangeBar;<br />
// Have a start and end date so plotting 2 points on <strong>the</strong> y-axis<br />
seriesInstance.YValuesPerPoint = 2;<br />
// We want to draw datapoint side by side (night is day?)<br />
seriesInstance.CustomProperties = "DrawSideBySide=false";<br />
// Add <strong>the</strong> datapoint to <strong>the</strong> series, specifying tooltiptext, color<br />
and label<br />
xordinal = (double)itemIndex;<br />
seriesInstance.Points.AddXY(xordinal, yplot1, yplot2);<br />
seriesInstance.Points[0].ToolTip = someTipText;<br />
seriesInstance.Points[0].Color = resourceColor;<br />
seriesInstance.Points[0].AxisLabel = xlabel;<br />
seriesList.Add(seriesInstance);<br />
Loop again until all tasks/resources have been added to <strong>the</strong> seriesList.
Now that all <strong>the</strong> datapoints have been collected, add <strong>the</strong>m to <strong>the</strong> chart and update it.<br />
// Add all <strong>the</strong> series to <strong>the</strong> chart<br />
foreach (Series plotSeries in seriesList)<br />
{<br />
}<br />
chart1.Series.Add(plotSeries);<br />
// Force x-axis to show each task or resource<br />
chart1.<strong>Chart</strong>Areas[0].AxisX.Interval = 1;<br />
// Set y-axis to show each day of <strong>the</strong> month<br />
chart1.<strong>Chart</strong>Areas[0].AxisY.Interval = 1;<br />
// Set x-axis to show in reversed order so dates are displayed leftto-right<br />
chartArea1.AxisX.IsReversed = true;<br />
// Set o<strong>the</strong>r y-axis properties<br />
chartArea1.AxisY.IsStartedFromZero = false;<br />
chartArea1.AxisY.IsMarginVisible = false;<br />
chartArea1.AxisY.IsStartedFromZero = false;<br />
chartArea1.AxisY.IntervalType = DateTimeIntervalType.Days;<br />
// Set <strong>the</strong> y-axis labels<br />
chart1.<strong>Chart</strong>Areas[0].AxisY.Minimum = firstDate.AddDays(-1).ToOADate();<br />
chart1.<strong>Chart</strong>Areas[0].AxisY.Maximum = lastDate.AddDays(+1).ToOADate();<br />
//chart1.<strong>Chart</strong>Areas[0].AxisY.LabelStyle.Format = "MMM dd ddd";<br />
//chart1.<strong>Chart</strong>Areas[0].AxisY.LabelStyle.Format = "ddd MMM dd";<br />
chart1.<strong>Chart</strong>Areas[0].AxisY.LabelStyle.Format = "ddd M/d";<br />
// Force redraw of chart<br />
chart1.Update();
Sample output from <strong>the</strong> project (showing by task)<br />
Sample output from <strong>the</strong> project (showing by resource)