Search code examples
xamarinxamarin.formsteechart

Steema TeeChart Xamarin Forms - X axis DateTime labels


I am building a candlestick chart with Steema TeeChart. I have this class for the data:

public class Candles
    {
        public DateTime date { get; set; }
        public double open { get; set; }
        public double high { get; set; }
        public double low { get; set; }
        public double close { get; set; }

        public Candles (long date, double open, double high, double low, double close)
        {
            this.date = epoch2string(date/1000000);
            this.open = open;
            this.high = high;
            this.low = low;
            this.close = close;
        }

        public Candles(int date)
        {
            this.date = epoch2string(date);
        }

        private DateTime epoch2string(long epoch) {
            return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epoch); 
        }
    }

I am adding the data with a loop:

foreach (var item in candleList) {
            chartStyle.Add (item.open, item.high, item.low, item.close);
        }

But how am I supposed to add the "date" value of my class as a label on the X axis?


Solution

  • Adding date as a DateTime value to each candle will automatically do this, for example:

      Steema.TeeChart.Styles.Candle candle1 = new Steema.TeeChart.Styles.Candle(tChart1.Chart);
      DateTime date = DateTime.Now;
    
      Random tmp = new Random();
    
      for (int i = 0; i < 10; i++)
      {
        double open = tmp.Next();
        double high = tmp.Next();
        double low = tmp.Next();
        double close = tmp.Next();
    
        candle1.Add(date.AddDays(i), open, high, low, close);
      }
    

    Which in your case should be:

        foreach (var item in candleList) {
            chartStyle.Add (item.date, item.open, item.high, item.low, item.close);
        }
    

    Depending on some chart settings this may not be automatic, in which case you can force that:

      candle1.XValues.DateTime = true;
      tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.Value;
      tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MMM";
    

    based on my example code.

    If you want to add date as text labels and remove weekend gaps you can use the RemoveGaps property or as shown in the All Features\Welcome !\Chart styles\Financial\Candle (OHLC)\Axis Labels no Weekends example at the features demo included with TeeChart for .NET. I posted an example based on the mentiond demo here.