Page 1 of 1

Relative vs. absolute time values on x-axis.

Posted: Wed Jun 22, 2011 8:48 am
by 15659035
Hi there,

I hope you can help me.

Our x-axis is always a DateTime axis. However, we would like to be able to switch between relative and absolute time, eg. switching from
relTime.jpg
relTime.jpg (8.91 KiB) Viewed 6646 times
to
absTime.jpg
absTime.jpg (8.15 KiB) Viewed 6646 times
and vice versa, without having to change the x-values within the line serie?

Thanks in advance.

BR,
Christian

Re: Relative vs. absolute time values on x-axis.

Posted: Wed Jun 22, 2011 3:40 pm
by 10050769
Hello Christian,

I have made a simple code that works with absolute and relative values. The absolute value is the DateTime of now and relative value is the subtract of absolute, DateTime now, and OldTime, DateTime there is when you initialize Chart:

Code: Select all

  public Form1()
            {
                InitializeComponent();
                InitializeChart();
            }
            Steema.TeeChart.Styles.Line line1;
            Random rnd;
            DateTime oldTime,relative;
            private void InitializeChart()
            {

                tChart1.Aspect.View3D = false;
                line1 = new Line(tChart1.Chart);
                oldTime = DateTime.Now;//Time to initializeChart.
                relative = new DateTime();

             }

        private void button1_Click(object sender, EventArgs e)
        {
            //Absolut time.
            tChart1.Series.Clear();
            line1 = new Line(tChart1.Chart);
            line1.XValues.DateTime=true;
            line1.DateTimeFormat = "HH:mm:ss";
            TimeSpan oneSecond = TimeSpan.FromSeconds(10);
            DateTime absolut = DateTime.Now;
            rnd = new Random();
            for(int i = 1; i <= 15; ++i)
            {
               line1.Add(absolut, rnd.Next(100), Color.Red);
               absolut += oneSecond;
            }
            tChart1.Axes.Bottom.Labels.DateTimeFormat = "HH:mm:ss";
            tChart1.Axes.Bottom.Labels.Angle = 90;
            tChart1.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(Steema.TeeChart.DateTimeSteps.FiveSeconds);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //relative value.
            tChart1.Series.Clear();
            line1 = new Line(tChart1.Chart);
            line1.XValues.DateTime=true;
            line1.DateTimeFormat = "HH:mm:ss";
            rnd = new Random();
            for(int i = 1; i <= 15; ++i)
            {
               DateTime absolut = DateTime.Now;
               TimeSpan oneSecond = absolut.Subtract(oldTime);
               relative += oneSecond;
               line1.Add(relative, rnd.Next(100), Color.Red);
            }
            tChart1.Axes.Bottom.Labels.DateTimeFormat = "HH:mm:ss";
            tChart1.Axes.Bottom.Labels.Angle = 90;
            tChart1.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(Steema.TeeChart.DateTimeSteps.TenSeconds);

        }
Could you tell us if previous code works as you expected?

I hope will helps.

Thanks,

Re: Relative vs. absolute time values on x-axis.

Posted: Tue Jun 28, 2011 2:39 pm
by 15659035
Hi Sandra,

Thanks for your reply. You example works as stated.

I was just hoping that there was some way to subtract a constant value from the shown values on an axis, if you get my meaning. In your example, I would have to clear the entire chart, and redraw everything, including color lines, annotations, draw lines ect., and of course all the series. We got up to 200 series, each with up to 60.000 points. As you can imagine, it would take some time to redraw everything :shock: :D

Anyway - if this is the only way to it, I guess that is that.

Again, thanks for your reply.

BR,
Christian

Re: Relative vs. absolute time values on x-axis.

Posted: Wed Jun 29, 2011 11:18 am
by 10050769
Hello Christian,

I have made a simple code that change absolute DateTime to relative DateTime, using a checkbox and GetAxisLabel method and I you can do something as next:

Code: Select all

public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }
        Steema.TeeChart.Styles.Line line1;
        Random rnd;
        DateTime oldTime, relative, absolute;
        TimeSpan oneSecond;
        String tmpLabel;
        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;
            line1 = new Line(tChart1.Chart);
           
            //Absolute time.
            tChart1.Series.Clear();
            line1 = new Line(tChart1.Chart);
            line1.XValues.DateTime = true;
            line1.DateTimeFormat = "HH:mm:ss";
            oneSecond = TimeSpan.FromSeconds(10);
            absolute = DateTime.Now;
            rnd = new Random();
            for (int i = 1; i <= 15; ++i)
            {
                line1.Add(absolute, rnd.Next(100), Color.Red);
                absolute += oneSecond;
            }
            checkBox1.Checked = true;
            tChart1.Axes.Bottom.Labels.DateTimeFormat = "HH:mm:ss";
            tChart1.Axes.Bottom.Labels.Angle = 90;
            tChart1.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(Steema.TeeChart.DateTimeSteps.FiveSeconds);
            tChart1.GetAxisLabel += new Steema.TeeChart.GetAxisLabelEventHandler(tChart1_GetAxisLabel);
            checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);
        }

        void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            tChart1.Refresh();
        
        }
        void tChart1_GetAxisLabel(object sender, Steema.TeeChart.GetAxisLabelEventArgs e)
        {
            if (sender == tChart1.Axes.Bottom)
            {
                if (!checkBox1.Checked)
                {
                   //Convert string to Correct DateTime.                  
                    DateTime tmpLabelDate = DateTime.Parse(e.LabelText);
                    //Calculate difference to absolute value and labelValue.
                    Double oneSecondValue = Math.Abs(absolute.ToOADate() - tmpLabelDate.ToOADate());
                    //ConvertDouble result to DateTime.
                    relative = DateTime.FromOADate(oneSecondValue);
                    e.LabelText = relative.ToLongTimeString();
                }
            }
        }
Could you check if previous code works as you expected?

Thanks,

Re: Relative vs. absolute time values on x-axis.

Posted: Wed Jun 29, 2011 2:32 pm
by 15659035
Hi again Sandra,

This was exactly what I was hoping for.
Thanks a million!!!

BR
Christian