Page 1 of 1

Draw Rectangle based on DateTime

Posted: Tue Feb 28, 2017 12:42 am
by 17279216
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?

Re: Draw Rectangle based on DateTime

Posted: Wed Mar 01, 2017 10:36 am
by Pep
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);
        }

Re: Draw Rectangle based on DateTime

Posted: Thu Mar 02, 2017 2:05 am
by 17279216
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 13031 times

Re: Draw Rectangle based on DateTime

Posted: Fri Mar 03, 2017 10:13 am
by Pep
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);
        }

Re: Draw Rectangle based on DateTime

Posted: Mon Mar 06, 2017 4:50 am
by 17279216
Thanks, that works : )