locked
Create a Gantt Chart RRS feed

  • Question

  • User1310055179 posted

    Hi,

    I wrote a code to create a Gantt chart and it works great when I select the "Days" view.

    However, I would like the user to be able to change the Y axis format to "Months" by changing the radio button "RadioButtonListGantt".

    When I try to change the radio button to "Months", the Y axis (dates) is empty.

    I would really appreciate your help with finding the right solution for my problem.

    Just to clarify, the start date and end date in the data source are in the following string format: "YYYYMMDD".

    void MakeGanttChart(DataSet graphDataSet, string[] cols)
            {
                double xordinal = 0;
                string xlabel = String.Empty;
                double yplot1 = 0;
                double yplot2 = 0;
                DateTime yplot1Date;
                DateTime yplot2Date;
                List<Series> seriesList = new List<Series>();
                DateTime firstdate = convertFromQsoftDate_2Datetime(graphDataSet.Tables[0].Rows[1][1].ToString());
                DateTime lastdate = convertFromQsoftDate_2Datetime(graphDataSet.Tables[0].Rows[1][2].ToString());
                int dateAggType = RadioButtonListGantt.SelectedIndex;
     
                if (cols.Length > 2)
                {
                    for (int c2 = 0; c2 < graphDataSet.Tables[0].Rows.Count; c2++)
                    {//loop over dataset rows- each row is a task in the gantt chart. create series per each row
                        xlabel = graphDataSet.Tables[0].Rows[c2][0].ToString();
                        yplot1Date = convertFromQsoftDate_2Datetime(graphDataSet.Tables[0].Rows[c2][1].ToString());
                        yplot2Date = convertFromQsoftDate_2Datetime(graphDataSet.Tables[0].Rows[c2][2].ToString());
                        yplot1 = (double)yplot1Date.ToOADate();//start date;
                        yplot2 = (double)yplot2Date.ToOADate();//end date;
     
                        //check min/max dates values
                        if (yplot1Date < firstdate)
                        {
                            firstdate = yplot1Date;
                        }
                        if (yplot2Date > lastdate)
                        {
                            lastdate = yplot2Date;
                        }
     
                        //use a different series for each datapoint
                        Series seriesInstance = new Series();
                        seriesInstance.ChartType = SeriesChartType.RangeBar;
                        seriesInstance.YValuesPerPoint = 2;
                        seriesInstance.CustomProperties = "DrawSideBySide=false";
                        switch (dateAggType)
                        {
                            case 0://Days aggregation
                                xordinal = (double)c2;
                                seriesInstance.Points.AddXY(xordinal, yplot1, yplot2);
                                break;
                            case 1://Months aggregation
                                xordinal = c2;
                                seriesInstance.Points.AddXY(xordinal, yplot1Date, yplot2Date);
                                break;
                        }
                        
                        seriesInstance.Points[0].ToolTip = "#AXISLABEL \n" + convertFromQsoftDate_2Datetime(graphDataSet.Tables[0].Rows[c2][1].ToString()) + "-" + convertFromQsoftDate_2Datetime(graphDataSet.Tables[0].Rows[c2][2].ToString()); 
                        seriesInstance.Points[0].AxisLabel = xlabel;
                        double usage; 
                        double.TryParse(graphDataSet.Tables[0].Rows[c2][3].ToString(), out usage);
                        if (usage == 0)
                        {
                            seriesInstance.Color = Color.Gainsboro;
                        }
                        if (usage == 100)
                        {
                            seriesInstance.Color = Color.Gray;
                        }
                        if (usage > 100)
                        {
                            seriesInstance.Color = Color.Orange;
                        }
                        if (usage < 100 && usage > 0)
                        {
                            seriesInstance.Color = Color.Silver;
                        }
                        
     
                        seriesList.Add(seriesInstance);
                    }
     
                    
                    Series s = Chart1.Series[0];
                    Chart1.Series.Remove(s);
     
                    foreach (Series plotSeries in seriesList)
                    {
                        Chart1.Series.Add(plotSeries);
                    }
                    
                    Chart1.ChartAreas[0].AxisX.Interval = 1;
                    //Chart1.ChartAreas[0].AxisY.Interval = 31;
                    Chart1.ChartAreas[0].AxisX.IsReversed = true;
                    
                    // enable autoscroll
                    //Chart1.ChartAreas[0].AxisX.
     
                    //other y axis properties
                    Chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;
                    Chart1.ChartAreas[0].AxisY.IsMarginVisible = false;
                    
     
                    //set the y axis labels
     
                    //Chart1.ChartAreas[0].AxisY.Minimum = firstdate.AddDays(-1).ToOADate();
                    //Chart1.ChartAreas[0].AxisY.Maximum = lastdate.AddDays(+1).ToOADate();
                    
                    switch (dateAggType)
                    {
                        case 0://Days aggregation
                            Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "d/M/yyyy"//day view
                            Chart1.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Days;
                            break;
                        case 1://Months aggregation
                            Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "yyyy-MM"//month view
                            Chart1.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Months;
                            break;
                    }
     
                    Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
                }
            }


    Tuesday, June 30, 2020 8:20 AM

All replies