Search code examples
c#asp.netdata-bindingchartsmschart

MS Chart Y Axis not staring from 0 Hours [RangeBar]


I am using a rangebar mschart to plot some data. Y axis represents the time ( total of 24 hours). The chart is plotting data correctly, but the y axis is not starting from 0 hours.

enter image description here

Code

 var IETable = (dt as System.ComponentModel.IListSource).GetList();
 chChart.DataBindCrossTable(IETable, "Number", "", "Start,Finish", "");

                  foreach (Series sr in chChart.Series)
                  {

                      sr.ChartType = SeriesChartType.RangeBar;
                      sr.YValueType = ChartValueType.Time;

                  }


                  chChart.ChartAreas[0].AxisY.LabelStyle.Format = "h : tt";
                  chChart.ChartAreas[0].AxisY.Interval = 2;
                  chChart.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Hours;
                  chChart.ChartAreas[0].AxisY.IntervalOffset = 0;
                  chChart.ChartAreas[0].AxisY.IsMarksNextToAxis = true;
                  chChart.ChartAreas[0].AxisY.IsStartedFromZero = true;


I also tried AxisY.Minimum=0 and AxisY.Maximum=1. This starts Y axis from 0 but then plotting is not visible.


Solution

  • DateTime minDate = new DateTime(0, 0, 0, 0, 0, 0);
    DateTime maxDate = minDate.AddHours(24);
    chChart.ChartAreas[0].AxisY.Minimum = minDate.ToOADate();
    chChart.ChartAreas[0].AxisY.Maximum = maxDate.ToOADate();
    

    Try this. You might need to change the new DateTime(0, 0, 0, 0, 0, 0); to fit the year and month etc when you will add stuff. Possible solution is to get minDate with DateTime.Now and substraction the hours, minutes and seconds