Search code examples
c#teechart

Datetime X-axis for Boxplot series in TeeChart


Currently, I have multiple boxplots in TeeChart that I have added like so:

seriesIndex = 0;
foreach(var dataGroup in DataGroups) //Each dataGroup contains all the ParameterValues at a specific point in time
{
    var series = new Box() { ... }
    var values = dataGroup.ParameterValues;
    series.Add(seriesIndex, values);
    seriesIndex++;
    Chart.Series.Add(series);
}

I want to convert this so that the X-axis uses a DateTime value (defined as below):

var timeIndex = dataGroup.TimeSeriesIndex;

However, the Box class's Add method does not support DateTime values. And when I use the inherited (from the base Series class) Add(DateTime, double) method (within a foreach loop), all the DateTime values become 12 AM December 31, 1899 which I recognize to be the base value for DateTime.ToOADate. This leads me to believe that I am not inputting the data correctly into the series. Can somebody point me in the right direction?


Solution

  • all the DateTime values become 12 AM December 31, 1899 which I recognize to be the base value for DateTime.ToOADate.

    Exactly, that's the way vertical box plot works in TeeChart. The X position is determined by its Position property, which is zero by default. To achieve what you request you should set a position for each box plot. This can be done assigning the Position property or via the specific add method override as shown in the code snippet below. For DateTime labels, you can just set XValues.DateTime to true and let TeeChart calculate labels automatically or use the labels trick shown in this code:

      tChart1.Aspect.View3D = false;
    
      var boxSeries1 = new Steema.TeeChart.Styles.Box(tChart1.Chart);
      var boxSeries2 = new Steema.TeeChart.Styles.Box(tChart1.Chart);
      var boxSeries3 = new Steema.TeeChart.Styles.Box(tChart1.Chart);
    
      boxSeries1.Add(DateTime.Now.AddDays(0).ToOADate(), new double[6] { 3, 6, 8, 15, 19, 21 });
      boxSeries2.Add(DateTime.Now.AddDays(1).ToOADate(), new double[4] { 5, 7, 12, 21 });
      boxSeries3.Add(DateTime.Now.AddDays(2).ToOADate(), new double[5] { 6, 7, 8, 15, 21 });
    
      // A simple trick to force custom axis labels on bottom axis.
      // In this case, series titles
      Steema.TeeChart.AxisLabelsItems labels = tChart1.Axes.Bottom.Labels.Items;
      labels.Clear();
    
      foreach (Steema.TeeChart.Styles.Box b in tChart1.Series)
      {
        b.XValues.DateTime = true;
        labels.Add(b.Position);
      }
    
      tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MM/yyyy hh:mm";