Draw Rectangle based on DateTime

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
theyield
Newbie
Newbie
Posts: 16
Joined: Thu Sep 22, 2016 12:00 am

Draw Rectangle based on DateTime

Post by theyield » Tue Feb 28, 2017 12:42 am

Using xamarin.ios I need to draw rectangles on my chart based on a series xvalue which is a datetime.

ie from 6pm to 6am i need a rectangle drawn behind my main series.
how would i go about doing this?

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Draw Rectangle based on DateTime

Post by Pep » Wed Mar 01, 2017 10:36 am

Hello,
you could do it by using the OnBeforeDrawSeries event, for example :

Code: Select all

        Chart.BeforeDrawSeries += Chart_BeforeDrawSeries;

        private void Chart_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            var tmpHour = Utils.GetDateTimeStep(DateTimeSteps.OneHour);

            Rectangle rect = Chart.Chart.ChartRect;
            g.BackColor = Color.Red;
            g.Rectangle(rect.Top + 60, Chart.Axes.Bottom.CalcXPosValue(DateTime.Now.ToOADate() + tmpHour * 3),            Chart.Axes.Bottom.CalcXPosValue(DateTime.Now.ToOADate() + tmpHour * 4), rect.Bottom - 60);
        }

theyield
Newbie
Newbie
Posts: 16
Joined: Thu Sep 22, 2016 12:00 am

Re: Draw Rectangle based on DateTime

Post by theyield » Thu Mar 02, 2017 2:05 am

Thanks, but that doesn't stay in the right place when the chart allows panning.
Also, how do you draw more than one rectangle?

I have attached a sample image of what im after (basically indicating nighttime on the chart)
chartExample.jpg
chartExample.jpg (45.84 KiB) Viewed 13147 times
Attachments
ViewController.cs.zip
(1.42 KiB) Downloaded 887 times

Pep
Site Admin
Site Admin
Posts: 3303
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Re: Draw Rectangle based on DateTime

Post by Pep » Fri Mar 03, 2017 10:13 am

Hello,

I did a mistake passing the left parameter in the draw rectangle method. The following code works fine even if you pan over the Chart. You can also draw several rectangles (areas).

Code: Select all

        private void Chart_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            var tmpHour = Utils.GetDateTimeStep(DateTimeSteps.OneHour);

            Rectangle rect = Chart.Chart.ChartRect;
            g.BackColor = Color.Red;
            g.Rectangle(Chart.Axes.Bottom.CalcXPosValue(DateTime.Now.ToOADate() + tmpHour * 1), rect.Top + 60, Chart.Axes.Bottom.CalcXPosValue(DateTime.Now.ToOADate() + tmpHour * 3), rect.Bottom - 60);
            g.BackColor = Color.Yellow;
            g.Rectangle(Chart.Axes.Bottom.CalcXPosValue(DateTime.Now.ToOADate() + tmpHour * 4), rect.Top + 60, Chart.Axes.Bottom.CalcXPosValue(DateTime.Now.ToOADate() + tmpHour * 5), rect.Bottom - 60);
        }

theyield
Newbie
Newbie
Posts: 16
Joined: Thu Sep 22, 2016 12:00 am

Re: Draw Rectangle based on DateTime

Post by theyield » Mon Mar 06, 2017 4:50 am

Thanks, that works : )

Post Reply